From 45906d91e44bc4e62e917184933ec3e92dbacc26 Mon Sep 17 00:00:00 2001 From: Alex Dima Date: Wed, 9 Mar 2016 10:50:05 +0100 Subject: [PATCH] Add ability to change a document's EOL --- src/vs/vscode.d.ts | 21 ++++++++++++++ src/vs/workbench/api/node/extHost.api.impl.ts | 28 ++++++++++--------- src/vs/workbench/api/node/extHostEditors.ts | 22 +++++++++++---- src/vs/workbench/api/node/extHostTypes.ts | 5 ++++ .../workbench/api/node/mainThreadEditors.ts | 9 +++++- 5 files changed, 66 insertions(+), 19 deletions(-) diff --git a/src/vs/vscode.d.ts b/src/vs/vscode.d.ts index d302d8b3ba0..27710ee4178 100644 --- a/src/vs/vscode.d.ts +++ b/src/vs/vscode.d.ts @@ -796,6 +796,20 @@ declare namespace vscode { hide(): void; } + /** + * Represents an end of line character sequence in a [document](#Document). + */ + export enum EndOfLine { + /** + * The line feed `\n` character. + */ + LF = 1, + /** + * The carriage return line feed `\r\n` sequence. + */ + CRLF = 2 + } + /** * A complex edit that will be applied in one transaction on a TextEditor. * This holds a description of the edits and if the edits are valid (i.e. no overlapping regions, document was not changed in the meantime, etc.) @@ -828,6 +842,13 @@ declare namespace vscode { * @param location The range this operation should remove. */ delete(location: Range | Selection): void; + + /** + * Set the end of line sequence. + * + * @param endOfLine The new end of line for the [document](#Document). + */ + setEndOfLine(endOfLine:EndOfLine): void; } /** diff --git a/src/vs/workbench/api/node/extHost.api.impl.ts b/src/vs/workbench/api/node/extHost.api.impl.ts index f3c88fda85a..86d3064bd03 100644 --- a/src/vs/workbench/api/node/extHost.api.impl.ts +++ b/src/vs/workbench/api/node/extHost.api.impl.ts @@ -85,6 +85,7 @@ export class ExtHostAPIImplementation { IndentAction: typeof vscode.IndentAction; OverviewRulerLane: typeof vscode.OverviewRulerLane; TextEditorRevealType: typeof vscode.TextEditorRevealType; + EndOfLine: typeof vscode.EndOfLine; commands: typeof vscode.commands; window: typeof vscode.window; workspace: typeof vscode.workspace; @@ -103,8 +104,8 @@ export class ExtHostAPIImplementation { this.version = contextService.getConfiguration().env.version; this.Uri = URI; this.Location = extHostTypes.Location; - this.Diagnostic = extHostTypes.Diagnostic; - this.DiagnosticSeverity = extHostTypes.DiagnosticSeverity; + this.Diagnostic = extHostTypes.Diagnostic; + this.DiagnosticSeverity = extHostTypes.DiagnosticSeverity; this.EventEmitter = Emitter; this.Disposable = extHostTypes.Disposable; this.TextEdit = extHostTypes.TextEdit; @@ -114,22 +115,23 @@ export class ExtHostAPIImplementation { this.Selection = extHostTypes.Selection; this.CancellationTokenSource = CancellationTokenSource; this.Hover = extHostTypes.Hover; - this.SymbolKind = extHostTypes.SymbolKind; - this.SymbolInformation = extHostTypes.SymbolInformation; - this.DocumentHighlightKind = extHostTypes.DocumentHighlightKind; - this.DocumentHighlight = extHostTypes.DocumentHighlight; + this.SymbolKind = extHostTypes.SymbolKind; + this.SymbolInformation = extHostTypes.SymbolInformation; + this.DocumentHighlightKind = extHostTypes.DocumentHighlightKind; + this.DocumentHighlight = extHostTypes.DocumentHighlight; this.CodeLens = extHostTypes.CodeLens; this.ParameterInformation = extHostTypes.ParameterInformation; this.SignatureInformation = extHostTypes.SignatureInformation; this.SignatureHelp = extHostTypes.SignatureHelp; - this.CompletionItem = extHostTypes.CompletionItem; - this.CompletionItemKind = extHostTypes.CompletionItemKind; + this.CompletionItem = extHostTypes.CompletionItem; + this.CompletionItemKind = extHostTypes.CompletionItemKind; this.CompletionList = extHostTypes.CompletionList; - this.ViewColumn = extHostTypes.ViewColumn; - this.StatusBarAlignment = extHostTypes.StatusBarAlignment; - this.IndentAction = Modes.IndentAction; - this.OverviewRulerLane = EditorCommon.OverviewRulerLane; - this.TextEditorRevealType = TextEditorRevealType; + this.ViewColumn = extHostTypes.ViewColumn; + this.StatusBarAlignment = extHostTypes.StatusBarAlignment; + this.IndentAction = Modes.IndentAction; + this.OverviewRulerLane = EditorCommon.OverviewRulerLane; + this.TextEditorRevealType = TextEditorRevealType; + this.EndOfLine = extHostTypes.EndOfLine; errors.setUnexpectedErrorHandler((err) => { this._proxy.onUnexpectedExtHostError(errors.transformErrorForSerialization(err)); diff --git a/src/vs/workbench/api/node/extHostEditors.ts b/src/vs/workbench/api/node/extHostEditors.ts index 6a603b6d096..0a6a73b859a 100644 --- a/src/vs/workbench/api/node/extHostEditors.ts +++ b/src/vs/workbench/api/node/extHostEditors.ts @@ -10,7 +10,7 @@ import {IDisposable, disposeAll} from 'vs/base/common/lifecycle'; import {TPromise} from 'vs/base/common/winjs.base'; import {Remotable, IThreadService} from 'vs/platform/thread/common/thread'; import {ExtHostModelService, ExtHostDocumentData} from 'vs/workbench/api/node/extHostDocuments'; -import {Selection, Range, Position, EditorOptions} from './extHostTypes'; +import {Selection, Range, Position, EditorOptions, EndOfLine} from './extHostTypes'; import {ISingleEditOperation, ISelection, IRange, IEditor, EditorType, ICommonCodeEditor, ICommonDiffEditor, IDecorationRenderOptions, IRangeWithMessage} from 'vs/editor/common/editorCommon'; import {ICodeEditorService} from 'vs/editor/common/services/codeEditorService'; import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService'; @@ -196,22 +196,26 @@ export interface ITextEditOperation { export interface IEditData { documentVersionId: number; edits: ITextEditOperation[]; + setEndOfLine: EndOfLine; } export class TextEditorEdit { private _documentVersionId: number; private _collectedEdits: ITextEditOperation[]; + private _setEndOfLine: EndOfLine; constructor(document: vscode.TextDocument) { this._documentVersionId = document.version; this._collectedEdits = []; + this._setEndOfLine = 0; } finalize(): IEditData { return { documentVersionId: this._documentVersionId, - edits: this._collectedEdits + edits: this._collectedEdits, + setEndOfLine: this._setEndOfLine }; } @@ -260,6 +264,14 @@ export class TextEditorEdit { forceMoveMarkers: true }); } + + setEndOfLine(endOfLine:EndOfLine): void { + if (endOfLine !== EndOfLine.LF && endOfLine !== EndOfLine.CRLF) { + throw illegalArg('endOfLine'); + } + + this._setEndOfLine = endOfLine; + } } function readonly(name: string, alt?: string) { @@ -435,7 +447,7 @@ class ExtHostTextEditor implements vscode.TextEditor { }; }); - return this._proxy._tryApplyEdits(this._id, editData.documentVersionId, edits); + return this._proxy._tryApplyEdits(this._id, editData.documentVersionId, edits, editData.setEndOfLine); } // ---- util @@ -705,11 +717,11 @@ export class MainThreadEditors { return TPromise.as(null); } - _tryApplyEdits(id: string, modelVersionId: number, edits: ISingleEditOperation[]): TPromise { + _tryApplyEdits(id: string, modelVersionId: number, edits: ISingleEditOperation[], setEndOfLine:EndOfLine): TPromise { if (!this._textEditorsMap[id]) { return TPromise.wrapError('TextEditor disposed'); } - return TPromise.as(this._textEditorsMap[id].applyEdits(modelVersionId, edits)); + return TPromise.as(this._textEditorsMap[id].applyEdits(modelVersionId, edits, setEndOfLine)); } _registerTextEditorDecorationType(key: string, options: IDecorationRenderOptions): void { diff --git a/src/vs/workbench/api/node/extHostTypes.ts b/src/vs/workbench/api/node/extHostTypes.ts index 0a0f3abb9be..7bba90c4858 100644 --- a/src/vs/workbench/api/node/extHostTypes.ts +++ b/src/vs/workbench/api/node/extHostTypes.ts @@ -688,3 +688,8 @@ export enum StatusBarAlignment { Left = 1, Right = 2 } + +export enum EndOfLine { + LF = 1, + CRLF = 2 +} \ No newline at end of file diff --git a/src/vs/workbench/api/node/mainThreadEditors.ts b/src/vs/workbench/api/node/mainThreadEditors.ts index 3748f2d69cb..d0eb0a02b49 100644 --- a/src/vs/workbench/api/node/mainThreadEditors.ts +++ b/src/vs/workbench/api/node/mainThreadEditors.ts @@ -13,6 +13,7 @@ import {IDisposable, disposeAll} from 'vs/base/common/lifecycle'; import {RunOnceScheduler} from 'vs/base/common/async'; import {Range} from 'vs/editor/common/core/range'; import {Selection} from 'vs/editor/common/core/selection'; +import {EndOfLine} from 'vs/workbench/api/node/extHostTypes'; export interface ITextEditorConfigurationUpdate { tabSize?: number | string; @@ -262,7 +263,7 @@ export class MainThreadTextEditor { return editor.getControl() === this._codeEditor; } - public applyEdits(versionIdCheck:number, edits:EditorCommon.ISingleEditOperation[]): boolean { + public applyEdits(versionIdCheck:number, edits:EditorCommon.ISingleEditOperation[], setEndOfLine:EndOfLine): boolean { if (this._model.getVersionId() !== versionIdCheck) { console.warn('Model has changed in the meantime!'); // throw new Error('Model has changed in the meantime!'); @@ -271,6 +272,12 @@ export class MainThreadTextEditor { } if (this._codeEditor) { + if (setEndOfLine === EndOfLine.CRLF) { + this._model.setEOL(EditorCommon.EndOfLineSequence.CRLF); + } else if (setEndOfLine === EndOfLine.LF) { + this._model.setEOL(EditorCommon.EndOfLineSequence.LF); + } + let transformedEdits = edits.map((edit): EditorCommon.IIdentifiedSingleEditOperation => { return { identifier: null, -- GitLab