Skip to content

Commit f1d51d2

Browse files
author
angelozerr
committed
Generate parameter name when completion is applyed for function. See
#37
1 parent 7f48afd commit f1d51d2

31 files changed

+1084
-102
lines changed

core/ts.core/src/ts/client/completions/AbstractCompletionCollector.java

+15-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
import ts.client.AbstractTypeScriptCollector;
1414
import ts.client.ITypeScriptServiceClient;
15+
import ts.internal.matcher.LCSS;
16+
import ts.utils.StringUtils;
1517

1618
public abstract class AbstractCompletionCollector extends AbstractTypeScriptCollector
1719
implements ITypeScriptCompletionCollector {
@@ -27,11 +29,18 @@ public AbstractCompletionCollector(String prefix, ICompletionEntryMatcher matche
2729
@Override
2830
public void addCompletionEntry(String name, String kind, String kindModifiers, String sortText, String fileName,
2931
int line, int offset, ITypeScriptServiceClient client) {
30-
if (matcher.match(name, prefix)) {
31-
doAddCompletionEntry(name, kind, kindModifiers, sortText, fileName, line, offset, client);
32-
}
32+
String prefix = getPrefix();
33+
ICompletionEntry entry = createEntry(name, kind, kindModifiers, sortText, fileName, line, offset, client);
34+
if (entry.updatePrefix(prefix)) {
35+
addCompletionEntry(entry);
36+
}
3337
}
3438

39+
protected abstract ICompletionEntry createEntry(String name, String kind, String kindModifiers, String sortText,
40+
String fileName, int line, int offset, ITypeScriptServiceClient client);
41+
42+
protected abstract void addCompletionEntry(ICompletionEntry entry);
43+
3544
public String getPrefix() {
3645
return prefix;
3746
}
@@ -40,6 +49,7 @@ public ICompletionEntryMatcher getMatcher() {
4049
return matcher;
4150
}
4251

43-
protected abstract void doAddCompletionEntry(String name, String kind, String kindModifiers, String sortText,
44-
String fileName, int line, int offset, ITypeScriptServiceClient client);
52+
// protected abstract void doAddCompletionEntry(String name, String kind, String kindModifiers, String sortText,
53+
// String fileName, int line, int offset, int[] bestSubsequence, int relevance,
54+
// ITypeScriptServiceClient client);
4555
}

core/ts.core/src/ts/client/completions/CompletionEntry.java

+56-4
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,17 @@
1212

1313
import ts.TypeScriptException;
1414
import ts.client.ITypeScriptServiceClient;
15+
import ts.internal.matcher.LCSS;
16+
import ts.utils.StringUtils;
1517

1618
public class CompletionEntry implements ICompletionEntry, ITypeScriptCompletionEntryDetailsCollector {
1719

20+
// Negative value ensures subsequence matches have a lower relevance than
21+
// standard JDT or template proposals
22+
private static final int SUBWORDS_RANGE_START = -9000;
23+
24+
private static final int minPrefixLengthForTypes = 1;
25+
1826
private final String name;
1927
private final String kind;
2028
private final String kindModifiers;
@@ -23,21 +31,22 @@ public class CompletionEntry implements ICompletionEntry, ITypeScriptCompletionE
2331
private final String fileName;
2432
private final int line;
2533
private final int offset;
26-
private final ITypeScriptServiceClient client;
2734
private CompletionEntryDetails entryDetails;
28-
private ICompletionEntryMatcher matcher;
35+
private final ICompletionEntryMatcher matcher;
36+
private int relevance;
37+
private final ITypeScriptServiceClient client;
2938

3039
public CompletionEntry(String name, String kind, String kindModifiers, String sortText, String fileName, int line,
31-
int offset, ITypeScriptServiceClient client, ICompletionEntryMatcher matcher) {
40+
int offset, ICompletionEntryMatcher matcher, ITypeScriptServiceClient client) {
3241
this.name = name;
3342
this.kind = kind;
3443
this.kindModifiers = kindModifiers;
3544
this.sortText = sortText;
3645
this.fileName = fileName;
3746
this.line = line;
3847
this.offset = offset;
39-
this.client = client;
4048
this.matcher = matcher;
49+
this.client = client;
4150
}
4251

4352
@Override
@@ -86,4 +95,47 @@ public void addDocumentation(String text, String kind) {
8695
public ICompletionEntryMatcher getMatcher() {
8796
return matcher;
8897
}
98+
99+
@Override
100+
public int getRelevance() {
101+
return relevance;
102+
}
103+
104+
@Override
105+
public boolean updatePrefix(String prefix) {
106+
Integer relevanceBoost = null;
107+
int[] bestSequence = null;
108+
if (StringUtils.isEmpty(prefix)) {
109+
relevanceBoost = 0;
110+
} else {
111+
bestSequence = matcher.bestSubsequence(name, prefix);
112+
if ((bestSequence != null && bestSequence.length > 0)) {
113+
relevanceBoost = 0;
114+
if (name.equals(prefix)) {
115+
if (minPrefixLengthForTypes < prefix.length()) {
116+
relevanceBoost = 16 * (RelevanceConstants.R_EXACT_NAME + RelevanceConstants.R_CASE);
117+
}
118+
} else if (name.equalsIgnoreCase(prefix)) {
119+
if (minPrefixLengthForTypes < prefix.length()) {
120+
relevanceBoost = 16 * RelevanceConstants.R_EXACT_NAME;
121+
}
122+
} else if (startsWithIgnoreCase(prefix, name)) {
123+
// Don't adjust score
124+
} else {
125+
int score = LCSS.scoreSubsequence(bestSequence);
126+
relevanceBoost = SUBWORDS_RANGE_START + score;
127+
}
128+
129+
}
130+
}
131+
if (relevanceBoost != null) {
132+
relevance = relevanceBoost;
133+
return true;
134+
}
135+
return false;
136+
}
137+
138+
private boolean startsWithIgnoreCase(String prefix, String name) {
139+
return prefix.toUpperCase().startsWith(name.toUpperCase());
140+
}
89141
}

core/ts.core/src/ts/client/completions/CompletionInfo.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@ public CompletionInfo(String prefix, ICompletionEntryMatcher matcher) {
2929
}
3030

3131
@Override
32-
protected void doAddCompletionEntry(String name, String kind, String kindModifiers, String sortText,
32+
protected ICompletionEntry createEntry(String name, String kind, String kindModifiers, String sortText,
3333
String fileName, int line, int offset, ITypeScriptServiceClient client) {
34-
entries.add(
35-
new CompletionEntry(name, kind, kindModifiers, sortText, fileName, line, offset, client, getMatcher()));
34+
return new CompletionEntry(name, kind, kindModifiers, sortText, fileName, line, offset, getMatcher(), client);
35+
}
36+
37+
@Override
38+
protected void addCompletionEntry(ICompletionEntry entry) {
39+
entries.add(entry);
3640
}
3741

3842
@Override

core/ts.core/src/ts/client/completions/ICompletionEntry.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,25 @@
1212

1313
import ts.client.IKindProvider;
1414

15-
public interface ICompletionEntry extends IKindProvider{
15+
public interface ICompletionEntry extends IKindProvider {
1616

1717
ICompletionEntry[] EMPTY_ENTRIES = new ICompletionEntry[0];
1818

1919
String getName();
2020

2121
String getSortText();
22+
23+
/**
24+
* Returns the relevance of this completion proposal.
25+
* <p>
26+
* The relevance is used to determine if this proposal is more relevant than
27+
* another proposal.
28+
* </p>
29+
*
30+
* @return the relevance of this completion proposal in the range of [0,
31+
* 100]
32+
*/
33+
int getRelevance();
34+
35+
boolean updatePrefix(String prefix);
2236
}

core/ts.core/src/ts/client/completions/ICompletionEntryDetails.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@
1212

1313
import java.util.List;
1414

15-
public interface ICompletionEntryDetails {
15+
import ts.client.IKindProvider;
1616

17-
String getName();
18-
19-
String getKind();
17+
public interface ICompletionEntryDetails extends IKindProvider {
2018

21-
String getKindModifiers();
19+
String getName();
2220

2321
List<SymbolDisplayPart> getDisplayParts();
2422

core/ts.core/src/ts/client/completions/ICompletionEntryMatcher.java

+4-21
Original file line numberDiff line numberDiff line change
@@ -20,40 +20,23 @@ public interface ICompletionEntryMatcher {
2020

2121
public static ICompletionEntryMatcher LCS = new ICompletionEntryMatcher() {
2222

23-
@Override
24-
public boolean match(String completion, String token) {
25-
return LCSS.containsSubsequence(completion, token);
26-
}
27-
2823
@Override
2924
public int[] bestSubsequence(String completion, String token) {
3025
return LCSS.bestSubsequence(completion, token);
3126
}
27+
3228
};
3329
public static ICompletionEntryMatcher START_WITH_MATCHER = new ICompletionEntryMatcher() {
3430

35-
@Override
36-
public boolean match(String completion, String token) {
37-
return completion.startsWith(token);
38-
}
39-
4031
@Override
4132
public int[] bestSubsequence(String completion, String token) {
33+
if (!completion.startsWith(token)) {
34+
return null;
35+
}
4236
return new int[] { 0, token.length() - 1 };
4337
}
4438
};
4539

46-
/**
47-
* Returns true if the given completion entry name match the given token and
48-
* false otherwise.
49-
*
50-
* @param completion
51-
* @param token
52-
* @return true if the given completion entry name match the given token and
53-
* false otherwise.
54-
*/
55-
boolean match(String completion, String token);
56-
5740
int[] bestSubsequence(String completion, String token);
5841

5942
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package ts.client.completions;
2+
3+
public interface RelevanceConstants {
4+
5+
public static final int R_EXACT_NAME = 4;
6+
7+
public static final int R_CASE = 10;
8+
}

eclipse/jsdt/ts.eclipse.ide.jsdt.core/META-INF/MANIFEST.MF

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ Require-Bundle: org.eclipse.core.runtime,
1616
org.eclipse.wst.xml.core,
1717
org.eclipse.wst.sse.ui
1818
Bundle-ActivationPolicy: lazy
19-
Bundle-Activator: ts.eclipse.ide.jsdt.internal.core.JSDTTypeScriptCorePlugin
19+
Bundle-Activator: ts.eclipse.ide.jsdt.core.JSDTTypeScriptCorePlugin
2020
Import-Package: com.eclipsesource.json;version="[0.9.4,0.9.5)"
21+
Export-Package: ts.eclipse.ide.jsdt.core
+10-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
* Contributors:
99
* Angelo Zerr <[email protected]> - initial API and implementation
1010
*/
11-
package ts.eclipse.ide.jsdt.internal.core;
11+
package ts.eclipse.ide.jsdt.core;
1212

13+
import org.eclipse.core.runtime.Platform;
1314
import org.eclipse.core.runtime.Plugin;
1415
import org.osgi.framework.BundleContext;
1516

@@ -24,6 +25,8 @@ public class JSDTTypeScriptCorePlugin extends Plugin {
2425
// The shared instance
2526
private static JSDTTypeScriptCorePlugin plugin;
2627

28+
private boolean isJSDT2;
29+
2730
/**
2831
* The constructor
2932
*/
@@ -39,6 +42,8 @@ public JSDTTypeScriptCorePlugin() {
3942
public void start(BundleContext context) throws Exception {
4043
super.start(context);
4144
plugin = this;
45+
//Platform.getPlugin("org.eclipse.wst.jsdt.core")
46+
isJSDT2 = Platform.getBundle("org.eclipse.wst.jsdt.core").getVersion().toString().startsWith("2.0.0");
4247
}
4348

4449
/*
@@ -61,4 +66,8 @@ public static JSDTTypeScriptCorePlugin getDefault() {
6166
return plugin;
6267
}
6368

69+
public boolean isJSDT2() {
70+
return isJSDT2;
71+
}
72+
6473
}

eclipse/jsdt/ts.eclipse.ide.jsdt.core/src/ts/eclipse/ide/jsdt/internal/core/Trace.java renamed to eclipse/jsdt/ts.eclipse.ide.jsdt.core/src/ts/eclipse/ide/jsdt/core/Trace.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Contributors:
99
* Angelo Zerr <[email protected]> - initial API and implementation
1010
*/
11-
package ts.eclipse.ide.jsdt.internal.core;
11+
package ts.eclipse.ide.jsdt.core;
1212

1313
import java.text.SimpleDateFormat;
1414
import java.util.Date;

eclipse/jsdt/ts.eclipse.ide.jsdt.ui/META-INF/MANIFEST.MF

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ Require-Bundle: org.eclipse.core.runtime,
2929
org.eclipse.debug.ui,
3030
org.eclipse.wst.jsdt.debug.ui,
3131
org.eclipse.wst.jsdt.debug.core,
32-
org.eclipse.ui.views
32+
org.eclipse.ui.views,
33+
org.eclipse.core.filesystem
3334
Bundle-ActivationPolicy: lazy
3435
Bundle-Activator: ts.eclipse.ide.jsdt.internal.ui.JSDTTypeScriptUIPlugin
3536
Import-Package: com.eclipsesource.json;version="[0.9.4,0.9.5)"

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

+16-9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.List;
1717
import java.util.Map;
1818

19+
import org.eclipse.core.filesystem.IFileStore;
1920
import org.eclipse.core.resources.IResource;
2021
import org.eclipse.core.runtime.CoreException;
2122
import org.eclipse.core.runtime.IProgressMonitor;
@@ -68,7 +69,6 @@
6869
import ts.client.Location;
6970
import ts.client.navbar.NavigationBarItem;
7071
import ts.client.occurrences.ITypeScriptOccurrencesCollector;
71-
import ts.eclipse.ide.core.resources.IIDETypeScriptFile;
7272
import ts.eclipse.ide.core.resources.IIDETypeScriptProject;
7373
import ts.eclipse.ide.core.utils.TypeScriptResourceUtil;
7474
import ts.eclipse.ide.jsdt.internal.ui.Trace;
@@ -541,10 +541,10 @@ class OccurrencesCollector
541541
private ITextSelection selection;
542542
private boolean canceled;
543543

544-
public OccurrencesCollector() {
544+
public OccurrencesCollector() {
545545
this.positions = new ArrayList<Position>();
546546
}
547-
547+
548548
public void setDocument(IDocument document) {
549549
this.document = document;
550550
}
@@ -626,13 +626,20 @@ private void updateOccurrenceAnnotations(ITextSelection selection) {
626626

627627
}
628628

629-
private IIDETypeScriptFile getTypeScriptFile(IDocument document) throws CoreException, TypeScriptException {
629+
private ITypeScriptFile getTypeScriptFile(IDocument document) throws CoreException, TypeScriptException {
630630
IResource file = EditorUtils.getResource(this);
631-
IIDETypeScriptProject tsProject = TypeScriptResourceUtil.getTypeScriptProject(file.getProject());
632-
return tsProject.openFile(file, document);
631+
if (file != null) {
632+
IIDETypeScriptProject tsProject = TypeScriptResourceUtil.getTypeScriptProject(file.getProject());
633+
return tsProject.openFile(file, document);
634+
}
635+
IFileStore fs = EditorUtils.getFileStore(this);
636+
if(fs != null) {
637+
// TODO
638+
}
639+
return null;
633640
}
634641

635-
public IIDETypeScriptFile getTypeScriptFile() throws CoreException, TypeScriptException {
642+
public ITypeScriptFile getTypeScriptFile() throws CoreException, TypeScriptException {
636643
final IDocument document = getSourceViewer().getDocument();
637644
if (document == null) {
638645
return null;
@@ -774,7 +781,7 @@ public TypeScriptContentOutlinePage getOutlinePage() {
774781
return contentOutlinePage;
775782
}
776783

777-
private void setOutlinePageInput(IIDETypeScriptFile tsFile) {
784+
private void setOutlinePageInput(ITypeScriptFile tsFile) {
778785
// try {
779786
contentOutlinePage.setInput(tsFile);
780787
// } catch (TypeScriptException e) {
@@ -854,7 +861,7 @@ private void setSelection(NavigationBarItem reference, boolean moveCursor) {
854861
if (start == null || end == null)
855862
return;
856863

857-
IIDETypeScriptFile tsFile = getTypeScriptFile();
864+
ITypeScriptFile tsFile = getTypeScriptFile();
858865

859866
int offset = tsFile.getPosition(start);
860867
int length = tsFile.getPosition(end) - offset;

0 commit comments

Comments
 (0)