提交 895bdd44 编写于 作者: J Johannes Rieken

add Snippet#scopes and SnippetFile#select, #13182

上级 d95d5e1f
......@@ -81,6 +81,7 @@ class InsertSnippetAction extends EditorAction {
undefined,
undefined,
undefined,
undefined,
snippet,
undefined
));
......
......@@ -40,6 +40,7 @@ export class Snippet {
private _isBogous: boolean;
constructor(
readonly scopes: string[],
readonly name: string,
readonly prefix: string,
readonly description: string,
......
......@@ -9,9 +9,13 @@ import { readFile } from 'vs/base/node/pfs';
import { parse as jsonParse } from 'vs/base/common/json';
import { forEach } from 'vs/base/common/collections';
import { Snippet } from 'vs/workbench/parts/snippets/electron-browser/snippets.contribution';
import { endsWith } from 'vs/base/common/strings';
import { basename } from 'path';
import { isFalsyOrEmpty } from 'vs/base/common/arrays';
interface JsonSerializedSnippet {
body: string;
scope: string;
prefix: string | string[];
description: string;
}
......@@ -26,13 +30,35 @@ interface JsonSerializedSnippets {
export class SnippetFile {
private constructor(
constructor(
readonly filepath: string,
readonly data: Snippet[]
) {
//
}
select(selector: string, bucket: Snippet[]): void {
for (const snippet of this.data) {
if (isFalsyOrEmpty(snippet.scopes)) {
// always accept
bucket.push(snippet);
} else {
// match
for (const scope of snippet.scopes) {
if (scope === selector) {
bucket.push(snippet);
break; // match only once!
}
}
}
}
let idx = selector.lastIndexOf('.');
if (idx >= 0) {
this.select(selector.substring(0, idx), bucket);
}
}
static fromFile(filepath: string, source: string, isFromExtension?: boolean): Promise<SnippetFile> {
return Promise.resolve(readFile(filepath)).then(value => {
const data = <JsonSerializedSnippets>jsonParse(value.toString());
......@@ -41,11 +67,11 @@ export class SnippetFile {
forEach(data, entry => {
const { key: name, value: scopeOrTemplate } = entry;
if (isJsonSerilziedSnippet(scopeOrTemplate)) {
SnippetFile._parseSnippet(name, scopeOrTemplate, source, isFromExtension, snippets);
SnippetFile._parseSnippet(filepath, name, scopeOrTemplate, source, isFromExtension, snippets);
} else {
forEach(scopeOrTemplate, entry => {
const { key: name, value: template } = entry;
SnippetFile._parseSnippet(name, template, source, isFromExtension, snippets);
SnippetFile._parseSnippet(filepath, name, template, source, isFromExtension, snippets);
});
}
});
......@@ -54,7 +80,7 @@ export class SnippetFile {
});
}
private static _parseSnippet(name: string, snippet: JsonSerializedSnippet, source: string, isFromExtension: boolean, bucket: Snippet[]): void {
private static _parseSnippet(filepath: string, name: string, snippet: JsonSerializedSnippet, source: string, isFromExtension: boolean, bucket: Snippet[]): void {
let { prefix, body, description } = snippet;
......@@ -66,7 +92,17 @@ export class SnippetFile {
return;
}
let scopes: string[];
if (endsWith(filepath, '.json')) {
scopes = [basename(filepath, '.json')];
} else if (typeof snippet.scope === 'string') {
scopes = snippet.scope.split(',');
} else {
scopes = [];
}
bucket.push(new Snippet(
scopes,
name,
prefix,
description,
......
/*---------------------------------------------------------------------------------------------
* 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 * as assert from 'assert';
import { Snippet } from 'vs/workbench/parts/snippets/electron-browser/snippets.contribution';
import { SnippetFile } from 'vs/workbench/parts/snippets/electron-browser/snippetsFile';
suite('Snippets', function () {
test('SnippetFile#select', function () {
let file = new SnippetFile('somepath/foo.json', []);
let bucket: Snippet[] = [];
file.select('', bucket);
assert.equal(bucket.length, 0);
file = new SnippetFile('somepath/foo.json', [
new Snippet(['foo'], 'FooSnippet1', 'foo', '', 'snippet', 'test'),
new Snippet(['foo'], 'FooSnippet2', 'foo', '', 'snippet', 'test'),
new Snippet(['bar'], 'BarSnippet1', 'foo', '', 'snippet', 'test'),
new Snippet(['bar.comment'], 'BarSnippet2', 'foo', '', 'snippet', 'test'),
new Snippet(['bar.strings'], 'BarSnippet2', 'foo', '', 'snippet', 'test'),
new Snippet(['bazz', 'bazz'], 'BazzSnippet1', 'foo', '', 'snippet', 'test'),
]);
bucket = [];
file.select('foo', bucket);
assert.equal(bucket.length, 2);
bucket = [];
file.select('fo', bucket);
assert.equal(bucket.length, 0);
bucket = [];
file.select('bar', bucket);
assert.equal(bucket.length, 1);
bucket = [];
file.select('bar.comment', bucket);
assert.equal(bucket.length, 2);
bucket = [];
file.select('bazz', bucket);
assert.equal(bucket.length, 1);
});
test('SnippetFile#select - any scope', function () {
let file = new SnippetFile('somepath/foo.json', [
new Snippet([], 'AnySnippet1', 'foo', '', 'snippet', 'test'),
new Snippet(['foo'], 'FooSnippet1', 'foo', '', 'snippet', 'test'),
]);
let bucket: Snippet[] = [];
file.select('foo', bucket);
assert.equal(bucket.length, 2);
});
});
......@@ -45,7 +45,7 @@ suite('SnippetRewrite', function () {
});
test('lazy bogous variable rewrite', function () {
const snippet = new Snippet('foo', 'prefix', 'desc', 'This is ${bogous} because it is a ${var}', 'source');
const snippet = new Snippet(['fooLang'], 'foo', 'prefix', 'desc', 'This is ${bogous} because it is a ${var}', 'source');
assert.equal(snippet.body, 'This is ${bogous} because it is a ${var}');
assert.equal(snippet.codeSnippet, 'This is ${1:bogous} because it is a ${2:var}');
assert.equal(snippet.isBogous, true);
......
......@@ -40,12 +40,14 @@ suite('SnippetsService', function () {
setup(function () {
modeService = new ModeServiceImpl();
snippetService = new SimpleSnippetService([new Snippet(
['fooLang'],
'barTest',
'bar',
'',
'barCodeSnippet',
''
), new Snippet(
['fooLang'],
'bazzTest',
'bazz',
'',
......@@ -81,6 +83,7 @@ suite('SnippetsService', function () {
test('Cannot use "<?php" as user snippet prefix anymore, #26275', function () {
snippetService = new SimpleSnippetService([new Snippet(
['fooLang'],
'',
'<?php',
'',
......@@ -113,6 +116,7 @@ suite('SnippetsService', function () {
test('No user snippets in suggestions, when inside the code, #30508', function () {
snippetService = new SimpleSnippetService([new Snippet(
['fooLang'],
'',
'foo',
'',
......@@ -133,6 +137,7 @@ suite('SnippetsService', function () {
test('SnippetSuggest - ensure extension snippets come last ', function () {
snippetService = new SimpleSnippetService([new Snippet(
['fooLang'],
'second',
'second',
'',
......@@ -140,6 +145,7 @@ suite('SnippetsService', function () {
'',
true
), new Snippet(
['fooLang'],
'first',
'first',
'',
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册