提交 9b3d5294 编写于 作者: J Johannes Rieken

first cut of tab completion, #9286

上级 b7cfce2c
......@@ -33,6 +33,7 @@ import 'vs/editor/contrib/rename/browser/rename';
import 'vs/editor/contrib/smartSelect/common/smartSelect';
import 'vs/editor/contrib/smartSelect/common/jumpToBracket';
import 'vs/editor/contrib/snippet/common/snippet';
import 'vs/editor/contrib/snippet/common/tabCompletion';
import 'vs/editor/contrib/snippet/browser/snippet';
import 'vs/editor/contrib/suggest/browser/suggest';
import 'vs/editor/contrib/toggleTabFocusMode/common/toggleTabFocusMode';
......
......@@ -217,5 +217,31 @@ export class SnippetsRegistry {
}
public static getSnippetsStrict(model: IReadOnlyModel, position: IPosition): modes.ISuggestion[] {
const match = model.getLineContent(position.lineNumber).substr(0, position.column - 1).match(/[^\s]+$/);
if (!match) {
return [];
}
const prefix = match[0];
const result: modes.ISuggestion[] = [];
SnippetsRegistry._fillInSuggestion(this._defaultSnippets[model.getModeId()], prefix, result);
let snipppetsByMode = this._snippets[model.getModeId()];
if (snipppetsByMode) {
for (let path in snipppetsByMode) {
SnippetsRegistry._fillInSuggestion(snipppetsByMode[path], prefix, result);
}
}
return result;
}
private static _fillInSuggestion(suggestions: modes.ISuggestion[], prefix: string, bucket: modes.ISuggestion[]): void {
if (!suggestions) {
return;
}
for (const suggestion of suggestions) {
if (strings.endsWith(prefix, suggestion.label)) {
bucket.push(suggestion);
}
}
}
}
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import {KeyCode} from 'vs/base/common/keyCodes';
import {IKeybindingService} from 'vs/platform/keybinding/common/keybinding';
import {KeybindingsRegistry} from 'vs/platform/keybinding/common/keybindingsRegistry';
import {SnippetsRegistry} from 'vs/editor/common/modes/supports';
import {ISuggestion} from 'vs/editor/common/modes';
import {IDisposable} from 'vs/base/common/lifecycle';
import * as editor from 'vs/editor/common/editorCommon';
import {CommonEditorRegistry} from 'vs/editor/common/editorCommonExtensions';
import {CodeSnippet, ISnippetController, getSnippetController} from 'vs/editor/contrib/snippet/common/snippet';
class TabCompletionController implements editor.IEditorContribution {
static Id = 'editor.tabCompletionController';
static ContextKey = 'hasSnippetCompletions';
private _snippetController: ISnippetController;
private _cursorChangeSubscription: IDisposable;
private _currentCompletions: ISuggestion[] = [];
constructor(
editor: editor.ICommonCodeEditor,
@IKeybindingService keybindingService: IKeybindingService
) {
this._snippetController = getSnippetController(editor);
const hasSnippets = keybindingService.createKey(TabCompletionController.ContextKey, undefined);
this._cursorChangeSubscription = editor.onDidChangeCursorPosition(e => {
this._currentCompletions.length = 0;
this._currentCompletions = SnippetsRegistry.getSnippetsStrict(editor.getModel(), editor.getPosition());
hasSnippets.set(this._currentCompletions.length === 1); //todo@joh make it work with N
});
}
dispose(): void {
this._cursorChangeSubscription.dispose();
}
performSnippetCompletions(): void {
if (this._currentCompletions.length === 1) {
const suggestion = this._currentCompletions[0];
const snippet = new CodeSnippet(suggestion.codeSnippet);
this._snippetController.run(snippet, suggestion.label.length, 0);
} else {
// todo@joh - show suggest widget with proposals
}
}
getId(): string {
return TabCompletionController.Id;
}
}
CommonEditorRegistry.registerEditorContribution(TabCompletionController);
CommonEditorRegistry.registerEditorCommand(
'tabCompletion',
KeybindingsRegistry.WEIGHT.editorContrib(),
{ primary: KeyCode.Tab },
true,
TabCompletionController.ContextKey,
(accessor, editor) => {
const controller = <TabCompletionController>editor.getContribution(TabCompletionController.Id);
controller.performSnippetCompletions();
});
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册