Skip to content

Commit c6834bd

Browse files
committed
Implement auto import. See
#22
1 parent 4d783ec commit c6834bd

File tree

2 files changed

+44
-25
lines changed

2 files changed

+44
-25
lines changed

eclipse/ts.eclipse.ide.core/src/ts/eclipse/ide/core/utils/DocumentUtils.java renamed to eclipse/ts.eclipse/src/ts/eclipse/jface/text/DocumentUtils.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Angelo Zerr <[email protected]> - initial API and implementation
1010
* Lorenzo Dalla Vecchia <[email protected]> - made toTextEdit public
1111
*/
12-
package ts.eclipse.ide.core.utils;
12+
package ts.eclipse.jface.text;
1313

1414
import java.util.List;
1515

eclipse/ts.eclipse/src/ts/eclipse/jface/text/contentassist/TypeScriptCompletionProposal.java

+43-24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ts.eclipse.jface.text.contentassist;
22

33
import java.util.List;
4+
import java.util.stream.Collectors;
45

56
import org.eclipse.jface.internal.text.html.BrowserInformationControl;
67
import org.eclipse.jface.text.BadLocationException;
@@ -35,15 +36,18 @@
3536
import org.eclipse.swt.graphics.Image;
3637
import org.eclipse.swt.graphics.Point;
3738
import org.eclipse.swt.widgets.Shell;
39+
import org.eclipse.text.edits.MalformedTreeException;
3840
import org.eclipse.ui.texteditor.link.EditorLinkedModeUI;
3941

4042
import ts.TypeScriptException;
4143
import ts.client.ITypeScriptServiceClient;
44+
import ts.client.codefixes.CodeAction;
4245
import ts.client.completions.CompletionEntry;
4346
import ts.client.completions.CompletionEntryDetails;
4447
import ts.client.completions.ICompletionEntryMatcher;
4548
import ts.client.completions.SymbolDisplayPart;
4649
import ts.eclipse.jface.images.TypeScriptImagesRegistry;
50+
import ts.eclipse.jface.text.DocumentUtils;
4751
import ts.eclipse.jface.text.HoverControlCreator;
4852
import ts.eclipse.jface.text.PresenterControlCreator;
4953
import ts.utils.StringUtils;
@@ -131,7 +135,28 @@ public void apply(IDocument document, char trigger, int offset) {
131135
// selected the proposal, and where the cursor offset is
132136
// but we might in the future...
133137
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;
135160

136161
if (arguments != null && !arguments.isEmpty() && getTextViewer() != null) {
137162
try {// adjust offset of the whole arguments
@@ -159,8 +184,7 @@ public void apply(IDocument document, char trigger, int offset) {
159184
model.forceInstall();
160185
/*
161186
* JavaEditor editor = getJavaEditor(); if (editor != null) {
162-
* model.addLinkingListener(new EditorHighlightingSynchronizer(
163-
* editor)); }
187+
* model.addLinkingListener(new EditorHighlightingSynchronizer( editor)); }
164188
*/
165189

166190
LinkedModeUI ui = new EditorLinkedModeUI(model, getTextViewer());
@@ -537,21 +561,18 @@ public boolean validate(IDocument document, int offset, DocumentEvent event) {
537561
/*
538562
*
539563
*
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 =
545568
* 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); } } }
550572
*
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;
555576
*/
556577
}
557578

@@ -587,17 +608,16 @@ public StyledString getStyledDisplayString() {
587608
}
588609

589610
/**
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.
593613
*
594614
* @param document
595615
* the document where content assist is invoked
596616
* @param offset
597617
* 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
601621
* @since 3.12
602622
*/
603623
protected String getPatternToEmphasizeMatch(IDocument document, int offset) {
@@ -621,8 +641,7 @@ private void ensurePositionCategoryInstalled(final IDocument document, LinkedMod
621641
model.addLinkingListener(new ILinkedModeListener() {
622642

623643
/*
624-
* @see
625-
* org.eclipse.jface.text.link.ILinkedModeListener#left(org.
644+
* @see org.eclipse.jface.text.link.ILinkedModeListener#left(org.
626645
* eclipse.jface.text.link.LinkedModeModel, int)
627646
*/
628647
public void left(LinkedModeModel environment, int flags) {

0 commit comments

Comments
 (0)