mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-24 10:38:59 +01:00
This commit is contained in:
@@ -262,11 +262,11 @@ class DocumentFormattingAdapter {
|
||||
|
||||
provideDocumentFormattingEdits(resource: URI, options: modes.FormattingOptions): TPromise<ISingleEditOperation[]> {
|
||||
|
||||
const {document, version} = this._documents.getDocumentData(resource);
|
||||
const {document} = this._documents.getDocumentData(resource);
|
||||
|
||||
return asWinJsPromise(token => this._provider.provideDocumentFormattingEdits(document, <any>options, token)).then(value => {
|
||||
if (Array.isArray(value)) {
|
||||
return TypeConverters.TextEdit.minimalEditOperations(value, document, version);
|
||||
return value.map(TypeConverters.TextEdit.from);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -284,12 +284,12 @@ class RangeFormattingAdapter {
|
||||
|
||||
provideDocumentRangeFormattingEdits(resource: URI, range: IRange, options: modes.FormattingOptions): TPromise<ISingleEditOperation[]> {
|
||||
|
||||
const {document, version} = this._documents.getDocumentData(resource);
|
||||
const {document} = this._documents.getDocumentData(resource);
|
||||
const ran = TypeConverters.toRange(range);
|
||||
|
||||
return asWinJsPromise(token => this._provider.provideDocumentRangeFormattingEdits(document, ran, <any>options, token)).then(value => {
|
||||
if (Array.isArray(value)) {
|
||||
return TypeConverters.TextEdit.minimalEditOperations(value, document, version);
|
||||
return value.map(TypeConverters.TextEdit.from);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -309,12 +309,12 @@ class OnTypeFormattingAdapter {
|
||||
|
||||
provideOnTypeFormattingEdits(resource: URI, position: IPosition, ch: string, options: modes.FormattingOptions): TPromise<ISingleEditOperation[]> {
|
||||
|
||||
const {document, version} = this._documents.getDocumentData(resource);
|
||||
const {document} = this._documents.getDocumentData(resource);
|
||||
const pos = TypeConverters.toPosition(position);
|
||||
|
||||
return asWinJsPromise(token => this._provider.provideOnTypeFormattingEdits(document, pos, ch, <any>options, token)).then(value => {
|
||||
if (Array.isArray(value)) {
|
||||
return TypeConverters.TextEdit.minimalEditOperations(value, document, version);
|
||||
return value.map(TypeConverters.TextEdit.from);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
'use strict';
|
||||
|
||||
import Severity from 'vs/base/common/severity';
|
||||
import { stringDiff } from 'vs/base/common/diff/diff';
|
||||
import * as modes from 'vs/editor/common/modes';
|
||||
import * as types from './extHostTypes';
|
||||
import { Position as EditorPosition } from 'vs/platform/editor/common/editor';
|
||||
@@ -154,44 +153,6 @@ export function fromRangeOrRangeWithMessage(ranges: vscode.Range[] | vscode.Deco
|
||||
|
||||
export const TextEdit = {
|
||||
|
||||
minimalEditOperations(edits: vscode.TextEdit[], document: vscode.TextDocument, beforeDocumentVersion: number): ISingleEditOperation[] {
|
||||
|
||||
// document has changed in the meantime and we shouldn't do
|
||||
// offset math as it's likely to be all wrong
|
||||
if (document.version !== beforeDocumentVersion) {
|
||||
return edits.map(TextEdit.from);
|
||||
}
|
||||
|
||||
const result: ISingleEditOperation[] = [];
|
||||
|
||||
for (let edit of edits) {
|
||||
|
||||
const original = document.getText(edit.range);
|
||||
const modified = edit.newText;
|
||||
const changes = stringDiff(original, modified);
|
||||
|
||||
if (changes.length <= 1) {
|
||||
result.push(TextEdit.from(edit));
|
||||
continue;
|
||||
}
|
||||
|
||||
const editOffset = document.offsetAt(edit.range.start);
|
||||
|
||||
for (let j = 0; j < changes.length; j++) {
|
||||
const {originalStart, originalLength, modifiedStart, modifiedLength} = changes[j];
|
||||
const start = fromPosition(<types.Position>document.positionAt(editOffset + originalStart));
|
||||
const end = fromPosition(<types.Position>document.positionAt(editOffset + originalStart + originalLength));
|
||||
|
||||
result.push({
|
||||
text: modified.substr(modifiedStart, modifiedLength),
|
||||
range: { startLineNumber: start.lineNumber, startColumn: start.column, endLineNumber: end.lineNumber, endColumn: end.column }
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
},
|
||||
|
||||
from(edit: vscode.TextEdit): ISingleEditOperation {
|
||||
return <ISingleEditOperation>{
|
||||
text: edit.newText,
|
||||
|
||||
Reference in New Issue
Block a user