提交 9e589be9 编写于 作者: A Alex Dima

Move SnippetsRegistry out of modesRegistry

上级 ebc76bdf
......@@ -5,8 +5,6 @@
'use strict';
import Modes = require('vs/editor/common/modes');
import Strings = require('vs/base/common/strings');
import EditorCommon = require('vs/editor/common/editorCommon');
import {Registry} from 'vs/platform/platform';
import {ILanguageExtensionPoint} from 'vs/editor/common/services/modeService';
import Event, {Emitter} from 'vs/base/common/event';
......@@ -122,81 +120,3 @@ export function registerLanguage(def:ILanguageExtensionPoint): void {
export function registerWorkerParticipant(modeId:string, moduleId:string, ctorName?:string): void {
mR.registerWorkerParticipant(modeId, moduleId, ctorName);
}
// TODO@Martin: find a better home for this code:
// TODO@Martin: modify suggestSupport to return a boolean if snippets should be presented or not
// and turn this into a real registry
var _defaultSnippets: { [modeId: string]: Modes.ISuggestion[] } = Object.create(null);
var _snippets: { [modeId: string]: { [path: string]: Modes.ISuggestion[] } } = Object.create(null);
export function registerDefaultSnippets(modeId: string, snippets: Modes.ISuggestion[]): void {
_defaultSnippets[modeId] = (_defaultSnippets[modeId] || []).concat(snippets);
}
export function registerSnippets(modeId: string, path: string, snippets: Modes.ISuggestion[]): void {
var snippetsByMode = _snippets[modeId];
if (!snippetsByMode) {
_snippets[modeId] = snippetsByMode = {};
}
snippetsByMode[path] = snippets;
}
export function getSnippets(model: EditorCommon.IModel, position: EditorCommon.IPosition): Modes.ISuggestResult {
var word = model.getWordAtPosition(position);
var currentPrefix = word ? word.word.substring(0, position.column - word.startColumn) : '';
var result : Modes.ISuggestResult = {
currentWord: currentPrefix,
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
var filter = null;
if (currentPrefix.length === 0) {
if (position.column > 1) {
var previousCharacter = model.getValueInRange({ startLineNumber: position.lineNumber, startColumn: position.column - 1, endLineNumber: position.lineNumber, endColumn: position.column });
if (previousCharacter.trim().length !== 0) {
return result;
}
}
} else {
var lowerCasePrefix = currentPrefix.toLowerCase();
filter = (p: Modes.ISuggestion) => {
return Strings.startsWith(p.label.toLowerCase(), lowerCasePrefix);
};
}
var modeId = model.getMode().getId();
var snippets : Modes.ISuggestion[]= [];
var snipppetsByMode = _snippets[modeId];
if (snipppetsByMode) {
for (var s in snipppetsByMode) {
snippets = snippets.concat(snipppetsByMode[s]);
}
}
var defaultSnippets = _defaultSnippets[modeId];
if (defaultSnippets) {
snippets = snippets.concat(defaultSnippets);
}
result.suggestions = filter ? snippets.filter(filter) : snippets;
// if (result.suggestions.length > 0) {
// if (word) {
// // Push also the current word as first suggestion, to avoid unexpected snippet acceptance on Enter.
// result.suggestions = result.suggestions.slice(0);
// result.suggestions.unshift({
// codeSnippet: word.word,
// label: word.word,
// type: 'text'
// });
// }
// result.incomplete = true;
// }
return result;
}
......@@ -7,6 +7,7 @@
import Strings = require('vs/base/common/strings');
import Modes = require('vs/editor/common/modes');
import {Arrays} from 'vs/editor/common/core/arrays';
import * as EditorCommon from 'vs/editor/common/editorCommon';
export class Token implements Modes.IToken {
public startIndex:number;
......@@ -142,3 +143,83 @@ export class FilteredLineContext implements Modes.ILineContext {
export function ignoreBracketsInToken(tokenType:string): boolean {
return /\b(comment|string|regex)\b/.test(tokenType);
}
// TODO@Martin: find a better home for this code:
// TODO@Martin: modify suggestSupport to return a boolean if snippets should be presented or not
// and turn this into a real registry
export class SnippetsRegistry {
private static _defaultSnippets: { [modeId: string]: Modes.ISuggestion[] } = Object.create(null);
private static _snippets: { [modeId: string]: { [path: string]: Modes.ISuggestion[] } } = Object.create(null);
public static registerDefaultSnippets(modeId: string, snippets: Modes.ISuggestion[]): void {
this._defaultSnippets[modeId] = (this._defaultSnippets[modeId] || []).concat(snippets);
}
public static registerSnippets(modeId: string, path: string, snippets: Modes.ISuggestion[]): void {
let snippetsByMode = this._snippets[modeId];
if (!snippetsByMode) {
this._snippets[modeId] = snippetsByMode = {};
}
snippetsByMode[path] = snippets;
}
public static getSnippets(model: EditorCommon.IModel, position: EditorCommon.IPosition): Modes.ISuggestResult {
let word = model.getWordAtPosition(position);
let currentPrefix = word ? word.word.substring(0, position.column - word.startColumn) : '';
let result : Modes.ISuggestResult = {
currentWord: currentPrefix,
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];
if (snipppetsByMode) {
for (let s in snipppetsByMode) {
snippets = snippets.concat(snipppetsByMode[s]);
}
}
let defaultSnippets = this._defaultSnippets[modeId];
if (defaultSnippets) {
snippets = snippets.concat(defaultSnippets);
}
result.suggestions = filter ? snippets.filter(filter) : snippets;
// if (result.suggestions.length > 0) {
// if (word) {
// // Push also the current word as first suggestion, to avoid unexpected snippet acceptance on Enter.
// result.suggestions = result.suggestions.slice(0);
// result.suggestions.unshift({
// codeSnippet: word.word,
// label: word.word,
// type: 'text'
// });
// }
// result.incomplete = true;
// }
return result;
}
}
......@@ -11,7 +11,7 @@ import {onUnexpectedError, illegalArgument} from 'vs/base/common/errors';
import {ISuggestSupport, ISuggestResult} from 'vs/editor/common/modes';
import LanguageFeatureRegistry from 'vs/editor/common/modes/languageFeatureRegistry';
import {CommonEditorRegistry} from 'vs/editor/common/editorCommonExtensions';
import {getSnippets} from 'vs/editor/common/modes/modesRegistry';
import {SnippetsRegistry} from 'vs/editor/common/modes/supports';
export var CONTEXT_SUGGEST_WIDGET_VISIBLE = 'suggestWidgetVisible';
export var CONTEXT_SUGGESTION_SUPPORTS_ACCEPT_ON_KEY = 'suggestionSupportsAcceptOnKey';
......@@ -78,7 +78,7 @@ export function suggest(model: IModel, position: IPosition, triggerCharacter: st
return sequence(factory).then(() => {
// add snippets to the first group
const snippets = getSnippets(model, position);
const snippets = SnippetsRegistry.getSnippets(model, position);
if (suggestions.length === 0) {
suggestions.push([snippets]);
} else {
......
......@@ -9,11 +9,11 @@ import {TPromise} from 'vs/base/common/winjs.base';
import Modes = require('vs/editor/common/modes');
import snippets = require('vs/editor/contrib/snippet/common/snippet');
import json = require('vs/base/common/json');
import modesExt = require('vs/editor/common/modes/modesRegistry');
import paths = require('vs/base/common/paths');
import {IModelService} from 'vs/editor/common/services/modelService';
import {IModeService} from 'vs/editor/common/services/modeService';
import {PluginsRegistry, IMessageCollector} from 'vs/platform/plugins/common/pluginsRegistry';
import {SnippetsRegistry} from 'vs/editor/common/modes/supports';
import pfs = require('vs/base/node/pfs');
......@@ -27,7 +27,7 @@ export function snippetUpdated(modeId: string, filePath: string): TPromise<void>
var errors: string[] = [];
var snippets = json.parse(fileContents.toString(), errors);
var adaptedSnippets = TMSnippetsAdaptor.adapt(snippets);
modesExt.registerSnippets(modeId, filePath, adaptedSnippets);
SnippetsRegistry.registerSnippets(modeId, filePath, adaptedSnippets);
});
}
......@@ -99,7 +99,7 @@ export class MainProcessTextMateSnippet {
var errors: string[] = [];
var snippets = json.parse(fileContents.toString(), errors);
var adaptedSnippets = TMSnippetsAdaptor.adapt(snippets);
modesExt.registerDefaultSnippets(modeId, adaptedSnippets);
SnippetsRegistry.registerDefaultSnippets(modeId, adaptedSnippets);
});
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册