Skip to content

Commit 179a24b

Browse files
author
angelozerr
committed
Implement "Mark occurrences" by consumming tsserver "occurences"
command. See #17
1 parent 281fdd8 commit 179a24b

22 files changed

+803
-95
lines changed

core/ts.core/META-INF/MANIFEST.MF

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Export-Package: ts,
1616
ts.client.definition,
1717
ts.client.format,
1818
ts.client.geterr,
19+
ts.client.occurrences,
1920
ts.client.quickinfo,
2021
ts.client.references,
2122
ts.client.signaturehelp,

core/ts.core/src/ts/client/ITypeScriptServiceClient.java

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import ts.client.definition.ITypeScriptDefinitionCollector;
1717
import ts.client.format.ITypeScriptFormatCollector;
1818
import ts.client.geterr.ITypeScriptGeterrCollector;
19+
import ts.client.occurrences.ITypeScriptOccurrencesCollector;
1920
import ts.client.quickinfo.ITypeScriptQuickInfoCollector;
2021
import ts.client.references.ITypeScriptReferencesCollector;
2122
import ts.client.signaturehelp.ITypeScriptSignatureHelpCollector;
@@ -60,6 +61,9 @@ void format(String fileName, int line, int offset, int endLine, int endOffset, I
6061
void references(String fileName, int line, int offset, ITypeScriptReferencesCollector collector)
6162
throws TypeScriptException;
6263

64+
void occurrences(String fileName, int line, int offset, ITypeScriptOccurrencesCollector collector)
65+
throws TypeScriptException;
66+
6367
void join() throws InterruptedException;
6468

6569
void addClientListener(ITypeScriptClientListener listener);

core/ts.core/src/ts/client/TypeScriptServiceClient.java

+31
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import ts.client.definition.ITypeScriptDefinitionCollector;
3333
import ts.client.format.ITypeScriptFormatCollector;
3434
import ts.client.geterr.ITypeScriptGeterrCollector;
35+
import ts.client.occurrences.ITypeScriptOccurrencesCollector;
3536
import ts.client.quickinfo.ITypeScriptQuickInfoCollector;
3637
import ts.client.references.ITypeScriptReferencesCollector;
3738
import ts.client.signaturehelp.ITypeScriptSignatureHelpCollector;
@@ -46,6 +47,7 @@
4647
import ts.internal.client.protocol.DefinitionRequest;
4748
import ts.internal.client.protocol.FormatRequest;
4849
import ts.internal.client.protocol.GeterrRequest;
50+
import ts.internal.client.protocol.OccurrencesRequest;
4951
import ts.internal.client.protocol.OpenRequest;
5052
import ts.internal.client.protocol.QuickInfoRequest;
5153
import ts.internal.client.protocol.ReferencesRequest;
@@ -375,6 +377,35 @@ private void collectReferences(JsonObject response, ITypeScriptReferencesCollect
375377
}
376378
}
377379

380+
// ----------------- Occurrences
381+
382+
@Override
383+
public void occurrences(String fileName, int line, int offset,
384+
ITypeScriptOccurrencesCollector collector) throws TypeScriptException {
385+
OccurrencesRequest request = new OccurrencesRequest(fileName, line, offset);
386+
JsonObject response = execute(request, true, null).asObject();
387+
collectOccurrences(response, collector);
388+
}
389+
390+
private void collectOccurrences(JsonObject response, ITypeScriptOccurrencesCollector collector) throws TypeScriptException {
391+
JsonArray body = response.get("body").asArray();
392+
JsonObject occurrence = null;
393+
String file = null;
394+
JsonObject start = null;
395+
JsonObject end = null;
396+
boolean isWriteAccess = false;
397+
for (JsonValue b : body) {
398+
occurrence = b.asObject();
399+
file = occurrence.getString("file", null);
400+
start = occurrence.get("start").asObject();
401+
end = occurrence.get("end").asObject();
402+
isWriteAccess= occurrence.getBoolean("isWriteAccess", false);
403+
collector.addOccurrence(file, start.getInt("line", -1), start.getInt("offset", -1), end.getInt("line", -1),
404+
end.getInt("offset", -1), isWriteAccess);
405+
406+
}
407+
}
408+
378409
/**
379410
* Write the buffer of editor content to a temporary file and have the
380411
* server reload it
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ts.client.occurrences;
2+
3+
import ts.TypeScriptException;
4+
import ts.client.ITypeScriptCollector;
5+
6+
public interface ITypeScriptOccurrencesCollector extends ITypeScriptCollector {
7+
8+
void addOccurrence(String file, int startLine, int startOffset, int endLine, int endOffset, boolean isWriteAccess)
9+
throws TypeScriptException;
10+
11+
}

core/ts.core/src/ts/internal/client/protocol/CommandNames.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public enum CommandNames {
1818

1919
Open("open"), Close("close"), Change("change"), NavBar("navbar"), Completions(
2020
"completions"), CompletionEntryDetails("completionEntryDetails"), Reload("reload"), Definition(
21-
"definition"), SignatureHelp("signatureHelp"), QuickInfo(
22-
"quickinfo"), Geterr("geterr"), Format("format"), References("references");
21+
"definition"), SignatureHelp("signatureHelp"), QuickInfo("quickinfo"), Geterr(
22+
"geterr"), Format("format"), References("references"), Occurrences("occurrences");
2323

2424
private final String name;
2525

core/ts.core/src/ts/internal/client/protocol/GeterrRequest.java

-43
Original file line numberDiff line numberDiff line change
@@ -60,35 +60,6 @@ public int getDelay() {
6060
return delay;
6161
}
6262

63-
// public boolean handleResponse(String event, String file, JsonArray
64-
// diagnostics) {
65-
// JsonObject diagnostic = null;
66-
// String text = null;
67-
// JsonObject start = null;
68-
// JsonObject end = null;
69-
// for (JsonValue value : diagnostics) {
70-
// diagnostic = value.asObject();
71-
// text = diagnostic.getString("text", null);
72-
// start = diagnostic.get("start").asObject();
73-
// end = diagnostic.get("end").asObject();
74-
// collector.addDiagnostic(event, file, text, start.getInt("line", -1),
75-
// start.getInt("offset", -1),
76-
// end.getInt("line", -1), end.getInt("offset", -1));
77-
// }
78-
// Integer mask = stateFiles.get(file);
79-
// mask = mask.intValue() | ("syntaxDiag".equals(event) ? EVENT_SYNTAX_DIAG
80-
// : EVENT_SEMANTIC_DIAG);
81-
// if (mask == EVENT_FINAL) {
82-
// dispose(file);
83-
// return true;
84-
// } else {
85-
// synchronized (stateFiles) {
86-
// stateFiles.put(file, mask);
87-
// }
88-
// return false;
89-
// }
90-
// }
91-
9263
public void dispose(String file) {
9364
synchronized (stateFiles) {
9465
stateFiles.remove(file);
@@ -128,18 +99,4 @@ protected Object getResult() throws Exception {
12899
return result;
129100
}
130101

131-
// @Override
132-
// public JsonObject call() throws Exception {
133-
// while (!files.isEmpty()) {
134-
// synchronized (this) {
135-
// // wait for 200ms otherwise if we don't set ms, if completion is
136-
// // executed several times
137-
// // quickly (do Ctrl+Space every time), the Thread could be
138-
// // blocked? Why?
139-
// this.wait(5);
140-
// }
141-
// }
142-
// return null;
143-
// }
144-
145102
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright (c) 2015-2016 Angelo ZERR.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Angelo Zerr <[email protected]> - initial API and implementation
10+
*/
11+
package ts.internal.client.protocol;
12+
13+
/**
14+
* Get occurrences request; value of command field is "occurrences". Return
15+
* response giving spans that are relevant in the file at a given line and
16+
* column.
17+
*
18+
* @see https://github.com/Microsoft/TypeScript/blob/master/src/server/protocol.
19+
* d.ts
20+
*/
21+
public class OccurrencesRequest extends FileLocationRequest {
22+
23+
public OccurrencesRequest(String fileName, int line, int offset) {
24+
super(CommandNames.Occurrences, new FileLocationRequestArgs(fileName, line, offset));
25+
}
26+
27+
}

core/ts.core/src/ts/resources/AbstractTypeScriptFile.java

+11
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import ts.client.completions.ITypeScriptCompletionCollector;
1717
import ts.client.definition.ITypeScriptDefinitionCollector;
1818
import ts.client.format.ITypeScriptFormatCollector;
19+
import ts.client.occurrences.ITypeScriptOccurrencesCollector;
1920
import ts.client.references.ITypeScriptReferencesCollector;
2021
import ts.internal.LocationReader;
2122

@@ -122,6 +123,16 @@ public void references(int position, ITypeScriptReferencesCollector collector) t
122123
client.references(this.getName(), line, offset, collector);
123124
}
124125

126+
@Override
127+
public void occurrences(int position, ITypeScriptOccurrencesCollector collector) throws TypeScriptException {
128+
this.synch();
129+
ITypeScriptServiceClient client = tsProject.getClient();
130+
Location location = this.getLocation(position);
131+
int line = location.getLine();
132+
int offset = location.getOffset();
133+
client.occurrences(this.getName(), line, offset, collector);
134+
}
135+
125136
@Override
126137
public synchronized void synch() throws TypeScriptException {
127138
if (!isDirty()) {

core/ts.core/src/ts/resources/ITypeScriptFile.java

+9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import ts.client.completions.ITypeScriptCompletionCollector;
1616
import ts.client.definition.ITypeScriptDefinitionCollector;
1717
import ts.client.format.ITypeScriptFormatCollector;
18+
import ts.client.occurrences.ITypeScriptOccurrencesCollector;
1819
import ts.client.references.ITypeScriptReferencesCollector;
1920

2021
/**
@@ -123,4 +124,12 @@ public interface ITypeScriptFile {
123124
*/
124125
void references(int position, ITypeScriptReferencesCollector collector) throws TypeScriptException;
125126

127+
/**
128+
* Find occurrences of the given position.
129+
*
130+
* @param position
131+
* @param collector
132+
* @throws TypeScriptException
133+
*/
134+
void occurrences(int position, ITypeScriptOccurrencesCollector collector) throws TypeScriptException;
126135
}

core/ts.repository/node_modules/typescript/lib/tsc.js

+18-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)