提交 1e68d940 编写于 作者: J Johannes Rieken

smarter SnippetFile#select, #13182

上级 376641be
......@@ -11,6 +11,8 @@ import { Snippet } from 'vs/workbench/parts/snippets/electron-browser/snippets.c
import { IExtensionDescription } from 'vs/platform/extensions/common/extensions';
import { localize } from 'vs/nls';
import { readFile } from 'vs/base/node/pfs';
import { endsWith } from 'vs/base/common/strings';
import { basename } from 'path';
interface JsonSerializedSnippet {
body: string;
......@@ -41,6 +43,22 @@ export class SnippetFile {
}
select(selector: string, bucket: Snippet[]): void {
if (endsWith(this.filepath, '.json')) {
this._filepathSelect(selector, bucket);
} else {
this._scopeSelect(selector, bucket);
}
}
private _filepathSelect(selector: string, bucket: Snippet[]): void {
// for `fooLang.json` files all snippets are accepted
if (selector === basename(this.filepath, '.json')) {
bucket.push(...this.data);
}
}
private _scopeSelect(selector: string, bucket: Snippet[]): void {
// for `my.code-snippets` files we need to look at each snippet
for (const snippet of this.data) {
const len = snippet.scopes.length;
if (len === 0) {
......@@ -60,7 +78,7 @@ export class SnippetFile {
let idx = selector.lastIndexOf('.');
if (idx >= 0) {
this.select(selector.substring(0, idx), bucket);
this._scopeSelect(selector.substring(0, idx), bucket);
}
}
......
......@@ -19,12 +19,12 @@ suite('Snippets', function () {
}
test('SnippetFile#select', function () {
let file = new TestSnippetFile('somepath/foo.json', []);
let file = new TestSnippetFile('somepath/foo.code-snippets', []);
let bucket: Snippet[] = [];
file.select('', bucket);
assert.equal(bucket.length, 0);
file = new TestSnippetFile('somepath/foo.json', [
file = new TestSnippetFile('somepath/foo.code-snippets', [
new Snippet(['foo'], 'FooSnippet1', 'foo', '', 'snippet', 'test'),
new Snippet(['foo'], 'FooSnippet2', 'foo', '', 'snippet', 'test'),
new Snippet(['bar'], 'BarSnippet1', 'foo', '', 'snippet', 'test'),
......@@ -56,7 +56,7 @@ suite('Snippets', function () {
test('SnippetFile#select - any scope', function () {
let file = new TestSnippetFile('somepath/foo.json', [
let file = new TestSnippetFile('somepath/foo.code-snippets', [
new Snippet([], 'AnySnippet1', 'foo', '', 'snippet', 'test'),
new Snippet(['foo'], 'FooSnippet1', 'foo', '', 'snippet', 'test'),
]);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册