提交 27647f07 编写于 作者: J Johannes Rieken

wip

上级 17ea7e51
/*---------------------------------------------------------------------------------------------
* 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 { getLeadingWhitespace } from 'vs/base/common/strings';
import { ICommonCodeEditor, IModel } from 'vs/editor/common/editorCommon';
import { TextmateSnippet } from '../common/snippetParser';
export class SnippetSession {
private readonly _editor: ICommonCodeEditor;
private readonly _model: IModel;
private readonly _snippets: TextmateSnippet[] = [];
constructor(editor: ICommonCodeEditor, snippet: TextmateSnippet) {
this._editor = editor;
this._model = editor.getModel();
for (const selection of editor.getSelections()) {
// for each selection get the leading 'reference' whitespace and
// adjust the snippet accordingly. this makes one snippet per selection/cursor
const line = this._model.getLineContent(selection.startLineNumber);
const leadingWhitespace = getLeadingWhitespace(line, 0, selection.startColumn - 1);
const newSnippet = snippet.withIndentation(whitespace => this._model.normalizeIndentation(leadingWhitespace + whitespace));
this._snippets.push(newSnippet);
// const offset = this._model.getOffsetAt(selection.getStartPosition());
// for (const placeholder of snippet.getPlaceholders()) {
// const pos = this._model.getPositionAt(offset + snippet.offset(placeholder));
// this._model.deltaDecorations
// }
}
}
}
......@@ -231,20 +231,16 @@ export class TextmateSnippet {
return pos;
}
placeholders(): Map<string, Placeholder[]> {
const map = new Map<string, Placeholder[]>();
getPlaceholders(): Placeholder[] {
const ret: Placeholder[] = [];
walk(this.marker, candidate => {
if (candidate instanceof Placeholder) {
let array = map.get(candidate.name);
if (!array) {
map.set(candidate.name, [candidate]);
} else {
array.push(candidate);
}
ret.push(candidate);
}
return true;
});
return map;
return ret;
}
get value() {
......
/*---------------------------------------------------------------------------------------------
* 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 { Selection } from 'vs/editor/common/core/selection';
import { SnippetSession } from 'vs/editor/contrib/snippet/browser/editorSnippets';
import { SnippetParser } from 'vs/editor/contrib/snippet/common/snippetParser';
import { ICommonCodeEditor } from 'vs/editor/common/editorCommon';
import { mockCodeEditor } from 'vs/editor/test/common/mocks/mockCodeEditor';
suite('Editor Contrib - Snippets', () => {
let editor: ICommonCodeEditor;
setup(() => {
editor = mockCodeEditor([
'function foo() {',
'\tconsole.log(a)',
'}'
], {});
});
teardown(() => {
editor.dispose();
});
test('snippets', () => {
editor.setSelections([
new Selection(1, 1, 1, 1),
new Selection(2, 2, 2, 2),
]);
new SnippetSession(editor, SnippetParser.parse('foo\n${1:bar}\nfoo'));
assert.ok(true);
});
});
......@@ -332,35 +332,21 @@ suite('SnippetParser', () => {
test('TextmateSnippet#placeholder', () => {
let snippet = SnippetParser.parse('te$1xt');
let placeholders = snippet.placeholders();
assert.equal(placeholders.size, 1);
let array = placeholders.get('1');
assert.equal(array.length, 1);
let placeholders = snippet.getPlaceholders();
assert.equal(placeholders.length, 1);
snippet = SnippetParser.parse('te$1xt$1');
placeholders = snippet.placeholders();
assert.equal(placeholders.size, 1);
array = placeholders.get('1');
assert.equal(array.length, 2);
placeholders = snippet.getPlaceholders();
assert.equal(placeholders.length, 2);
snippet = SnippetParser.parse('te$1xt$2');
placeholders = snippet.placeholders();
assert.equal(placeholders.size, 2);
array = placeholders.get('1');
assert.equal(array.length, 1);
array = placeholders.get('2');
assert.equal(array.length, 1);
placeholders = snippet.getPlaceholders();
assert.equal(placeholders.length, 2);
snippet = SnippetParser.parse('${1:bar${2:foo}bar}');
placeholders = snippet.placeholders();
assert.equal(placeholders.size, 2);
array = placeholders.get('1');
assert.equal(array.length, 1);
assert.equal(snippet.offset(array[0]), 0);
array = placeholders.get('2');
assert.equal(array.length, 1);
assert.equal(snippet.offset(array[0]), 3);
placeholders = snippet.getPlaceholders();
assert.equal(placeholders.length, 2);
});
test('TextmateSnippet#withIndentation', () => {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册