提交 6bbff65c 编写于 作者: J Johannes Rieken

support insert none snippet text

上级 ead17dea
......@@ -5,6 +5,7 @@
'use strict';
import { Range } from 'vs/editor/common/core/range';
import { IPosition } from 'vs/editor/common/core/position';
/**
* A selection in the editor.
......@@ -140,6 +141,13 @@ export class Selection extends Range {
// ----
/**
* Create a `Selection` from one or two positions
*/
public static fromPositions(start: IPosition, end: IPosition = start): Selection {
return new Selection(start.lineNumber, start.column, end.lineNumber, end.column);
}
/**
* Create a `Selection` from an `ISelection`.
*/
......@@ -192,4 +200,4 @@ export class Selection extends Range {
return new Selection(endLineNumber, endColumn, startLineNumber, startColumn);
}
}
\ No newline at end of file
}
......@@ -146,6 +146,10 @@ class OneSnippet {
}
}
get hasPlaceholder() {
return this._snippet.getPlaceholders().length > 0;
}
get range() {
return this._snippetDecoration !== undefined && this._editor.getModel().getDecorationRange(this._snippetDecoration);
}
......@@ -204,6 +208,7 @@ export class SnippetSession {
}
insert(): void {
let delta = 0;
let edits: IIdentifiedSingleEditOperation[] = [];
let model = this._editor.getModel();
......@@ -231,7 +236,13 @@ export class SnippetSession {
}
// make insert edit and start with first selections
const newSelections = model.pushEditOperations(this._editor.getSelections(), edits, () => this._move(true));
const newSelections = model.pushEditOperations(this._editor.getSelections(), edits, undoEdits => {
if (this._snippets[0].hasPlaceholder) {
return this._move(true);
} else {
return undoEdits.map(edit => Selection.fromPositions(edit.range.getEndPosition()));
}
});
this._editor.setSelections(newSelections);
}
......@@ -262,6 +273,10 @@ export class SnippetSession {
return this._snippets[0].isAtFinalPlaceholder;
}
get hasPlaceholder() {
return this._snippets[0].hasPlaceholder;
}
validateSelections(): boolean {
const selections = this._editor.getSelections();
if (selections.length < this._snippets.length) {
......
......@@ -61,8 +61,16 @@ export class SnippetController2 {
private _updateState(): void {
if (!this._snippet) {
// canceled in the meanwhile
return;
}
if (!this._snippet.hasPlaceholder) {
// don't listen for selection changes and don't
// update context keys when the snippet is plain text
return;
}
if (this._snippet.isAtFinalPlaceholder || !this._snippet.validateSelections()) {
return this.cancel();
}
......@@ -74,9 +82,9 @@ export class SnippetController2 {
cancel(): void {
if (this._snippet) {
this._inSnippet.reset();
this._hasPrevTabstop.reset();
this._hasNextTabstop.reset();
this._inSnippet.reset();
dispose(this._snippetListener);
dispose(this._snippet);
this._snippet = undefined;
......
......@@ -99,6 +99,13 @@ suite('SnippetSession', function () {
);
});
test('snippets, just text', function () {
const session = new SnippetSession(editor, 'foobar');
session.insert();
assert.equal(model.getValue(), 'foobarfunction foo() {\n foobarconsole.log(a);\n}');
assertSelections(editor, new Selection(1, 7, 1, 7), new Selection(2, 11, 2, 11));
});
test('snippets, selections and new text with newlines', () => {
const session = new SnippetSession(editor, 'foo\n\t${1:bar}\n$0');
......
......@@ -116,6 +116,15 @@ suite('SnippetController2', function () {
assertContextKeys(contextKeys, false, false, false);
});
test('insert, insert plain text -> no snippet mode', function () {
const ctrl = new SnippetController2(editor, contextKeys);
ctrl.insert('foobar');
assertContextKeys(contextKeys, false, false, false);
assertSelections(editor, new Selection(1, 7, 1, 7), new Selection(2, 11, 2, 11));
});
test('insert, delete snippet text', function () {
const ctrl = new SnippetController2(editor, contextKeys);
......
......@@ -726,6 +726,10 @@ declare module monaco {
* Create a new selection with a different `selectionStartLineNumber` and `selectionStartColumn`.
*/
setStartPosition(startLineNumber: number, startColumn: number): Selection;
/**
* Create a `Selection` from one or two positions
*/
static fromPositions(start: IPosition, end?: IPosition): Selection;
/**
* Create a `Selection` from an `ISelection`.
*/
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册