1
1
package ts .eclipse .jface .text .contentassist ;
2
2
3
3
import java .util .List ;
4
+ import java .util .stream .Collectors ;
4
5
5
6
import org .eclipse .jface .internal .text .html .BrowserInformationControl ;
6
7
import org .eclipse .jface .text .BadLocationException ;
35
36
import org .eclipse .swt .graphics .Image ;
36
37
import org .eclipse .swt .graphics .Point ;
37
38
import org .eclipse .swt .widgets .Shell ;
39
+ import org .eclipse .text .edits .MalformedTreeException ;
38
40
import org .eclipse .ui .texteditor .link .EditorLinkedModeUI ;
39
41
40
42
import ts .TypeScriptException ;
41
43
import ts .client .ITypeScriptServiceClient ;
44
+ import ts .client .codefixes .CodeAction ;
42
45
import ts .client .completions .CompletionEntry ;
43
46
import ts .client .completions .CompletionEntryDetails ;
44
47
import ts .client .completions .ICompletionEntryMatcher ;
45
48
import ts .client .completions .SymbolDisplayPart ;
46
49
import ts .eclipse .jface .images .TypeScriptImagesRegistry ;
50
+ import ts .eclipse .jface .text .DocumentUtils ;
47
51
import ts .eclipse .jface .text .HoverControlCreator ;
48
52
import ts .eclipse .jface .text .PresenterControlCreator ;
49
53
import ts .utils .StringUtils ;
@@ -131,7 +135,28 @@ public void apply(IDocument document, char trigger, int offset) {
131
135
// selected the proposal, and where the cursor offset is
132
136
// but we might in the future...
133
137
proposal .apply (document );
134
- int baseOffset = getReplacementOffset ();
138
+
139
+ int oldlength = document .getLength ();
140
+ int newlength = oldlength ;
141
+ try {
142
+ List <CompletionEntryDetails > entryDetails = super .getEntryDetails ();
143
+ if (entryDetails != null && entryDetails .size () > 0 ) {
144
+ List <CodeAction > codeActions = entryDetails .get (0 ).getCodeActions ();
145
+ if (codeActions != null ) {
146
+ try {
147
+ DocumentUtils .applyEdits (document , codeActions .get (0 ).getChanges ().stream ()
148
+ .flatMap (f -> f .getTextChanges ().stream ()).collect (Collectors .toList ()));
149
+
150
+ newlength = document .getLength ();
151
+ } catch (Exception e ) {
152
+ e .printStackTrace ();
153
+ }
154
+ }
155
+ }
156
+ } catch (TypeScriptException e ) {
157
+ }
158
+
159
+ int baseOffset = getReplacementOffset () + newlength - oldlength ;
135
160
136
161
if (arguments != null && !arguments .isEmpty () && getTextViewer () != null ) {
137
162
try {// adjust offset of the whole arguments
@@ -159,8 +184,7 @@ public void apply(IDocument document, char trigger, int offset) {
159
184
model .forceInstall ();
160
185
/*
161
186
* JavaEditor editor = getJavaEditor(); if (editor != null) {
162
- * model.addLinkingListener(new EditorHighlightingSynchronizer(
163
- * editor)); }
187
+ * model.addLinkingListener(new EditorHighlightingSynchronizer( editor)); }
164
188
*/
165
189
166
190
LinkedModeUI ui = new EditorLinkedModeUI (model , getTextViewer ());
@@ -537,21 +561,18 @@ public boolean validate(IDocument document, int offset, DocumentEvent event) {
537
561
/*
538
562
*
539
563
*
540
- * boolean validated = isMatchWord(document, offset,
541
- * getReplacementString()); if (validated) { StyledString
542
- * styledDisplayString = new StyledString();
543
- * styledDisplayString.append(getName()); String pattern = if (pattern
544
- * != null && pattern.length() > 0) { String displayString =
564
+ * boolean validated = isMatchWord(document, offset, getReplacementString()); if
565
+ * (validated) { StyledString styledDisplayString = new StyledString();
566
+ * styledDisplayString.append(getName()); String pattern = if (pattern != null
567
+ * && pattern.length() > 0) { String displayString =
545
568
* styledDisplayString.getString(); int[] bestSequence =
546
- * getMatcher().bestSubsequence(displayString, pattern); int
547
- * highlightAdjustment = 0; for (int index : bestSequence) {
548
- * styledDisplayString.setStyle(index + highlightAdjustment, 1, null); }
549
- * } }
569
+ * getMatcher().bestSubsequence(displayString, pattern); int highlightAdjustment
570
+ * = 0; for (int index : bestSequence) { styledDisplayString.setStyle(index +
571
+ * highlightAdjustment, 1, null); } } }
550
572
*
551
- * // if (fUpdateLengthOnValidate && event != null) { //
552
- * replacementLength += event.fText.length() - event.fLength; // adjust
553
- * // the // replacement // length // by // the // event's // text //
554
- * replacement // } return validated;
573
+ * // if (fUpdateLengthOnValidate && event != null) { // replacementLength +=
574
+ * event.fText.length() - event.fLength; // adjust // the // replacement //
575
+ * length // by // the // event's // text // replacement // } return validated;
555
576
*/
556
577
}
557
578
@@ -587,17 +608,16 @@ public StyledString getStyledDisplayString() {
587
608
}
588
609
589
610
/**
590
- * Computes the token at the given <code>offset</code> in
591
- * <code>document</code> to emphasize the ranges matching this token in
592
- * proposal's display string.
611
+ * Computes the token at the given <code>offset</code> in <code>document</code>
612
+ * to emphasize the ranges matching this token in proposal's display string.
593
613
*
594
614
* @param document
595
615
* the document where content assist is invoked
596
616
* @param offset
597
617
* the offset in the document at current caret location
598
- * @return the token at the given <code>offset</code> in
599
- * <code>document</code> to be used for emphasizing matching ranges
600
- * in proposal's display string
618
+ * @return the token at the given <code>offset</code> in <code>document</code>
619
+ * to be used for emphasizing matching ranges in proposal's display
620
+ * string
601
621
* @since 3.12
602
622
*/
603
623
protected String getPatternToEmphasizeMatch (IDocument document , int offset ) {
@@ -621,8 +641,7 @@ private void ensurePositionCategoryInstalled(final IDocument document, LinkedMod
621
641
model .addLinkingListener (new ILinkedModeListener () {
622
642
623
643
/*
624
- * @see
625
- * org.eclipse.jface.text.link.ILinkedModeListener#left(org.
644
+ * @see org.eclipse.jface.text.link.ILinkedModeListener#left(org.
626
645
* eclipse.jface.text.link.LinkedModeModel, int)
627
646
*/
628
647
public void left (LinkedModeModel environment , int flags ) {
0 commit comments