Skip to content

Commit 3fcdb23

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

File tree

8 files changed

+50
-31
lines changed

8 files changed

+50
-31
lines changed

Diff for: eclipse/jsdt/ts.eclipse.ide.jsdt.ui/src/ts/eclipse/ide/jsdt/internal/ui/actions/RenameTypeScriptElementAction.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
import ts.client.quickinfo.QuickInfo;
2020
import ts.eclipse.ide.core.TypeScriptCorePlugin;
2121
import ts.eclipse.ide.core.preferences.TypeScriptCorePreferenceConstants;
22-
import ts.eclipse.ide.core.utils.DocumentUtils;
2322
import ts.eclipse.ide.jsdt.internal.ui.editor.TypeScriptEditor;
2423
import ts.eclipse.ide.jsdt.internal.ui.refactoring.RefactoringMessages;
2524
import ts.eclipse.ide.jsdt.internal.ui.refactoring.RenameLinkedMode;
2625
import ts.eclipse.ide.jsdt.internal.ui.refactoring.RenameSupport;
26+
import ts.eclipse.jface.text.DocumentUtils;
2727
import ts.resources.ITypeScriptFile;
2828

2929
public class RenameTypeScriptElementAction extends SelectionDispatchAction {

Diff for: eclipse/jsdt/ts.eclipse.ide.jsdt.ui/src/ts/eclipse/ide/jsdt/internal/ui/editor/TypeScriptDocumentProvider.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@
4848
import ts.eclipse.ide.core.TypeScriptCorePlugin;
4949
import ts.eclipse.ide.core.resources.IIDETypeScriptFile;
5050
import ts.eclipse.ide.core.resources.IIDETypeScriptProject;
51-
import ts.eclipse.ide.core.utils.DocumentUtils;
5251
import ts.eclipse.ide.core.utils.TypeScriptResourceUtil;
5352
import ts.eclipse.ide.jsdt.internal.ui.JSDTTypeScriptUIPlugin;
5453
import ts.eclipse.ide.ui.TypeScriptUIPlugin;
5554
import ts.eclipse.ide.ui.preferences.TypeScriptUIPreferenceConstants;
55+
import ts.eclipse.jface.text.DocumentUtils;
5656

5757
public class TypeScriptDocumentProvider extends TextFileDocumentProvider {
5858

Diff for: eclipse/jsdt/ts.eclipse.ide.jsdt.ui/src/ts/eclipse/ide/jsdt/internal/ui/editor/TypeScriptEditor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@
9292
import ts.eclipse.ide.core.TypeScriptCorePlugin;
9393
import ts.eclipse.ide.core.preferences.TypeScriptCorePreferenceConstants;
9494
import ts.eclipse.ide.core.resources.IIDETypeScriptProject;
95-
import ts.eclipse.ide.core.utils.DocumentUtils;
9695
import ts.eclipse.ide.core.utils.TypeScriptResourceUtil;
9796
import ts.eclipse.ide.jsdt.internal.ui.JSDTTypeScriptUIMessages;
9897
import ts.eclipse.ide.jsdt.internal.ui.Trace;
@@ -107,6 +106,7 @@
107106
import ts.eclipse.ide.ui.outline.IEditorOutlineFeatures;
108107
import ts.eclipse.ide.ui.outline.TypeScriptContentOutlinePage;
109108
import ts.eclipse.ide.ui.utils.EditorUtils;
109+
import ts.eclipse.jface.text.DocumentUtils;
110110
import ts.resources.ITypeScriptFile;
111111
import ts.utils.CompletableFutureUtils;
112112

Diff for: eclipse/jsdt/ts.eclipse.ide.jsdt.ui/src/ts/eclipse/ide/jsdt/internal/ui/editor/format/TypeScriptContentFormatter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424

2525
import ts.eclipse.ide.core.resources.IIDETypeScriptFile;
2626
import ts.eclipse.ide.core.resources.IIDETypeScriptProject;
27-
import ts.eclipse.ide.core.utils.DocumentUtils;
2827
import ts.eclipse.ide.core.utils.TypeScriptResourceUtil;
2928
import ts.eclipse.ide.jsdt.internal.ui.JSDTTypeScriptUIMessages;
3029
import ts.eclipse.ide.jsdt.internal.ui.JSDTTypeScriptUIPlugin;
30+
import ts.eclipse.jface.text.DocumentUtils;
3131

3232
/**
3333
* Content formatter which consumes tsserver "format" command to format a

Diff for: eclipse/ts.eclipse.ide.core/src/ts/eclipse/ide/internal/core/resources/IDETypeScriptFile.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
import ts.client.Location;
2121
import ts.eclipse.ide.core.resources.IIDETypeScriptFile;
2222
import ts.eclipse.ide.core.resources.IIDETypeScriptProject;
23-
import ts.eclipse.ide.core.utils.DocumentUtils;
2423
import ts.eclipse.ide.core.utils.TypeScriptResourceUtil;
2524
import ts.eclipse.ide.core.utils.WorkbenchResourceUtil;
25+
import ts.eclipse.jface.text.DocumentUtils;
2626
import ts.resources.AbstractTypeScriptFile;
2727
import ts.resources.SynchStrategy;
2828

Diff for: eclipse/ts.eclipse.ide.ui/src/ts/eclipse/ide/ui/utils/EditorUtils.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@
3434
import ts.client.TextSpan;
3535
import ts.client.codefixes.FileCodeEdits;
3636
import ts.client.navbar.NavigationBarItem;
37-
import ts.eclipse.ide.core.utils.DocumentUtils;
3837
import ts.eclipse.ide.core.utils.TypeScriptResourceUtil;
3938
import ts.eclipse.ide.core.utils.WorkbenchResourceUtil;
4039
import ts.eclipse.ide.ui.TypeScriptUIPlugin;
40+
import ts.eclipse.jface.text.DocumentUtils;
4141
import ts.utils.StringUtils;
4242

4343
public class EditorUtils {

Diff for: 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

Diff for: 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)