mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-26 03:29:00 +01:00
add EndOfLineEdit and allow it to be returned from onWillSave
This commit is contained in:
@@ -486,6 +486,7 @@ export function createApiFactory(initData: IInitData, threadService: IThreadServ
|
||||
SymbolKind: extHostTypes.SymbolKind,
|
||||
TextDocumentSaveReason: extHostTypes.TextDocumentSaveReason,
|
||||
TextEdit: extHostTypes.TextEdit,
|
||||
EndOfLineEdit: extHostTypes.EndOfLineEdit,
|
||||
TextEditorCursorStyle: EditorCommon.TextEditorCursorStyle,
|
||||
TextEditorLineNumbersStyle: extHostTypes.TextEditorLineNumbersStyle,
|
||||
TextEditorRevealType: extHostTypes.TextEditorRevealType,
|
||||
|
||||
@@ -26,7 +26,7 @@ import { IWorkspace } from 'vs/platform/workspace/common/workspace';
|
||||
|
||||
import * as editorCommon from 'vs/editor/common/editorCommon';
|
||||
import * as modes from 'vs/editor/common/modes';
|
||||
import { IResourceTextEdit } from 'vs/editor/common/services/bulkEdit';
|
||||
import { IResourceEdit } from 'vs/editor/common/services/bulkEdit';
|
||||
import { ITextSource } from 'vs/editor/common/model/textSource';
|
||||
|
||||
import { ConfigurationTarget } from 'vs/workbench/services/configuration/common/configurationEditing';
|
||||
@@ -234,7 +234,7 @@ export abstract class MainThreadWorkspaceShape {
|
||||
$startSearch(include: string, exclude: string, maxResults: number, requestId: number): Thenable<URI[]> { throw ni(); }
|
||||
$cancelSearch(requestId: number): Thenable<boolean> { throw ni(); }
|
||||
$saveAll(includeUntitled?: boolean): Thenable<boolean> { throw ni(); }
|
||||
$applyWorkspaceEdit(edits: IResourceTextEdit[]): TPromise<boolean> { throw ni(); }
|
||||
$applyWorkspaceEdit(edits: IResourceEdit[]): TPromise<boolean> { throw ni(); }
|
||||
}
|
||||
|
||||
export abstract class MainProcessExtensionServiceShape {
|
||||
|
||||
@@ -11,9 +11,9 @@ import { sequence, always } from 'vs/base/common/async';
|
||||
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 { IResourceTextEdit } from 'vs/editor/common/services/bulkEdit';
|
||||
import { TextEdit, EndOfLineEdit } from 'vs/workbench/api/node/extHostTypes';
|
||||
import { fromRange, TextDocumentSaveReason, fromEOL } 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';
|
||||
import * as vscode from 'vscode';
|
||||
@@ -101,7 +101,7 @@ 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[] | vscode.EndOfLineEdit>[] = [];
|
||||
|
||||
const { document, reason } = stubEvent;
|
||||
const { version } = document;
|
||||
@@ -127,16 +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[] | vscode.EndOfLineEdit)[]>((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: IResourceTextEdit[] = [];
|
||||
let edits: IResourceEdit[] = [];
|
||||
|
||||
for (const value of values) {
|
||||
if (Array.isArray(value) && (<vscode.TextEdit[]>value).every(e => e instanceof TextEdit)) {
|
||||
|
||||
if (value instanceof EndOfLineEdit) {
|
||||
edits.push({
|
||||
resource: <URI>document.uri,
|
||||
eol: fromEOL(value.newEol)
|
||||
});
|
||||
} else if (Array.isArray(value) && (<vscode.TextEdit[]>value).every(e => e instanceof TextEdit)) {
|
||||
for (const { newText, range } of value) {
|
||||
edits.push({
|
||||
resource: <URI>document.uri,
|
||||
|
||||
@@ -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, ISingleEditOperation, EndOfLineSequence } from 'vs/editor/common/editorCommon';
|
||||
import { IWorkspaceSymbol } from 'vs/workbench/parts/search/common/search';
|
||||
import * as vscode from 'vscode';
|
||||
import URI from 'vs/base/common/uri';
|
||||
@@ -30,14 +30,14 @@ export interface SelectionLike extends RangeLike {
|
||||
}
|
||||
|
||||
export function toSelection(selection: ISelection): types.Selection {
|
||||
let {selectionStartLineNumber, selectionStartColumn, positionLineNumber, positionColumn} = selection;
|
||||
let { selectionStartLineNumber, selectionStartColumn, positionLineNumber, positionColumn } = selection;
|
||||
let start = new types.Position(selectionStartLineNumber - 1, selectionStartColumn - 1);
|
||||
let end = new types.Position(positionLineNumber - 1, positionColumn - 1);
|
||||
return new types.Selection(start, end);
|
||||
}
|
||||
|
||||
export function fromSelection(selection: SelectionLike): ISelection {
|
||||
let {anchor, active} = selection;
|
||||
let { anchor, active } = selection;
|
||||
return {
|
||||
selectionStartLineNumber: anchor.line + 1,
|
||||
selectionStartColumn: anchor.character + 1,
|
||||
@@ -47,7 +47,7 @@ export function fromSelection(selection: SelectionLike): ISelection {
|
||||
}
|
||||
|
||||
export function fromRange(range: RangeLike): IRange {
|
||||
let {start, end} = range;
|
||||
let { start, end } = range;
|
||||
return {
|
||||
startLineNumber: start.line + 1,
|
||||
startColumn: start.character + 1,
|
||||
@@ -57,7 +57,7 @@ export function fromRange(range: RangeLike): IRange {
|
||||
}
|
||||
|
||||
export function toRange(range: IRange): types.Range {
|
||||
let {startLineNumber, startColumn, endLineNumber, endColumn} = range;
|
||||
let { startLineNumber, startColumn, endLineNumber, endColumn } = range;
|
||||
return new types.Range(startLineNumber - 1, startColumn - 1, endLineNumber - 1, endColumn - 1);
|
||||
}
|
||||
|
||||
@@ -345,3 +345,14 @@ export namespace TextDocumentSaveReason {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export function fromEOL(eol: vscode.EndOfLine): EndOfLineSequence {
|
||||
if (eol === types.EndOfLine.CRLF) {
|
||||
return EndOfLineSequence.CRLF;
|
||||
} else if (eol === types.EndOfLine.LF) {
|
||||
return EndOfLineSequence.LF;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
|
||||
@@ -66,7 +66,7 @@ export class Position {
|
||||
if (other instanceof Position) {
|
||||
return true;
|
||||
}
|
||||
let {line, character} = <Position>other;
|
||||
let { line, character } = <Position>other;
|
||||
if (typeof line === 'number' && typeof character === 'number') {
|
||||
return true;
|
||||
}
|
||||
@@ -399,6 +399,20 @@ export class Selection extends Range {
|
||||
}
|
||||
}
|
||||
|
||||
export enum EndOfLine {
|
||||
LF = 1,
|
||||
CRLF = 2
|
||||
}
|
||||
|
||||
export class EndOfLineEdit {
|
||||
|
||||
static readonly LF: EndOfLineEdit = Object.freeze({ newEol: EndOfLine.LF });
|
||||
|
||||
static readonly CRLF: EndOfLineEdit = Object.freeze({ newEol: EndOfLine.CRLF });
|
||||
|
||||
newEol: EndOfLine;
|
||||
}
|
||||
|
||||
export class TextEdit {
|
||||
|
||||
static isTextEdit(thing: any): thing is TextEdit {
|
||||
@@ -900,11 +914,6 @@ export enum StatusBarAlignment {
|
||||
Right = 2
|
||||
}
|
||||
|
||||
export enum EndOfLine {
|
||||
LF = 1,
|
||||
CRLF = 2
|
||||
}
|
||||
|
||||
export enum TextEditorLineNumbersStyle {
|
||||
Off = 0,
|
||||
On = 1,
|
||||
|
||||
@@ -10,7 +10,7 @@ import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace
|
||||
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
|
||||
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
|
||||
import { ICommonCodeEditor } from 'vs/editor/common/editorCommon';
|
||||
import { bulkEdit, IResourceTextEdit } from 'vs/editor/common/services/bulkEdit';
|
||||
import { bulkEdit, IResourceEdit } from 'vs/editor/common/services/bulkEdit';
|
||||
import { TPromise } from 'vs/base/common/winjs.base';
|
||||
import { Uri } from 'vscode';
|
||||
import { MainThreadWorkspaceShape } from './extHost.protocol';
|
||||
@@ -89,7 +89,7 @@ export class MainThreadWorkspace extends MainThreadWorkspaceShape {
|
||||
});
|
||||
}
|
||||
|
||||
$applyWorkspaceEdit(edits: IResourceTextEdit[]): TPromise<boolean> {
|
||||
$applyWorkspaceEdit(edits: IResourceEdit[]): TPromise<boolean> {
|
||||
|
||||
let codeEditor: ICommonCodeEditor;
|
||||
let editor = this._editorService.getActiveEditor();
|
||||
|
||||
Reference in New Issue
Block a user