Skip to content

Commit

Permalink
Allow go-to-declaration to go to an unindexed file
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffrey-easyesi committed Jul 13, 2016
1 parent 9f83067 commit ce5d3ad
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
2 changes: 1 addition & 1 deletion manifest.mf
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ AutoUpdate-Show-In-Client: true
OpenIDE-Module: netbeanstypescript
OpenIDE-Module-Layer: netbeanstypescript/resources/layer.xml
OpenIDE-Module-Localizing-Bundle: netbeanstypescript/Bundle.properties
OpenIDE-Module-Specification-Version: 1.8.10.1
OpenIDE-Module-Specification-Version: 1.8.10.2
2 changes: 1 addition & 1 deletion src/netbeanstypescript/TSDeclarationFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public DeclarationLocation findDeclaration(ParserResult info, int caretOffset) {
}
for (final JSONObject def: (List<JSONObject>) defs) {
final String destFileName = (String) def.get("fileName");
FileObject destFileObj = TSService.findFileObject(destFileName);
FileObject destFileObj = TSService.findAnyFileObject(destFileName);
if (destFileObj == null) {
return DeclarationLocation.NONE;
}
Expand Down
4 changes: 2 additions & 2 deletions src/netbeanstypescript/TSRefactoring.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class Plugin implements RefactoringPlugin {
final int lineStart = ((Number) use.get("lineStart")).intValue();
final String lineText = (String) use.get("lineText");

final FileObject useFileObj = TSService.findFileObject((String) use.get("fileName"));
final FileObject useFileObj = TSService.findIndexedFileObject((String) use.get("fileName"));
if (useFileObj == null) {
continue;
}
Expand Down Expand Up @@ -302,7 +302,7 @@ class Plugin implements RefactoringPlugin {
Problem firstProblem = null, lastProblem = null;
for (JSONObject loc: (List<JSONObject>) arr) {
String locFileName = (String) loc.get("fileName");
FileObject locFileObj = TSService.findFileObject(locFileName);
FileObject locFileObj = TSService.findIndexedFileObject(locFileName);
if (locFileObj == null) {
Problem p = new Problem(false, "Reference in unindexed file " + locFileName);
if (lastProblem == null) firstProblem = p; else lastProblem.setNext(p);
Expand Down
32 changes: 20 additions & 12 deletions src/netbeanstypescript/TSService.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import org.netbeans.modules.parsing.spi.indexing.ErrorsCache.Convertor;
import org.netbeans.modules.parsing.spi.indexing.Indexable;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.filesystems.URLMapper;
import org.openide.modules.InstalledFileLocator;
import org.openide.util.RequestProcessor;
Expand Down Expand Up @@ -104,6 +105,15 @@ static void stringToJS(StringBuilder sb, CharSequence s) {
sb.append('"');
}

static final String builtinLibPrefix = "(builtin) ";
static final Map<String, FileObject> builtinLibs = new HashMap<>();
static {
for (String lib: new String[] { "lib.d.ts", "lib.es6.d.ts" }) {
URL libURL = TSService.class.getClassLoader().getResource("netbeanstypescript/resources/" + lib);
builtinLibs.put(builtinLibPrefix + lib, URLMapper.findFileObject(libURL));
}
}

// All access to the TSService state below should be done with this lock acquired. This lock
// has a fair ordering policy so error checking won't starve other user actions.
private static final Lock lock = new ReentrantLock(true);
Expand All @@ -116,8 +126,6 @@ private static class NodeJSProcess {
OutputStream stdin;
BufferedReader stdout;
String error;
static final String builtinLibPrefix = "(builtin) ";
Map<String, FileObject> builtinLibs = new HashMap<>();
int nextProgId = 0;

NodeJSProcess() throws Exception {
Expand All @@ -144,15 +152,12 @@ private static class NodeJSProcess {
}

StringBuilder initLibs = new StringBuilder();
for (String lib: new String[] { "lib.d.ts", "lib.es6.d.ts" }) {
for (Map.Entry<String, FileObject> lib: builtinLibs.entrySet()) {
initLibs.append("void(builtinLibs[");
stringToJS(initLibs, builtinLibPrefix + lib);
stringToJS(initLibs, lib.getKey());
initLibs.append("]=");
URL libURL = TSService.class.getClassLoader().getResource("netbeanstypescript/resources/" + lib);
FileObject libObj = URLMapper.findFileObject(libURL);
stringToJS(initLibs, Source.create(libObj).createSnapshot().getText());
stringToJS(initLibs, Source.create(lib.getValue()).createSnapshot().getText());
initLibs.append(");");
builtinLibs.put(builtinLibPrefix + lib, libObj);
}
eval(initLibs.append('\n').toString());
}
Expand Down Expand Up @@ -484,16 +489,19 @@ static Object call(String method, FileObject fileObj, Object... args) {
}
}

static FileObject findFileObject(String path) {
static FileObject findIndexedFileObject(String path) {
lock.lock();
try {
if (path.startsWith(NodeJSProcess.builtinLibPrefix)) {
return nodejs != null ? nodejs.builtinLibs.get(path) : null;
}
FileData fd = allFiles.get(path);
return fd != null ? fd.fileObject : null;
} finally {
lock.unlock();
}
}

static FileObject findAnyFileObject(String path) {
return path.startsWith(builtinLibPrefix)
? builtinLibs.get(path)
: FileUtil.toFileObject(new File(path));
}
}

0 comments on commit ce5d3ad

Please sign in to comment.