mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-24 18:49:00 +01:00
Merge pull request #10310 from Microsoft/joh/miniedits
Make text edits more minimal
This commit is contained in:
@@ -336,11 +336,11 @@ class DocumentFormattingAdapter {
|
||||
|
||||
provideDocumentFormattingEdits(resource: URI, options: modes.FormattingOptions): TPromise<ISingleEditOperation[]> {
|
||||
|
||||
let doc = this._documents.getDocumentData(resource).document;
|
||||
const {document, version} = this._documents.getDocumentData(resource);
|
||||
|
||||
return asWinJsPromise(token => this._provider.provideDocumentFormattingEdits(doc, <any>options, token)).then(value => {
|
||||
return asWinJsPromise(token => this._provider.provideDocumentFormattingEdits(document, <any>options, token)).then(value => {
|
||||
if (Array.isArray(value)) {
|
||||
return value.map(TypeConverters.TextEdit.from);
|
||||
return TypeConverters.TextEdit.minimalEditOperations(value, document, version);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -358,12 +358,12 @@ class RangeFormattingAdapter {
|
||||
|
||||
provideDocumentRangeFormattingEdits(resource: URI, range: IRange, options: modes.FormattingOptions): TPromise<ISingleEditOperation[]> {
|
||||
|
||||
let doc = this._documents.getDocumentData(resource).document;
|
||||
let ran = TypeConverters.toRange(range);
|
||||
const {document, version} = this._documents.getDocumentData(resource);
|
||||
const ran = TypeConverters.toRange(range);
|
||||
|
||||
return asWinJsPromise(token => this._provider.provideDocumentRangeFormattingEdits(doc, ran, <any>options, token)).then(value => {
|
||||
return asWinJsPromise(token => this._provider.provideDocumentRangeFormattingEdits(document, ran, <any>options, token)).then(value => {
|
||||
if (Array.isArray(value)) {
|
||||
return value.map(TypeConverters.TextEdit.from);
|
||||
return TypeConverters.TextEdit.minimalEditOperations(value, document, version);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -383,12 +383,12 @@ class OnTypeFormattingAdapter {
|
||||
|
||||
provideOnTypeFormattingEdits(resource: URI, position: IPosition, ch: string, options: modes.FormattingOptions): TPromise<ISingleEditOperation[]> {
|
||||
|
||||
let doc = this._documents.getDocumentData(resource).document;
|
||||
let pos = TypeConverters.toPosition(position);
|
||||
const {document, version} = this._documents.getDocumentData(resource);
|
||||
const pos = TypeConverters.toPosition(position);
|
||||
|
||||
return asWinJsPromise(token => this._provider.provideOnTypeFormattingEdits(doc, pos, ch, <any> options, token)).then(value => {
|
||||
return asWinJsPromise(token => this._provider.provideOnTypeFormattingEdits(document, pos, ch, <any> options, token)).then(value => {
|
||||
if (Array.isArray(value)) {
|
||||
return value.map(TypeConverters.TextEdit.from);
|
||||
return TypeConverters.TextEdit.minimalEditOperations(value, document, version);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import {ExtHostCommands} from 'vs/workbench/api/node/extHostCommands';
|
||||
import Severity from 'vs/base/common/severity';
|
||||
import {isFalsyOrEmpty} from 'vs/base/common/arrays';
|
||||
import {IDisposable} from 'vs/base/common/lifecycle';
|
||||
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,6 +155,43 @@ export function fromRangeOrRangeWithMessage(ranges:vscode.Range[]|vscode.Decorat
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
for (let j = 0; j < changes.length; j++) {
|
||||
const {originalStart, originalLength, modifiedStart, modifiedLength} = changes[j];
|
||||
const start = fromPosition(<types.Position> document.positionAt(originalStart));
|
||||
const end = fromPosition(<types.Position> document.positionAt(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