Fix partial completions, add space, cancel on input

codeql
Jeremy Mahieu 5 years ago
parent e9633bf721
commit f8608f4738
  1. 29
      js/inputbar.js

@ -31,6 +31,9 @@ weechat.directive('inputBar', function() {
// Emojify input. E.g. Turn :smile: into the unicode equivalent, but // Emojify input. E.g. Turn :smile: into the unicode equivalent, but
// don't do replacements in the middle of a word (e.g. std::io::foo) // don't do replacements in the middle of a word (e.g. std::io::foo)
$scope.inputChanged = function() { $scope.inputChanged = function() {
// Cancel any command completion that was still ongoing
commandCompletionInputChanged = true;
var emojiRegex = /^(?:[\uD800-\uDBFF][\uDC00-\uDFFF])+$/, // *only* emoji var emojiRegex = /^(?:[\uD800-\uDBFF][\uDC00-\uDFFF])+$/, // *only* emoji
changed = false, // whether a segment was modified changed = false, // whether a segment was modified
inputNode = $scope.getInputNode(), inputNode = $scope.getInputNode(),
@ -115,15 +118,20 @@ weechat.directive('inputBar', function() {
var previousInput; var previousInput;
var commandCompletionList; var commandCompletionList;
var commandCompletionAddSpace;
var commandCompletionBaseWord; var commandCompletionBaseWord;
var commandCompletionPosition; var commandCompletionPosition;
var commandCompletionPositionInList; var commandCompletionPositionInList;
var commandCompletionInputChanged;
$scope.completeCommand = function(direction) { $scope.completeCommand = function(direction) {
if ( !$scope.command.startsWith('/') ) { if ( !$scope.command.startsWith('/') ) {
// We are not completing a command, maybe a nick? // We are not completing a command, maybe a nick?
return; return;
} }
// Cancel if input changes
commandCompletionInputChanged = false;
// input DOM node // input DOM node
var inputNode = $scope.getInputNode(); var inputNode = $scope.getInputNode();
@ -138,6 +146,11 @@ weechat.directive('inputBar', function() {
// This function is for later cycling the list after we got it // This function is for later cycling the list after we got it
var cycleCompletionList = function (direction) { var cycleCompletionList = function (direction) {
// Don't do anything, the input has changed before we were able to complete the command
if ( commandCompletionInputChanged ) {
return;
}
// Check if the list has elements and we have not cycled to the end yet // Check if the list has elements and we have not cycled to the end yet
if ( !commandCompletionList || !commandCompletionList[0] ) { if ( !commandCompletionList || !commandCompletionList[0] ) {
return; return;
@ -162,12 +175,15 @@ weechat.directive('inputBar', function() {
// Cycle the list // Cycle the list
// First remove the word that's to be completed // First remove the word that's to be completed
var commandBeforeReplace = $scope.command.substring(0, commandCompletionPosition - commandCompletionBaseWord.length); var commandBeforeReplace = $scope.command.substring(0, commandCompletionPosition - commandCompletionBaseWord.length);
var commandAfterReplace = $scope.command.substring(commandCompletionPosition + commandCompletionBaseWord.length); var commandAfterReplace = $scope.command.substring(commandCompletionPosition, $scope.command.length);
var replacedWord = commandCompletionList[commandCompletionPositionInList]; var replacedWord = commandCompletionList[commandCompletionPositionInList];
$scope.command = commandBeforeReplace + replacedWord + commandAfterReplace; var suffix = commandCompletionAddSpace ? ' ' : '';
// Fill in the new command
$scope.command = commandBeforeReplace + replacedWord + suffix + commandAfterReplace;
// Set the cursor position // Set the cursor position
var newCursorPos = commandBeforeReplace.length + replacedWord.length; var newCursorPos = commandBeforeReplace.length + replacedWord.length + suffix.length;
setTimeout(function() { setTimeout(function() {
inputNode.focus(); inputNode.focus();
inputNode.setSelectionRange(newCursorPos, newCursorPos); inputNode.setSelectionRange(newCursorPos, newCursorPos);
@ -175,9 +191,9 @@ weechat.directive('inputBar', function() {
// Setup for the next cycle // Setup for the next cycle
commandCompletionPositionInList++; commandCompletionPositionInList++;
commandCompletionBaseWord = replacedWord; commandCompletionBaseWord = replacedWord + suffix;
previousInput = $scope.command + activeBuffer.id; previousInput = $scope.command + activeBuffer.id;
commandCompletionPosition = $scope.command.length; commandCompletionPosition = newCursorPos;
} }
// Check if we have requested this completion info before // Check if we have requested this completion info before
@ -190,6 +206,7 @@ weechat.directive('inputBar', function() {
// Save the list of completion object, we will only request is once // Save the list of completion object, we will only request is once
// and cycle through it as long as the input doesn't change // and cycle through it as long as the input doesn't change
commandCompletionList = completionObject.list; commandCompletionList = completionObject.list;
commandCompletionAddSpace = completionObject.add_space
commandCompletionBaseWord = completionObject.base_word; commandCompletionBaseWord = completionObject.base_word;
commandCompletionPosition = caretPos; commandCompletionPosition = caretPos;
commandCompletionPositionInList = 0; commandCompletionPositionInList = 0;
@ -197,6 +214,8 @@ weechat.directive('inputBar', function() {
//after we get the list we can continue with our first cycle //after we get the list we can continue with our first cycle
cycleCompletionList(direction); cycleCompletionList(direction);
}); });
} else { } else {
// Input hasn't changed so we should already have our completion list // Input hasn't changed so we should already have our completion list
cycleCompletionList(direction); cycleCompletionList(direction);

Loading…
Cancel
Save