Merge pull request #22303 from Microsoft/joh/eol

TextDocument#eol
This commit is contained in:
Johannes Rieken
2017-03-27 12:08:42 +02:00
committed by GitHub
17 changed files with 270 additions and 72 deletions

View File

@@ -8,7 +8,7 @@ import { ok } from 'vs/base/common/assert';
import { regExpLeadsToEndlessLoop } from 'vs/base/common/strings';
import { MirrorModel2 } from 'vs/editor/common/model/mirrorModel2';
import URI from 'vs/base/common/uri';
import { Range, Position } from 'vs/workbench/api/node/extHostTypes';
import { Range, Position, EndOfLine } from 'vs/workbench/api/node/extHostTypes';
import * as vscode from 'vscode';
import { getWordAtText, ensureValidWordDefinition } from 'vs/editor/common/model/wordHelper';
import { MainThreadDocumentsShape } from './extHost.protocol';
@@ -76,6 +76,7 @@ export class ExtHostDocumentData extends MirrorModel2 {
get isDirty() { return data._isDirty; },
save() { return data._save(); },
getText(range?) { return range ? data._getTextInRange(range) : data.getText(); },
get eol() { return data._eol === '\n' ? EndOfLine.LF : EndOfLine.CRLF; },
get lineCount() { return data._lines.length; },
lineAt(lineOrPos) { return data._lineAt(lineOrPos); },
offsetAt(pos) { return data._offsetAt(pos); },

View File

@@ -12,7 +12,7 @@ import { illegalState } from 'vs/base/common/errors';
import { TPromise } from 'vs/base/common/winjs.base';
import { MainThreadWorkspaceShape, ExtHostDocumentSaveParticipantShape } from 'vs/workbench/api/node/extHost.protocol';
import { TextEdit } from 'vs/workbench/api/node/extHostTypes';
import { fromRange, TextDocumentSaveReason } from 'vs/workbench/api/node/extHostTypeConverters';
import { fromRange, TextDocumentSaveReason, EndOfLine } from 'vs/workbench/api/node/extHostTypeConverters';
import { IResourceEdit } from 'vs/editor/common/services/bulkEdit';
import { ExtHostDocuments } from 'vs/workbench/api/node/extHostDocuments';
import { SaveReason } from 'vs/workbench/services/textfile/common/textfiles';
@@ -101,10 +101,10 @@ export class ExtHostDocumentSaveParticipant extends ExtHostDocumentSaveParticipa
private _deliverEventAsync(listener: Function, thisArg: any, stubEvent: vscode.TextDocumentWillSaveEvent): TPromise<any> {
const promises: TPromise<any | vscode.TextEdit[]>[] = [];
const promises: TPromise<vscode.TextEdit[]>[] = [];
const {document, reason} = stubEvent;
const {version} = document;
const { document, reason } = stubEvent;
const { version } = document;
const event = Object.freeze(<vscode.TextDocumentWillSaveEvent>{
document,
@@ -127,21 +127,23 @@ export class ExtHostDocumentSaveParticipant extends ExtHostDocumentSaveParticipa
// freeze promises after event call
Object.freeze(promises);
return new TPromise<any[]>((resolve, reject) => {
return new TPromise<vscode.TextEdit[][]>((resolve, reject) => {
// join on all listener promises, reject after timeout
const handle = setTimeout(() => reject(new Error('timeout')), this._thresholds.timeout);
return always(TPromise.join(promises), () => clearTimeout(handle)).then(resolve, reject);
}).then(values => {
const edits: IResourceEdit[] = [];
let edits: IResourceEdit[] = [];
for (const value of values) {
if (Array.isArray(value) && (<vscode.TextEdit[]>value).every(e => e instanceof TextEdit)) {
for (const {newText, range} of value) {
for (const { newText, newEol, range } of value) {
edits.push({
resource: <URI>document.uri,
range: fromRange(range),
newText
range: range && fromRange(range),
newText,
newEol: EndOfLine.from(newEol)
});
}
}

View File

@@ -8,7 +8,7 @@ import Severity from 'vs/base/common/severity';
import * as modes from 'vs/editor/common/modes';
import * as types from './extHostTypes';
import { Position as EditorPosition } from 'vs/platform/editor/common/editor';
import { IPosition, ISelection, IRange, IDecorationOptions, ISingleEditOperation } from 'vs/editor/common/editorCommon';
import { IPosition, ISelection, IRange, IDecorationOptions, EndOfLineSequence } from 'vs/editor/common/editorCommon';
import * as vscode from 'vscode';
import URI from 'vs/base/common/uri';
import { SaveReason } from 'vs/workbench/services/textfile/common/textfiles';
@@ -46,6 +46,9 @@ export function fromSelection(selection: SelectionLike): ISelection {
}
export function fromRange(range: RangeLike): IRange {
if (!range) {
return undefined;
}
let { start, end } = range;
return {
startLineNumber: start.line + 1,
@@ -56,6 +59,9 @@ export function fromRange(range: RangeLike): IRange {
}
export function toRange(range: IRange): types.Range {
if (!range) {
return undefined;
}
let { startLineNumber, startColumn, endLineNumber, endColumn } = range;
return new types.Range(startLineNumber - 1, startColumn - 1, endLineNumber - 1, endColumn - 1);
}
@@ -153,14 +159,17 @@ export function fromRangeOrRangeWithMessage(ranges: vscode.Range[] | vscode.Deco
export const TextEdit = {
from(edit: vscode.TextEdit): ISingleEditOperation {
return <ISingleEditOperation>{
from(edit: vscode.TextEdit): modes.TextEdit {
return <modes.TextEdit>{
text: edit.newText,
eol: EndOfLine.from(edit.newEol),
range: fromRange(edit.range)
};
},
to(edit: ISingleEditOperation): vscode.TextEdit {
return new types.TextEdit(toRange(edit.range), edit.text);
to(edit: modes.TextEdit): vscode.TextEdit {
let result = new types.TextEdit(toRange(edit.range), edit.text);
result.newEol = EndOfLine.to(edit.eol);
return result;
}
};
@@ -364,3 +373,26 @@ export namespace TextDocumentSaveReason {
}
}
}
export namespace EndOfLine {
export function from(eol: vscode.EndOfLine): EndOfLineSequence {
if (eol === types.EndOfLine.CRLF) {
return EndOfLineSequence.CRLF;
} else if (eol === types.EndOfLine.LF) {
return EndOfLineSequence.LF;
}
return undefined;
}
export function to(eol: EndOfLineSequence): vscode.EndOfLine {
if (eol === EndOfLineSequence.CRLF) {
return types.EndOfLine.CRLF;
} else if (eol === EndOfLineSequence.LF) {
return types.EndOfLine.LF;
}
return undefined;
}
}

View File

@@ -399,6 +399,11 @@ export class Selection extends Range {
}
}
export enum EndOfLine {
LF = 1,
CRLF = 2
}
export class TextEdit {
static isTextEdit(thing: any): thing is TextEdit {
@@ -424,16 +429,22 @@ export class TextEdit {
return TextEdit.replace(range, '');
}
protected _range: Range;
static setEndOfLine(eol: EndOfLine): TextEdit {
let ret = new TextEdit(undefined, undefined);
ret.newEol = eol;
return ret;
}
protected _range: Range;
protected _newText: string;
protected _newEol: EndOfLine;
get range(): Range {
return this._range;
}
set range(value: Range) {
if (!value) {
if (value && !Range.isRange(value)) {
throw illegalArgument('range');
}
this._range = value;
@@ -443,10 +454,24 @@ export class TextEdit {
return this._newText || '';
}
set newText(value) {
set newText(value: string) {
if (value && typeof value !== 'string') {
throw illegalArgument('newText');
}
this._newText = value;
}
get newEol(): EndOfLine {
return this._newEol;
}
set newEol(value: EndOfLine) {
if (value && typeof value !== 'number') {
throw illegalArgument('newEol');
}
this._newEol = value;
}
constructor(range: Range, newText: string) {
this.range = range;
this.newText = newText;
@@ -455,7 +480,8 @@ export class TextEdit {
toJSON(): any {
return {
range: this.range,
newText: this.newText
newText: this.newText,
newEol: this._newEol
};
}
}
@@ -905,11 +931,6 @@ export enum StatusBarAlignment {
Right = 2
}
export enum EndOfLine {
LF = 1,
CRLF = 2
}
export enum TextEditorLineNumbersStyle {
Off = 0,
On = 1,
@@ -1186,4 +1207,4 @@ export class ShellTask extends BaseTask {
}
this._options = value;
}
}
}

View File

@@ -10,7 +10,7 @@ import { relative } from 'path';
import { IThreadService } from 'vs/workbench/services/thread/common/threadService';
import { IResourceEdit } from 'vs/editor/common/services/bulkEdit';
import { TPromise } from 'vs/base/common/winjs.base';
import { fromRange } from 'vs/workbench/api/node/extHostTypeConverters';
import { fromRange, EndOfLine } from 'vs/workbench/api/node/extHostTypeConverters';
import { MainContext, MainThreadWorkspaceShape } from './extHost.protocol';
import * as vscode from 'vscode';
@@ -80,7 +80,8 @@ export class ExtHostWorkspace {
resourceEdits.push({
resource: <URI>uri,
newText: edit.newText,
range: fromRange(edit.range)
newEol: EndOfLine.from(edit.newEol),
range: edit.range && fromRange(edit.range)
});
}
}