提交 7ec3165d 编写于 作者: M Martin Aeschlimann

fixes #5996 [snippets] prefix not matching forward slashes & fixes #4485...

fixes #5996 [snippets] prefix not matching forward slashes & fixes #4485 [snippets] Allow snippets where the prefix ends with =
上级 4ccc89a1
......@@ -5,6 +5,7 @@
'use strict';
import * as strings from 'vs/base/common/strings';
import * as objects from 'vs/base/common/objects';
import {TPromise} from 'vs/base/common/winjs.base';
import {IModel, IPosition} from 'vs/editor/common/editorCommon';
import * as modes from 'vs/editor/common/modes';
......@@ -183,32 +184,25 @@ export class SnippetsRegistry {
snippetsByMode[path] = snippets;
}
// the previous
private static getNonWhitespacePrefix(model: IModel, position: IPosition) {
let line = model.getLineContent(position.lineNumber);
let match = line.match(/[^\s]+$/);
if (match) {
return match[0];
}
return '';
}
public static getSnippets(model: IModel, position: IPosition): modes.ISuggestResult {
let word = model.getWordAtPosition(position);
let currentPrefix = word ? word.word.substring(0, position.column - word.startColumn) : '';
let currentWord = word ? word.word.substring(0, position.column - word.startColumn).toLowerCase() : '';
let currentFullWord = SnippetsRegistry.getNonWhitespacePrefix(model, position).toLowerCase();
let result : modes.ISuggestResult = {
currentWord: currentPrefix,
currentWord: currentWord,
suggestions: []
};
// to avoid that snippets are too prominent in the intellisense proposals:
// - force that the current prefix matches with the snippet prefix
// if there's no prfix, only show snippets at the beginning of the line, or after a whitespace
let filter = null;
if (currentPrefix.length === 0) {
if (position.column > 1) {
let previousCharacter = model.getValueInRange({ startLineNumber: position.lineNumber, startColumn: position.column - 1, endLineNumber: position.lineNumber, endColumn: position.column });
if (previousCharacter.trim().length !== 0) {
return result;
}
}
} else {
let lowerCasePrefix = currentPrefix.toLowerCase();
filter = (p: modes.ISuggestion) => {
return strings.startsWith(p.label.toLowerCase(), lowerCasePrefix);
};
}
let modeId = model.getMode().getId();
let snippets : modes.ISuggestion[]= [];
let snipppetsByMode = this._snippets[modeId];
......@@ -221,7 +215,25 @@ export class SnippetsRegistry {
if (defaultSnippets) {
snippets = snippets.concat(defaultSnippets);
}
result.suggestions = filter ? snippets.filter(filter) : snippets;
// to avoid that snippets are too prominent in the intellisense proposals:
// enforce that current word is matched or the position is after a whitespace
snippets.forEach(p => {
if (currentWord.length === 0 && currentFullWord.length === 0) {
// if there's no prefix, only show snippets at the beginning of the line, or after a whitespace
} else {
let label = p.label.toLowerCase();
// force that the current word or full word matches with the snippet prefix
if (currentWord.length > 0 && strings.startsWith(label, currentWord)) {
// ok
} else if (currentFullWord.length > currentWord.length && strings.startsWith(label, currentFullWord)) {
p = objects.clone(p);
p.overwriteBefore = currentFullWord.length;
} else {
return;
}
}
result.suggestions.push(p);
});
// if (result.suggestions.length > 0) {
// if (word) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册