mirror of
https://github.com/microsoft/vscode.git
synced 2026-05-26 10:16:01 +01:00
match snippets strict when having a non empty selection, fixes #11912
This commit is contained in:
@@ -5,17 +5,17 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
import {KeyCode} from 'vs/base/common/keyCodes';
|
||||
import {RawContextKey, IContextKeyService, ContextKeyExpr} from 'vs/platform/contextkey/common/contextkey';
|
||||
import {KeybindingsRegistry} from 'vs/platform/keybinding/common/keybindingsRegistry';
|
||||
import {ISnippetsRegistry, Extensions, getNonWhitespacePrefix, ISnippet} from 'vs/editor/common/modes/snippetsRegistry';
|
||||
import {Registry} from 'vs/platform/platform';
|
||||
import {endsWith} from 'vs/base/common/strings';
|
||||
import {IDisposable} from 'vs/base/common/lifecycle';
|
||||
import { KeyCode } from 'vs/base/common/keyCodes';
|
||||
import { RawContextKey, IContextKeyService, ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
|
||||
import { KeybindingsRegistry } from 'vs/platform/keybinding/common/keybindingsRegistry';
|
||||
import { ISnippetsRegistry, Extensions, getNonWhitespacePrefix, ISnippet } from 'vs/editor/common/modes/snippetsRegistry';
|
||||
import { Registry } from 'vs/platform/platform';
|
||||
import { endsWith } from 'vs/base/common/strings';
|
||||
import { IDisposable } from 'vs/base/common/lifecycle';
|
||||
import * as editorCommon from 'vs/editor/common/editorCommon';
|
||||
import {CommonEditorRegistry, commonEditorContribution, EditorCommand} from 'vs/editor/common/editorCommonExtensions';
|
||||
import {CodeSnippet} from 'vs/editor/contrib/snippet/common/snippet';
|
||||
import {SnippetController} from 'vs/editor/contrib/snippet/common/snippetController';
|
||||
import { CommonEditorRegistry, commonEditorContribution, EditorCommand } from 'vs/editor/common/editorCommonExtensions';
|
||||
import { CodeSnippet } from 'vs/editor/contrib/snippet/common/snippet';
|
||||
import { SnippetController } from 'vs/editor/contrib/snippet/common/snippetController';
|
||||
|
||||
import EditorContextKeys = editorCommon.EditorContextKeys;
|
||||
|
||||
@@ -27,7 +27,7 @@ export class TabCompletionController implements editorCommon.IEditorContribution
|
||||
private static ID = 'editor.tabCompletionController';
|
||||
static ContextKey = new RawContextKey<boolean>('hasSnippetCompletions', undefined);
|
||||
|
||||
public static get(editor:editorCommon.ICommonCodeEditor): TabCompletionController {
|
||||
public static get(editor: editorCommon.ICommonCodeEditor): TabCompletionController {
|
||||
return editor.getContribution<TabCompletionController>(TabCompletionController.ID);
|
||||
}
|
||||
|
||||
@@ -44,19 +44,28 @@ export class TabCompletionController implements editorCommon.IEditorContribution
|
||||
this._cursorChangeSubscription = editor.onDidChangeCursorSelection(e => {
|
||||
|
||||
this._currentSnippets.length = 0;
|
||||
let selectFn: (snippet: ISnippet) => boolean;
|
||||
|
||||
const prefix = e.selection.isEmpty()
|
||||
? getNonWhitespacePrefix(editor.getModel(), editor.getPosition())
|
||||
: editor.getModel().getValueInRange(e.selection);
|
||||
if (e.selection.isEmpty()) {
|
||||
// empty selection -> real text (no whitespace) left of cursor
|
||||
const prefix = getNonWhitespacePrefix(editor.getModel(), editor.getPosition());
|
||||
selectFn = prefix && (snippet => endsWith(prefix, snippet.prefix));
|
||||
|
||||
if (prefix) {
|
||||
} else {
|
||||
// actual selection -> snippet must be a full match
|
||||
const selected = editor.getModel().getValueInRange(e.selection);
|
||||
selectFn = snippet => selected === snippet.prefix;
|
||||
}
|
||||
|
||||
if (selectFn) {
|
||||
snippetsRegistry.visitSnippets(editor.getModel().getModeId(), s => {
|
||||
if (endsWith(prefix, s.prefix)) {
|
||||
if (selectFn(s)) {
|
||||
this._currentSnippets.push(s);
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
hasSnippets.set(this._currentSnippets.length === 1); //todo@joh make it work with N
|
||||
});
|
||||
}
|
||||
@@ -70,7 +79,7 @@ export class TabCompletionController implements editorCommon.IEditorContribution
|
||||
const snippet = this._currentSnippets[0];
|
||||
const codeSnippet = CodeSnippet.fromTextmate(snippet.codeSnippet);
|
||||
this._snippetController.run(codeSnippet, snippet.prefix.length, 0);
|
||||
// } else {
|
||||
// } else {
|
||||
// todo@joh - show suggest widget with proposals
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user