@@ -50,75 +50,6 @@ public class XMLAutoCloseTagReconciler implements IReconciler {
5050
5151 private Listener listener ;
5252
53- private void autoInsert (DocumentEvent event ) {
54- if (!isEnabled ()) {
55- return ;
56- }
57- if (event == null || viewer == null ) {
58- return ;
59- }
60- IDocument document = event .getDocument ();
61- if (document == null || event == null || event .getLength () != 0 || event .getText ().length () != 1 ) {
62- return ;
63- }
64-
65- int offset = event .getOffset () + 1 ;
66- char c = event .getText ().charAt (0 );
67- if (c != '>' && c != '/' ) {
68- return ;
69- }
70- URI uri = LSPEclipseUtils .toUri (document );
71- if (uri == null ) {
72- return ;
73- }
74-
75- TextDocumentIdentifier identifier = new TextDocumentIdentifier (uri .toString ());
76- Optional <LSPDocumentInfo > info = LanguageServiceAccessor
77- .getLSPDocumentInfosFor (document , (capabilities ) -> true ).stream ()
78- .filter (doc -> (doc .getLanguageClient () instanceof XMLLanguageServerAPI )).findAny ();
79- if (!info .isEmpty ()) {
80- // The document is bound with XML language server, consumes the xml/closeTag
81- final Display display = viewer .getTextWidget ().getDisplay ();
82- CompletableFuture .supplyAsync (() -> {
83- try {
84- // Wait for textDocument/didChange
85- Thread .sleep (100 );
86- } catch (InterruptedException ex ) {
87- Thread .interrupted ();
88- }
89- try {
90- TextDocumentPositionParams params = LSPEclipseUtils .toTextDocumentPosistionParams (uri , offset ,
91- document );
92- // consumes xml/closeTag from XML language server
93- ((XMLLanguageServerAPI ) info .get ().getLanguageClient ()).closeTag (params ).thenAccept (r -> {
94- if (r != null ) {
95- display .asyncExec (() -> {
96- try {
97- // we receive a text like
98- // $0</foo>
99- // $0 should be used for set the cursor.
100- String text = r .snippet .replace ("$0" , "" );
101- int replaceLength = getReplaceLength (r .range , document );
102- document .replace (offset , replaceLength , text );
103- } catch (BadLocationException e ) {
104- // Do nothing
105- }
106- });
107-
108- }
109- });
110- } catch (BadLocationException e ) {
111- // Do nothing
112- }
113- return null ;
114- });
115- }
116- }
117-
118- private boolean isEnabled () {
119- return Activator .getDefault ().getPreferenceStore ().getBoolean (XML_PREFERENCES_COMPLETION_AUTO_CLOSE_TAGS );
120- }
121-
12253 private static int getReplaceLength (Range range , IDocument document ) throws BadLocationException {
12354 if (range == null ) {
12455 return 0 ;
@@ -138,24 +69,15 @@ private static int getReplaceLength(Range range, IDocument document) throws BadL
13869 */
13970 class Listener implements IDocumentListener , ITextInputListener {
14071
141- /*
142- * @see IDocumentListener#documentAboutToBeChanged(DocumentEvent)
143- */
14472 @ Override
14573 public void documentAboutToBeChanged (DocumentEvent e ) {
14674 }
14775
148- /*
149- * @see IDocumentListener#documentChanged(DocumentEvent)
150- */
15176 @ Override
15277 public void documentChanged (DocumentEvent e ) {
15378 autoInsert (e );
15479 }
15580
156- /*
157- * @see ITextInputListener#inputDocumentAboutToBeChanged(IDocument, IDocument)
158- */
15981 @ Override
16082 public void inputDocumentAboutToBeChanged (IDocument oldInput , IDocument newInput ) {
16183 if (oldInput == document ) {
@@ -166,9 +88,6 @@ public void inputDocumentAboutToBeChanged(IDocument oldInput, IDocument newInput
16688 }
16789 }
16890
169- /*
170- * @see ITextInputListener#inputDocumentChanged(IDocument, IDocument)
171- */
17291 @ Override
17392 public void inputDocumentChanged (IDocument oldInput , IDocument newInput ) {
17493 document = newInput ;
@@ -177,6 +96,75 @@ public void inputDocumentChanged(IDocument oldInput, IDocument newInput) {
17796 }
17897 document .addDocumentListener (this );
17998 }
99+
100+ private void autoInsert (DocumentEvent event ) {
101+ if (!isEnabled ()) {
102+ return ;
103+ }
104+ if (event == null || viewer == null ) {
105+ return ;
106+ }
107+ IDocument document = event .getDocument ();
108+ if (document == null || event == null || event .getLength () != 0 || event .getText ().length () != 1 ) {
109+ return ;
110+ }
111+
112+ int offset = event .getOffset () + 1 ;
113+ char c = event .getText ().charAt (0 );
114+ if (c != '>' && c != '/' ) {
115+ return ;
116+ }
117+ URI uri = LSPEclipseUtils .toUri (document );
118+ if (uri == null ) {
119+ return ;
120+ }
121+
122+ TextDocumentIdentifier identifier = new TextDocumentIdentifier (uri .toString ());
123+ Optional <LSPDocumentInfo > info = LanguageServiceAccessor
124+ .getLSPDocumentInfosFor (document , capabilities -> true ).stream ()
125+ .filter (doc -> (doc .getLanguageClient () instanceof XMLLanguageServerAPI )).findAny ();
126+ if (!info .isEmpty ()) {
127+ // The document is bound with XML language server, consumes the xml/closeTag
128+ final Display display = viewer .getTextWidget ().getDisplay ();
129+ CompletableFuture .supplyAsync (() -> {
130+ try {
131+ // Wait for textDocument/didChange
132+ Thread .sleep (100 );
133+ } catch (InterruptedException ex ) {
134+ Thread .interrupted ();
135+ }
136+ try {
137+ TextDocumentPositionParams params = LSPEclipseUtils .toTextDocumentPosistionParams (uri , offset ,
138+ document );
139+ // consumes xml/closeTag from XML language server
140+ ((XMLLanguageServerAPI ) info .get ().getLanguageClient ()).closeTag (params ).thenAccept (r -> {
141+ if (r != null ) {
142+ display .asyncExec (() -> {
143+ try {
144+ // we receive a text like
145+ // $0</foo>
146+ // $0 should be used for set the cursor.
147+ String text = r .snippet .replace ("$0" , "" );
148+ int replaceLength = getReplaceLength (r .range , document );
149+ document .replace (offset , replaceLength , text );
150+ } catch (BadLocationException e ) {
151+ // Do nothing
152+ }
153+ });
154+
155+ }
156+ });
157+ } catch (BadLocationException e ) {
158+ // Do nothing
159+ }
160+ return null ;
161+ });
162+ }
163+ }
164+
165+ private boolean isEnabled () {
166+ return Activator .getDefault ().getPreferenceStore ().getBoolean (XML_PREFERENCES_COMPLETION_AUTO_CLOSE_TAGS );
167+ }
180168
181169 }
182170
0 commit comments