Only apply TS Dot Accept Suggestion if Previous Character is a valid identifier (#18063)

* Only apply TS Dot Accept Suggestion if previous character is a valid identifier char

Fixes #17825
Fixes #17770
Fixes #17584

**Bug**
When typing two or more `.` in a row, we end up unexpectedly accepting suggestions in TS files. This is caused by the custom keybinding that ts registers for `.`.

**Fix**
Only accept the suggestion on `.` if the previous character is a valid identifier character.

* Move title to nls
This commit is contained in:
Matt Bierner
2017-01-09 10:43:59 -08:00
committed by GitHub
parent 1fafbd089d
commit 41cb5ee5ae
3 changed files with 19 additions and 2 deletions
+5 -1
View File
@@ -257,7 +257,7 @@
}, },
"keybindings": { "keybindings": {
"key": ".", "key": ".",
"command": "^acceptSelectedSuggestion", "command": "typescript.tryAcceptSelectedSuggestionOnDot",
"when": "editorTextFocus && suggestWidgetVisible && editorLangId == 'typescript' && suggestionSupportsAcceptOnKey" "when": "editorTextFocus && suggestWidgetVisible && editorLangId == 'typescript' && suggestionSupportsAcceptOnKey"
}, },
"commands": [ "commands": [
@@ -268,6 +268,10 @@
{ {
"command": "javascript.reloadProjects", "command": "javascript.reloadProjects",
"title": "%javascript.reloadProjects.title%" "title": "%javascript.reloadProjects.title%"
},
{
"command": "typescript.tryAcceptSelectedSuggestionOnDot",
"title": "%typescript.tryAcceptSelectedSuggestionOnDot.title%"
} }
], ],
"breakpoints": [ "breakpoints": [
+1
View File
@@ -12,6 +12,7 @@
"typescript.tsserver.experimentalAutoBuild": "Enables experimental auto build. Requires 1.9 dev or 2.x tsserver version and a restart of VS Code after changing it.", "typescript.tsserver.experimentalAutoBuild": "Enables experimental auto build. Requires 1.9 dev or 2.x tsserver version and a restart of VS Code after changing it.",
"typescript.validate.enable": "Enable/disable TypeScript validation.", "typescript.validate.enable": "Enable/disable TypeScript validation.",
"typescript.format.enable": "Enable/disable default TypeScript formatter.", "typescript.format.enable": "Enable/disable default TypeScript formatter.",
"typescript.tryAcceptSelectedSuggestionOnDot": "Accept current suggestion when a dot is typed",
"javascript.format.enable": "Enable/disable default JavaScript formatter.", "javascript.format.enable": "Enable/disable default JavaScript formatter.",
"format.insertSpaceAfterCommaDelimiter": "Defines space handling after a comma delimiter.", "format.insertSpaceAfterCommaDelimiter": "Defines space handling after a comma delimiter.",
"format.insertSpaceAfterSemicolonInForStatements": " Defines space handling after a semicolon in a for statement.", "format.insertSpaceAfterSemicolonInForStatements": " Defines space handling after a semicolon in a for statement.",
+13 -1
View File
@@ -9,7 +9,7 @@
* ------------------------------------------------------------------------------------------ */ * ------------------------------------------------------------------------------------------ */
'use strict'; 'use strict';
import { env, languages, commands, workspace, window, Uri, ExtensionContext, Memento, IndentAction, Diagnostic, DiagnosticCollection, Range, DocumentFilter, Disposable } from 'vscode'; import { env, languages, commands, workspace, window, Uri, ExtensionContext, Memento, IndentAction, Diagnostic, DiagnosticCollection, Range, DocumentFilter, Disposable, Position } from 'vscode';
// This must be the first statement otherwise modules might got loaded with // This must be the first statement otherwise modules might got loaded with
// the wrong locale. // the wrong locale.
@@ -83,6 +83,18 @@ export function activate(context: ExtensionContext): void {
clientHost.reloadProjects(); clientHost.reloadProjects();
})); }));
context.subscriptions.push(commands.registerCommand('typescript.tryAcceptSelectedSuggestionOnDot', () => {
// When typing a dot, make sure the character before the dot is a valid indentifier character.
const editor = window.activeTextEditor;
if (editor && editor.selection && editor.selection.start.character >= 1) {
const preText = editor.document.getText(new Range(new Position(editor.selection.start.line, 0), new Position(editor.selection.start.line, editor.selection.start.character - 1)));
if (preText.match(/[a-z_$]\s*$/ig)) {
return commands.executeCommand('acceptSelectedSuggestion');
}
}
return commands.executeCommand('type', { text: '.' });
}));
window.onDidChangeActiveTextEditor(VersionStatus.showHideStatus, null, context.subscriptions); window.onDidChangeActiveTextEditor(VersionStatus.showHideStatus, null, context.subscriptions);
client.onReady().then(() => { client.onReady().then(() => {
context.subscriptions.push(ProjectStatus.create(client, context.subscriptions.push(ProjectStatus.create(client,