|
1 | 1 | package ts.eclipse.ide.jsdt.internal.ui.editor.format;
|
2 | 2 |
|
| 3 | +import org.eclipse.core.resources.IResource; |
| 4 | +import org.eclipse.jface.text.DocumentRewriteSession; |
3 | 5 | import org.eclipse.jface.text.IDocument;
|
4 | 6 | import org.eclipse.jface.text.IRegion;
|
5 | 7 | import org.eclipse.jface.text.formatter.IContentFormatter;
|
6 | 8 | 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; |
8 | 14 |
|
| 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 | + */ |
9 | 28 | public class TypeScriptContentFormatter implements IContentFormatter {
|
10 | 29 |
|
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; |
13 | 35 | }
|
14 | 36 |
|
15 | 37 | @Override
|
16 | 38 | 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 | + } |
18 | 72 | }
|
19 | 73 |
|
20 | 74 | @Override
|
|
0 commit comments