Skip to content

Commit 7f76f73

Browse files
author
angelozerr
committed
Consumes tsserver "format" command to format. See
#36
1 parent 0bb82fb commit 7f76f73

File tree

2 files changed

+60
-6
lines changed

2 files changed

+60
-6
lines changed

eclipse/jsdt/ts.eclipse.ide.jsdt.ui/src/ts/eclipse/ide/jsdt/internal/ui/editor/TypeScriptSourceViewerConfiguration.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
import org.eclipse.wst.jsdt.ui.text.JavaScriptSourceViewerConfiguration;
2929

3030
import ts.eclipse.ide.jsdt.internal.ui.editor.contentassist.TypeScriptCompletionProcessor;
31-
import ts.eclipse.ide.jsdt.internal.ui.editor.contentassist.TypeScriptContentAssistInvocationContext;
3231
import ts.eclipse.ide.jsdt.internal.ui.editor.format.TypeScriptContentFormatter;
32+
import ts.eclipse.ide.ui.utils.EditorUtils;
3333

3434
/**
3535
* Extension of JSDT {@link JavaScriptSourceViewerConfiguration}
@@ -101,6 +101,6 @@ private IDialogSettings getSettings(final String sectionName) {
101101

102102
@Override
103103
public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {
104-
return new TypeScriptContentFormatter(getEditor());
104+
return new TypeScriptContentFormatter(EditorUtils.getResource(getEditor()));
105105
}
106106
}

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

+58-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,74 @@
11
package ts.eclipse.ide.jsdt.internal.ui.editor.format;
22

3+
import org.eclipse.core.resources.IResource;
4+
import org.eclipse.jface.text.DocumentRewriteSession;
35
import org.eclipse.jface.text.IDocument;
46
import org.eclipse.jface.text.IRegion;
57
import org.eclipse.jface.text.formatter.IContentFormatter;
68
import org.eclipse.jface.text.formatter.IFormattingStrategy;
7-
import org.eclipse.ui.texteditor.ITextEditor;
9+
import org.eclipse.text.edits.DeleteEdit;
10+
import org.eclipse.text.edits.InsertEdit;
11+
import org.eclipse.text.edits.MultiTextEdit;
12+
import org.eclipse.text.edits.ReplaceEdit;
13+
import org.eclipse.text.edits.TextEdit;
814

15+
import ts.TypeScriptException;
16+
import ts.client.format.ITypeScriptFormatCollector;
17+
import ts.eclipse.ide.core.TypeScriptCorePlugin;
18+
import ts.eclipse.ide.core.resources.IIDETypeScriptFile;
19+
import ts.eclipse.ide.core.resources.IIDETypeScriptProject;
20+
21+
/**
22+
* Content formatter which consumes tsserver "format" command to format an
23+
* IDocument.
24+
*
25+
* @author azerr
26+
*
27+
*/
928
public class TypeScriptContentFormatter implements IContentFormatter {
1029

11-
public TypeScriptContentFormatter(ITextEditor editor) {
12-
// TODO Auto-generated constructor stub
30+
private final IResource resource;
31+
private DocumentRewriteSession fRewriteSession;
32+
33+
public TypeScriptContentFormatter(IResource resource) {
34+
this.resource = resource;
1335
}
1436

1537
@Override
1638
public void format(IDocument document, IRegion region) {
17-
System.err.println(document.get());
39+
try {
40+
IIDETypeScriptProject tsProject = TypeScriptCorePlugin.getDefault()
41+
.getTypeScriptProject(resource.getProject());
42+
final IIDETypeScriptFile tsFile = tsProject.openFile(resource, document);
43+
44+
final MultiTextEdit textEdit = new MultiTextEdit();
45+
int startPosition = region.getOffset();
46+
int endPosition = region.getOffset() + region.getLength() - 1;
47+
tsFile.format(startPosition, endPosition, new ITypeScriptFormatCollector() {
48+
49+
@Override
50+
public void format(int startLine, int startOffset, int endLine, int endOffset, String newText)
51+
throws TypeScriptException {
52+
int start = tsFile.getPosition(startLine, startOffset);
53+
int end = tsFile.getPosition(endLine, endOffset);
54+
int length = end - start;
55+
if (newText.isEmpty()) {
56+
if (length > 0) {
57+
textEdit.addChild(new DeleteEdit(start, length));
58+
}
59+
} else {
60+
if (length > 0) {
61+
textEdit.addChild(new ReplaceEdit(start, length, newText));
62+
} else if (length == 0) {
63+
textEdit.addChild(new InsertEdit(start, newText));
64+
}
65+
}
66+
}
67+
});
68+
textEdit.apply(document, TextEdit.CREATE_UNDO);
69+
} catch (Exception e) {
70+
e.printStackTrace();
71+
}
1872
}
1973

2074
@Override

0 commit comments

Comments
 (0)