Skip to content

Commit 564f3da

Browse files
committed
Apply all edits to document as single modification
1 parent 3396aab commit 564f3da

File tree

5 files changed

+62
-13
lines changed

5 files changed

+62
-13
lines changed

core/ts.core/src/ts/client/codefixes/CodeAction.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
/**
2+
* Copyright (c) 2015-2016 Angelo ZERR.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Angelo Zerr <[email protected]> - initial API and implementation
10+
*/
111
package ts.client.codefixes;
212

313
import java.util.List;
@@ -8,11 +18,11 @@ public class CodeAction {
818
private String description;
919
/** Text changes to apply to each file as part of the code action */
1020
private List<FileCodeEdits> changes;
11-
21+
1222
public String getDescription() {
1323
return description;
1424
}
15-
25+
1626
public List<FileCodeEdits> getChanges() {
1727
return changes;
1828
}

core/ts.core/src/ts/client/codefixes/FileCodeEdits.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
/**
2+
* Copyright (c) 2015-2016 Angelo ZERR.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Angelo Zerr <[email protected]> - initial API and implementation
10+
*/
111
package ts.client.codefixes;
212

313
import java.util.List;

eclipse/jsdt/ts.eclipse.ide.jsdt.ui/src/ts/eclipse/ide/jsdt/internal/ui/editor/format/TypeScriptContentFormatter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ public void format(IDocument document, IRegion region) {
4545
@Override
4646
public void run() {
4747
try {
48-
TextEdit textEdit = DocumentUtils.toTextEdit(codeEdits, document);
49-
textEdit.apply(document, TextEdit.CREATE_UNDO);
48+
DocumentUtils.applyEdits(document, codeEdits);
5049
} catch (Exception e) {
5150
IStatus status = new Status(IStatus.ERROR, JSDTTypeScriptUIPlugin.PLUGIN_ID, e.getMessage(),
5251
e);

eclipse/ts.eclipse.ide.core/src/ts/eclipse/ide/core/utils/DocumentUtils.java

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
import org.eclipse.jface.text.IDocument;
77
import org.eclipse.text.edits.DeleteEdit;
88
import org.eclipse.text.edits.InsertEdit;
9+
import org.eclipse.text.edits.MalformedTreeException;
910
import org.eclipse.text.edits.MultiTextEdit;
1011
import org.eclipse.text.edits.ReplaceEdit;
1112
import org.eclipse.text.edits.TextEdit;
13+
import org.eclipse.text.undo.DocumentUndoManagerRegistry;
14+
import org.eclipse.text.undo.IDocumentUndoManager;
1215

1316
import ts.TypeScriptException;
1417
import ts.client.CodeEdit;
@@ -31,13 +34,40 @@ public static int getPosition(IDocument document, int line, int offset) throws T
3134
}
3235
}
3336

34-
public static TextEdit toTextEdit(CodeEdit codeEdit, IDocument document) throws TypeScriptException {
35-
MultiTextEdit textEdit = new MultiTextEdit();
36-
toTextEdit(codeEdit, document, textEdit);
37-
return textEdit;
37+
/**
38+
* Method will apply all edits to document as single modification. Needs to
39+
* be executed in UI thread.
40+
*
41+
* @param document
42+
* document to modify
43+
* @param edits
44+
* list of TypeScript {@link CodeEdit}.
45+
* @throws TypeScriptException
46+
* @throws BadLocationException
47+
* @throws MalformedTreeException
48+
*/
49+
public static void applyEdits(IDocument document, List<CodeEdit> codeEdits)
50+
throws TypeScriptException, MalformedTreeException, BadLocationException {
51+
if (document == null || codeEdits.isEmpty()) {
52+
return;
53+
}
54+
55+
IDocumentUndoManager manager = DocumentUndoManagerRegistry.getDocumentUndoManager(document);
56+
if (manager != null) {
57+
manager.beginCompoundChange();
58+
}
59+
60+
try {
61+
TextEdit edit = toTextEdit(codeEdits, document);
62+
edit.apply(document);
63+
} finally {
64+
if (manager != null) {
65+
manager.endCompoundChange();
66+
}
67+
}
3868
}
3969

40-
public static TextEdit toTextEdit(List<CodeEdit> codeEdits, IDocument document) throws TypeScriptException {
70+
private static TextEdit toTextEdit(List<CodeEdit> codeEdits, IDocument document) throws TypeScriptException {
4171
MultiTextEdit textEdit = new MultiTextEdit();
4272
for (CodeEdit codeEdit : codeEdits) {
4373
toTextEdit(codeEdit, document, textEdit);

eclipse/ts.eclipse.ide.ui/src/ts/eclipse/ide/ui/hover/CodeActionCompletionProposal.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
import org.eclipse.jface.text.contentassist.IContextInformation;
1616
import org.eclipse.swt.graphics.Image;
1717
import org.eclipse.swt.graphics.Point;
18-
import org.eclipse.text.edits.TextEdit;
1918

2019
import ts.client.codefixes.CodeAction;
2120
import ts.client.codefixes.FileCodeEdits;
2221
import ts.eclipse.ide.core.utils.DocumentUtils;
22+
import ts.eclipse.ide.ui.TypeScriptUIPlugin;
2323

2424
/**
2525
* TypeScript {@link CodeAction} completion proposal.
@@ -42,11 +42,11 @@ public void apply(IDocument document) {
4242

4343
private void apply(FileCodeEdits codeEdits, IDocument document) {
4444
String fileName = codeEdits.getFileName();
45+
// TODO: support code edit for the given file name.
4546
try {
46-
TextEdit textEdit = DocumentUtils.toTextEdit(codeEdits.getTextChanges(), document);
47-
textEdit.apply(document, TextEdit.CREATE_UNDO);
47+
DocumentUtils.applyEdits(document, codeEdits.getTextChanges());
4848
} catch (Exception e) {
49-
e.printStackTrace();
49+
TypeScriptUIPlugin.log(e);
5050
}
5151
}
5252

0 commit comments

Comments
 (0)