|
1 | | -package org.netbeans.modules.python.editor; |
2 | | - |
3 | | -/** |
4 | | - * |
5 | | - * @author albilu |
6 | | - */ |
7 | | -import java.util.ArrayList; |
8 | | -import java.util.Comparator; |
9 | | -import java.util.List; |
10 | | -import java.util.concurrent.ExecutionException; |
11 | | -import java.util.logging.Level; |
12 | | -import java.util.logging.Logger; |
13 | | -import javax.swing.text.BadLocationException; |
14 | | -import javax.swing.text.Document; |
15 | | -import javax.swing.text.StyledDocument; |
16 | | -import org.apache.commons.lang3.StringUtils; |
17 | | -import org.eclipse.lsp4j.DocumentFormattingParams; |
18 | | -import org.eclipse.lsp4j.DocumentRangeFormattingParams; |
19 | | -import org.eclipse.lsp4j.FormattingOptions; |
20 | | -import org.eclipse.lsp4j.Range; |
21 | | -import org.eclipse.lsp4j.TextDocumentIdentifier; |
22 | | -import org.eclipse.lsp4j.TextEdit; |
23 | | -import org.netbeans.modules.editor.NbEditorUtilities; |
24 | | -import org.netbeans.modules.editor.indent.api.IndentUtils; |
25 | | -import org.netbeans.modules.editor.indent.spi.Context; |
26 | | -import org.netbeans.modules.editor.indent.spi.ExtraLock; |
27 | | -import org.netbeans.modules.lsp.client.LSPBindings; |
28 | | -import org.netbeans.modules.lsp.client.Utils; |
29 | | -import org.netbeans.modules.python.PythonUtility; |
30 | | -import org.openide.filesystems.FileObject; |
31 | | -import org.openide.text.NbDocument; |
32 | | -import org.openide.util.Exceptions; |
33 | | - |
34 | | -public class PythonReformatTask |
35 | | - implements org.netbeans.modules.editor.indent.spi.ReformatTask { |
36 | | - |
37 | | - private final Context context; |
38 | | - |
39 | | - public PythonReformatTask(Context context) { |
40 | | - this.context = context; |
41 | | - } |
42 | | - |
43 | | - @Override |
44 | | - public void reformat() throws BadLocationException { |
45 | | - Document document = context.document(); |
46 | | - boolean rangeOrDoc = context.startOffset() != 0; |
47 | | - FileObject fileObject = NbEditorUtilities.getFileObject(document); |
48 | | - if (fileObject != null && !StringUtils.containsAny(fileObject.getPath(), |
49 | | - PythonUtility.EXCLUDED_DIRS)) { |
50 | | - LSPBindings bindings = LSPBindings.getBindings(fileObject); |
51 | | - if (bindings != null) { |
52 | | - boolean documentFormatting = Utils.isEnabled(bindings |
53 | | - .getInitResult().getCapabilities() |
54 | | - .getDocumentFormattingProvider()); |
55 | | - boolean rangeFormatting = Utils.isEnabled(bindings |
56 | | - .getInitResult().getCapabilities() |
57 | | - .getDocumentRangeFormattingProvider()); |
58 | | - if (rangeFormatting && rangeOrDoc) { |
59 | | - rangeFormat(NbEditorUtilities.getFileObject(document), bindings); |
60 | | - } else if (documentFormatting && !rangeOrDoc) { |
61 | | - documentFormat(NbEditorUtilities.getFileObject(document), bindings); |
62 | | - } |
63 | | - } |
64 | | - } |
65 | | - |
66 | | - } |
67 | | - |
68 | | - private void rangeFormat(FileObject fo, LSPBindings bindings) |
69 | | - throws BadLocationException { |
70 | | - DocumentRangeFormattingParams drfp = new DocumentRangeFormattingParams(); |
71 | | - drfp.setTextDocument(new TextDocumentIdentifier(Utils.toURI(fo))); |
72 | | - drfp.setOptions(new FormattingOptions( |
73 | | - IndentUtils.indentLevelSize(context.document()), |
74 | | - IndentUtils.isExpandTabs(context.document()))); |
75 | | - drfp.setRange(new Range( |
76 | | - Utils.createPosition(context.document(), context.startOffset()), |
77 | | - Utils.createPosition(context.document(), context.endOffset()))); |
78 | | - List<TextEdit> edits = new ArrayList<>(); |
79 | | - try { |
80 | | - edits = new ArrayList<>(bindings.getTextDocumentService() |
81 | | - .rangeFormatting(drfp).get()); |
82 | | - } catch (InterruptedException | ExecutionException ex) { |
83 | | - Logger.getLogger(this.getClass().getName()).log(Level.INFO, |
84 | | - String.format("LSP document rangeFormat failed for {0}", fo), |
85 | | - ex); |
86 | | - } |
87 | | - |
88 | | - applyTextEdits(edits); |
89 | | - } |
90 | | - |
91 | | - private void documentFormat(FileObject fo, LSPBindings bindings) |
92 | | - throws BadLocationException { |
93 | | - DocumentFormattingParams dfp = new DocumentFormattingParams(); |
94 | | - dfp.setTextDocument(new TextDocumentIdentifier(Utils.toURI(fo))); |
95 | | - dfp.setOptions(new FormattingOptions( |
96 | | - IndentUtils.indentLevelSize(context.document()), |
97 | | - IndentUtils.isExpandTabs(context.document()))); |
98 | | - List<TextEdit> edits = new ArrayList<>(); |
99 | | - try { |
100 | | - edits.addAll(bindings.getTextDocumentService().formatting(dfp).get()); |
101 | | - } catch (InterruptedException | ExecutionException ex) { |
102 | | - Logger.getLogger(this.getClass().getName()).log(Level.INFO, |
103 | | - String.format("LSP document format failed for {0}", fo), |
104 | | - ex); |
105 | | - } |
106 | | - |
107 | | - applyTextEdits(edits); |
108 | | - } |
109 | | - |
110 | | - private void applyTextEdits(List<TextEdit> edits) { |
111 | | - if (context.document() instanceof StyledDocument) { |
112 | | - NbDocument.runAtomic((StyledDocument) context.document(), () -> { |
113 | | - applyEditsNoLock(context.document(), edits, context.startOffset(), |
114 | | - context.endOffset()); |
115 | | - }); |
116 | | - } else { |
117 | | - applyEditsNoLock(context.document(), edits, context.startOffset(), |
118 | | - context.endOffset()); |
119 | | - } |
120 | | - } |
121 | | - |
122 | | - public static void applyEditsNoLock(Document doc, List<? extends TextEdit> edits, Integer startLimit, Integer endLimit) { |
123 | | - edits |
124 | | - .stream() |
125 | | - .sorted(rangeReverseSort) |
126 | | - .forEach(te -> { |
127 | | - try { |
128 | | - int start = Utils.getOffset(doc, te.getRange().getStart()); |
129 | | - int end = Utils.getOffset(doc, te.getRange().getEnd()); |
130 | | - if ((startLimit == null || start >= startLimit) |
131 | | - && (endLimit == null || end >= 0 || endLimit >= 0)) { |
132 | | - //https://github.com/apache/netbeans/blob/862379894fc239268e55f00bb4ce337d4c4437b8/ide/lsp.client/src/org/netbeans/modules/lsp/client/Utils.java#L182 |
133 | | - doc.remove(start, (end - start < 0 || end - start > endLimit) ? endLimit - start : end - start); |
134 | | - doc.insertString(start, te.getNewText(), null); |
135 | | - } |
136 | | - } catch (BadLocationException ex) { |
137 | | - Exceptions.printStackTrace(ex); |
138 | | - } |
139 | | - }); |
140 | | - } |
141 | | - |
142 | | - private static final Comparator<TextEdit> rangeReverseSort = (s1, s2) -> { |
143 | | - int l1 = s1.getRange().getEnd().getLine(); |
144 | | - int l2 = s2.getRange().getEnd().getLine(); |
145 | | - int c1 = s1.getRange().getEnd().getCharacter(); |
146 | | - int c2 = s2.getRange().getEnd().getCharacter(); |
147 | | - if (l1 != l2) { |
148 | | - return l2 - l1; |
149 | | - } else { |
150 | | - return c2 - c1; |
151 | | - } |
152 | | - }; |
153 | | - |
154 | | - @Override |
155 | | - public ExtraLock reformatLock() { |
156 | | - return null; |
157 | | - } |
158 | | - |
159 | | -} |
| 1 | +package org.netbeans.modules.python.editor; |
| 2 | + |
| 3 | +/** |
| 4 | + * |
| 5 | + * @author albilu |
| 6 | + */ |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.Comparator; |
| 9 | +import java.util.List; |
| 10 | +import java.util.concurrent.ExecutionException; |
| 11 | +import java.util.logging.Level; |
| 12 | +import java.util.logging.Logger; |
| 13 | +import javax.swing.text.BadLocationException; |
| 14 | +import javax.swing.text.Document; |
| 15 | +import javax.swing.text.StyledDocument; |
| 16 | +import org.apache.commons.lang3.StringUtils; |
| 17 | +import org.eclipse.lsp4j.DocumentFormattingParams; |
| 18 | +import org.eclipse.lsp4j.DocumentRangeFormattingParams; |
| 19 | +import org.eclipse.lsp4j.FormattingOptions; |
| 20 | +import org.eclipse.lsp4j.Range; |
| 21 | +import org.eclipse.lsp4j.TextDocumentIdentifier; |
| 22 | +import org.eclipse.lsp4j.TextEdit; |
| 23 | +import org.netbeans.modules.editor.NbEditorUtilities; |
| 24 | +import org.netbeans.modules.editor.indent.api.IndentUtils; |
| 25 | +import org.netbeans.modules.editor.indent.spi.Context; |
| 26 | +import org.netbeans.modules.editor.indent.spi.ExtraLock; |
| 27 | +import org.netbeans.modules.lsp.client.LSPBindings; |
| 28 | +import org.netbeans.modules.lsp.client.Utils; |
| 29 | +import org.netbeans.modules.python.PythonUtility; |
| 30 | +import org.openide.filesystems.FileObject; |
| 31 | +import org.openide.text.NbDocument; |
| 32 | +import org.openide.util.Exceptions; |
| 33 | + |
| 34 | +public class PythonReformatTask |
| 35 | + implements org.netbeans.modules.editor.indent.spi.ReformatTask { |
| 36 | + |
| 37 | + private final Context context; |
| 38 | + |
| 39 | + public PythonReformatTask(Context context) { |
| 40 | + this.context = context; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void reformat() throws BadLocationException { |
| 45 | + Document document = context.document(); |
| 46 | + boolean rangeOrDoc = context.startOffset() != 0; |
| 47 | + FileObject fileObject = NbEditorUtilities.getFileObject(document); |
| 48 | + if (fileObject != null && !StringUtils.containsAny(fileObject.getPath(), |
| 49 | + PythonUtility.EXCLUDED_DIRS)) { |
| 50 | + List<LSPBindings> bindings = LSPBindings.getBindings(fileObject); |
| 51 | + for (LSPBindings binding : bindings) { |
| 52 | + boolean documentFormatting = Utils.isEnabled(binding |
| 53 | + .getInitResult().getCapabilities() |
| 54 | + .getDocumentFormattingProvider()); |
| 55 | + boolean rangeFormatting = Utils.isEnabled(binding |
| 56 | + .getInitResult().getCapabilities() |
| 57 | + .getDocumentRangeFormattingProvider()); |
| 58 | + if (rangeFormatting && rangeOrDoc) { |
| 59 | + rangeFormat(NbEditorUtilities.getFileObject(document), binding); |
| 60 | + } else if (documentFormatting && !rangeOrDoc) { |
| 61 | + documentFormat(NbEditorUtilities.getFileObject(document), binding); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + private void rangeFormat(FileObject fo, LSPBindings bindings) |
| 69 | + throws BadLocationException { |
| 70 | + DocumentRangeFormattingParams drfp = new DocumentRangeFormattingParams(); |
| 71 | + drfp.setTextDocument(new TextDocumentIdentifier(Utils.toURI(fo))); |
| 72 | + drfp.setOptions(new FormattingOptions( |
| 73 | + IndentUtils.indentLevelSize(context.document()), |
| 74 | + IndentUtils.isExpandTabs(context.document()))); |
| 75 | + drfp.setRange(new Range( |
| 76 | + Utils.createPosition(context.document(), context.startOffset()), |
| 77 | + Utils.createPosition(context.document(), context.endOffset()))); |
| 78 | + List<TextEdit> edits = new ArrayList<>(); |
| 79 | + try { |
| 80 | + edits = new ArrayList<>(bindings.getTextDocumentService() |
| 81 | + .rangeFormatting(drfp).get()); |
| 82 | + } catch (InterruptedException | ExecutionException ex) { |
| 83 | + Logger.getLogger(this.getClass().getName()).log(Level.INFO, |
| 84 | + String.format("LSP document rangeFormat failed for {0}", fo), |
| 85 | + ex); |
| 86 | + } |
| 87 | + |
| 88 | + applyTextEdits(edits); |
| 89 | + } |
| 90 | + |
| 91 | + private void documentFormat(FileObject fo, LSPBindings bindings) |
| 92 | + throws BadLocationException { |
| 93 | + DocumentFormattingParams dfp = new DocumentFormattingParams(); |
| 94 | + dfp.setTextDocument(new TextDocumentIdentifier(Utils.toURI(fo))); |
| 95 | + dfp.setOptions(new FormattingOptions( |
| 96 | + IndentUtils.indentLevelSize(context.document()), |
| 97 | + IndentUtils.isExpandTabs(context.document()))); |
| 98 | + List<TextEdit> edits = new ArrayList<>(); |
| 99 | + try { |
| 100 | + edits.addAll(bindings.getTextDocumentService().formatting(dfp).get()); |
| 101 | + } catch (InterruptedException | ExecutionException ex) { |
| 102 | + Logger.getLogger(this.getClass().getName()).log(Level.INFO, |
| 103 | + String.format("LSP document format failed for {0}", fo), |
| 104 | + ex); |
| 105 | + } |
| 106 | + |
| 107 | + applyTextEdits(edits); |
| 108 | + } |
| 109 | + |
| 110 | + private void applyTextEdits(List<TextEdit> edits) { |
| 111 | + if (context.document() instanceof StyledDocument) { |
| 112 | + NbDocument.runAtomic((StyledDocument) context.document(), () -> { |
| 113 | + applyEditsNoLock(context.document(), edits, context.startOffset(), |
| 114 | + context.endOffset()); |
| 115 | + }); |
| 116 | + } else { |
| 117 | + applyEditsNoLock(context.document(), edits, context.startOffset(), |
| 118 | + context.endOffset()); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + public static void applyEditsNoLock(Document doc, List<? extends TextEdit> edits, Integer startLimit, Integer endLimit) { |
| 123 | + edits |
| 124 | + .stream() |
| 125 | + .sorted(rangeReverseSort) |
| 126 | + .forEach(te -> { |
| 127 | + try { |
| 128 | + int start = Utils.getOffset(doc, te.getRange().getStart()); |
| 129 | + int end = Utils.getOffset(doc, te.getRange().getEnd()); |
| 130 | + if ((startLimit == null || start >= startLimit) |
| 131 | + && (endLimit == null || end >= 0 || endLimit >= 0)) { |
| 132 | + //https://github.com/apache/netbeans/blob/862379894fc239268e55f00bb4ce337d4c4437b8/ide/lsp.client/src/org/netbeans/modules/lsp/client/Utils.java#L182 |
| 133 | + doc.remove(start, (end - start < 0 || end - start > endLimit) ? endLimit - start : end - start); |
| 134 | + doc.insertString(start, te.getNewText(), null); |
| 135 | + } |
| 136 | + } catch (BadLocationException ex) { |
| 137 | + Exceptions.printStackTrace(ex); |
| 138 | + } |
| 139 | + }); |
| 140 | + } |
| 141 | + |
| 142 | + private static final Comparator<TextEdit> rangeReverseSort = (s1, s2) -> { |
| 143 | + int l1 = s1.getRange().getEnd().getLine(); |
| 144 | + int l2 = s2.getRange().getEnd().getLine(); |
| 145 | + int c1 = s1.getRange().getEnd().getCharacter(); |
| 146 | + int c2 = s2.getRange().getEnd().getCharacter(); |
| 147 | + if (l1 != l2) { |
| 148 | + return l2 - l1; |
| 149 | + } else { |
| 150 | + return c2 - c1; |
| 151 | + } |
| 152 | + }; |
| 153 | + |
| 154 | + @Override |
| 155 | + public ExtraLock reformatLock() { |
| 156 | + return null; |
| 157 | + } |
| 158 | + |
| 159 | +} |
0 commit comments