提交 b6104acb 编写于 作者: J Johannes Rieken

make sure user snippet are sorted before extension snippets, fixes #22933

上级 4cb14c4a
......@@ -84,14 +84,14 @@ export class MainProcessTextMateSnippet implements IWorkbenchContribution {
}
}
export function readAndRegisterSnippets(snippetService: ISnippetsService, languageIdentifier: LanguageIdentifier, filePath: string, ownerName: string): TPromise<void> {
export function readAndRegisterSnippets(snippetService: ISnippetsService, languageIdentifier: LanguageIdentifier, filePath: string, extensionName?: string): TPromise<void> {
return readFile(filePath).then(fileContents => {
let snippets = parseSnippetFile(fileContents.toString(), ownerName);
let snippets = parseSnippetFile(fileContents.toString(), extensionName);
snippetService.registerSnippets(languageIdentifier.id, snippets, filePath);
});
}
function parseSnippetFile(snippetFileContent: string, owner: string): ISnippet[] {
function parseSnippetFile(snippetFileContent: string, extensionName?: string): ISnippet[] {
let snippetsObj = parse(snippetFileContent);
if (!snippetsObj || typeof snippetsObj !== 'object') {
return [];
......@@ -111,7 +111,7 @@ function parseSnippetFile(snippetFileContent: string, owner: string): ISnippet[]
if (typeof prefix === 'string' && typeof bodyStringOrArray === 'string') {
result.push({
name,
owner,
extensionName,
prefix,
description: snippet['description'] || name,
codeSnippet: bodyStringOrArray
......
......@@ -82,7 +82,7 @@ class InsertSnippetAction extends EditorAction {
codeSnippet: snippet,
description: undefined,
name: undefined,
owner: undefined,
extensionName: undefined,
prefix: undefined
});
}
......
......@@ -26,10 +26,10 @@ export interface ISnippetsService {
export interface ISnippet {
name: string;
owner: string;
prefix: string;
description: string;
codeSnippet: string;
extensionName?: string;
}
interface ISnippetSuggestion extends ISuggestion {
......@@ -40,6 +40,8 @@ class SnippetsService implements ISnippetsService {
_serviceBrand: any;
private static _defaultDetail = localize('detail.userSnippet', "User Snippet");
private _snippets = new Map<LanguageId, Map<string, ISnippet[]>>();
constructor(
......@@ -119,7 +121,7 @@ class SnippetsService implements ISnippetsService {
type: 'snippet',
label: s.prefix,
get disambiguateLabel() { return localize('snippetSuggest.longLabel', "{0}, {1}", s.prefix, s.name); },
detail: s.owner,
detail: s.extensionName || SnippetsService._defaultDetail,
documentation: s.description,
insertText: s.codeSnippet,
noAutoAccept: true,
......@@ -141,12 +143,26 @@ class SnippetsService implements ISnippetsService {
lastSuggestion = suggestion;
}
result.sort(SnippetsService._compareSuggestionsByDetail);
return result;
}
private static _compareSuggestionsByLabel(a: ISuggestion, b: ISuggestion): number {
return strings.compare(a.label, b.label);
}
private static _compareSuggestionsByDetail(a: ISuggestion, b: ISuggestion): number {
if (a.detail === b.detail) {
return 0;
} else if (a.detail === SnippetsService._defaultDetail) {
return -1;
} else if (b.detail === SnippetsService._defaultDetail) {
return 1;
} else {
return strings.compare(a.detail, b.detail);
}
}
}
registerSingleton(ISnippetsService, SnippetsService);
......
......@@ -5,7 +5,6 @@
'use strict';
import { localize } from 'vs/nls';
import workbenchExt = require('vs/workbench/common/contributions');
import paths = require('vs/base/common/paths');
import async = require('vs/base/common/async');
......@@ -77,7 +76,7 @@ export class SnippetsTracker implements workbenchExt.IWorkbenchContribution {
var snippetPath = paths.join(this.snippetFolder, snippetFile);
let languageIdentifier = this.modeService.getLanguageIdentifier(modeId);
if (languageIdentifier) {
return readAndRegisterSnippets(this.snippetService, languageIdentifier, snippetPath, localize('userSnippet', "User Snippet"));
return readAndRegisterSnippets(this.snippetService, languageIdentifier, snippetPath);
}
return undefined;
}));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册