Skip to content

Commit f014b39

Browse files
committed
Use "Salsa" preferences to know if completion, hyperlink, validation,
hover must consume tsserver when JS file is edited. See #12
1 parent f1f5d98 commit f014b39

25 files changed

+434
-103
lines changed

core/ts.core/src/ts/internal/resources/DefaultTypeScriptResourcesManager.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,17 @@ public ITypeScriptProject getTypeScriptProject(Object project, boolean force) th
3939
}
4040

4141
@Override
42-
public boolean isTSFile(Object fileObject) {
42+
public boolean isTsFile(Object fileObject) {
4343
String ext = getExtension(fileObject);
4444
return ext != null && FileUtils.TS_EXTENSION.equals(ext.toLowerCase());
4545
}
4646

47+
@Override
48+
public boolean isJsFile(Object fileObject) {
49+
String ext = getExtension(fileObject);
50+
return ext != null && FileUtils.JS_EXTENSION.equals(ext.toLowerCase());
51+
}
52+
4753
protected String getExtension(Object fileObject) {
4854
if (fileObject instanceof File) {
4955
return FileUtils.getFileExtension(((File) fileObject).getName());

core/ts.core/src/ts/resources/ConfigurableTypeScriptResourcesManager.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ public void setTypeScriptResourcesManagerDelegate(
2424
}
2525

2626
public boolean isTSFile(Object fileObject) {
27-
return typeScriptResourcesManagerDelegate.isTSFile(fileObject);
27+
return typeScriptResourcesManagerDelegate.isTsFile(fileObject);
28+
}
29+
30+
public boolean isJavaScriptFile(Object fileObject) {
31+
return typeScriptResourcesManagerDelegate.isJsFile(fileObject);
2832
}
29-
3033
}

core/ts.core/src/ts/resources/ITypeScriptResourcesManagerDelegate.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ public interface ITypeScriptResourcesManagerDelegate {
2626
* @return true if the given file object is a TypeScript file and false
2727
* otherwise.
2828
*/
29-
boolean isTSFile(Object fileObject);
29+
boolean isTsFile(Object fileObject);
3030

31+
/**
32+
* Returns true if the given file object is a JavaScript file and false
33+
* otherwise.
34+
*
35+
* @param fileObject
36+
* @return true if the given file object is a JavaScript file and false
37+
* otherwise.
38+
*/
39+
boolean isJsFile(Object fileObject);
3140
}

core/ts.core/src/ts/resources/TypeScriptResourcesManager.java

+4
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,8 @@ public static ITypeScriptProject getTypeScriptProject(Object project, boolean fo
6666
public static boolean isTSFile(Object fileObject) {
6767
return INSTANCE.isTSFile(fileObject);
6868
}
69+
70+
public static boolean isJavaScriptFile(Object fileObject) {
71+
return INSTANCE.isTSFile(fileObject);
72+
}
6973
}

core/ts.core/src/ts/utils/FileUtils.java

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ public class FileUtils {
2626
public static final String JSX_EXTENSION = "jsx";
2727
public static final String TSX_EXTENSION = "tsx";
2828

29+
/**
30+
* Configuration file
31+
*/
32+
public static final String TSCONFIG_JSON = "tsconfig.json";
33+
public static final String JSCONFIG_JSON = "jsconfig.json";
34+
2935
public static String getFileExtension(String fileName) {
3036
int index = fileName.lastIndexOf('.');
3137
if (index == -1)

eclipse/jsdt/ts.eclipse.ide.jsdt.ui/src/ts/eclipse/ide/jsdt/internal/ui/contentassist/TypeScriptCompletionProposalComputer.java

+17-12
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import ts.eclipse.ide.jsdt.internal.ui.Trace;
2929
import ts.eclipse.jface.text.contentassist.CompletionProposalCollector;
3030
import ts.resources.ITypeScriptFile;
31+
import ts.resources.TypeScriptResourcesManager;
32+
import ts.utils.FileUtils;
3133

3234
/**
3335
* JSDT completion proposal computer manage completion Proposal for Javascript
@@ -42,20 +44,23 @@ public List computeCompletionProposals(ContentAssistInvocationContext context, I
4244
if (context instanceof JavaContentAssistInvocationContext) {
4345
try {
4446
JavaContentAssistInvocationContext javaContext = (JavaContentAssistInvocationContext) context;
45-
IProject project = javaContext.getProject().getProject();
46-
IIDETypeScriptProject tsProject = TypeScriptCorePlugin.getTypeScriptProject(project);
47-
if (tsProject != null) {
47+
IResource resource = javaContext.getCompilationUnit().getResource();
48+
if (TypeScriptCorePlugin.canConsumeTsserver(resource)) {
49+
IProject project = resource.getProject();
50+
IIDETypeScriptProject tsProject = TypeScriptCorePlugin.getTypeScriptProject(project);
51+
if (tsProject != null) {
4852

49-
int position = javaContext.getInvocationOffset();
50-
IResource resource = javaContext.getCompilationUnit().getResource();
51-
IDocument document = javaContext.getDocument();
52-
ITypeScriptFile tsFile = tsProject.openFile(resource, document);
53-
CharSequence prefix = javaContext.computeIdentifierPrefix();
53+
int position = javaContext.getInvocationOffset();
5454

55-
CompletionProposalCollector collector = new CompletionProposalCollector(position,
56-
prefix != null ? prefix.toString() : null);
57-
tsFile.completions(position, collector);
58-
return collector.getProposals();
55+
IDocument document = javaContext.getDocument();
56+
ITypeScriptFile tsFile = tsProject.openFile(resource, document);
57+
CharSequence prefix = javaContext.computeIdentifierPrefix();
58+
59+
CompletionProposalCollector collector = new CompletionProposalCollector(position,
60+
prefix != null ? prefix.toString() : null);
61+
tsFile.completions(position, collector);
62+
return collector.getProposals();
63+
}
5964
}
6065
} catch (Exception e) {
6166
Trace.trace(Trace.SEVERE, "Error while TypeScript completion", e);

eclipse/jsdt/ts.eclipse.ide.jsdt.ui/src/ts/eclipse/ide/jsdt/internal/ui/validation/TypeScriptDocumentRegionProcessor.java

-25
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,10 @@
1111
package ts.eclipse.ide.jsdt.internal.ui.validation;
1212

1313
import org.eclipse.core.resources.IResource;
14-
import org.eclipse.core.runtime.CoreException;
1514
import org.eclipse.jface.text.IDocument;
16-
import org.eclipse.jface.text.reconciler.DirtyRegion;
1715
import org.eclipse.jface.text.reconciler.IReconcilingStrategy;
1816
import org.eclipse.wst.sse.ui.internal.reconcile.DocumentRegionProcessor;
1917

20-
import ts.eclipse.ide.core.TypeScriptCorePlugin;
21-
import ts.eclipse.ide.core.resources.IIDETypeScriptProject;
22-
import ts.resources.ITypeScriptFile;
2318
import ts.utils.FileUtils;
2419

2520
/**
@@ -32,11 +27,9 @@
3227
public class TypeScriptDocumentRegionProcessor extends DocumentRegionProcessor {
3328

3429
private final String contentType;
35-
private final IResource resource;
3630

3731
public TypeScriptDocumentRegionProcessor(IResource resource) {
3832
this.contentType = getContentType(resource);
39-
this.resource = resource;
4033
}
4134

4235
private String getContentType(IResource resource) {
@@ -63,22 +56,4 @@ protected String getContentType(IDocument doc) {
6356
return contentType;
6457
}
6558

66-
@Override
67-
protected void process(DirtyRegion dirtyRegion) {
68-
super.process(dirtyRegion);
69-
try {
70-
IIDETypeScriptProject tsProject = TypeScriptCorePlugin.getTypeScriptProject(resource.getProject());
71-
if (tsProject != null) {
72-
ITypeScriptFile tsFile = tsProject.openFile(resource, getDocument());
73-
int start = dirtyRegion.getOffset();
74-
int end = start + dirtyRegion.getLength() - 1;
75-
String newText = dirtyRegion.getText();
76-
// It doesn't works, why?
77-
// tsProject.changeFile(tsFile, start, end, newText);
78-
}
79-
} catch (Exception e) {
80-
e.printStackTrace();
81-
}
82-
}
83-
8459
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package ts.eclipse.ide.internal.core.resources;
2+
3+
import org.eclipse.core.resources.IProject;
4+
import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent;
5+
6+
import ts.TypeScriptException;
7+
import ts.eclipse.ide.core.TypeScriptCorePlugin;
8+
import ts.eclipse.ide.core.preferences.TypeScriptCorePreferenceConstants;
9+
import ts.eclipse.ide.core.resources.AbstractTypeScriptSettings;
10+
import ts.eclipse.ide.internal.core.Trace;
11+
12+
class IDEResourcesProjectStatus extends AbstractTypeScriptSettings {
13+
14+
private boolean typeScript;
15+
private boolean salsa;
16+
private IDETypeScriptProject tsProject;
17+
18+
public IDEResourcesProjectStatus(IProject project) {
19+
super(project, TypeScriptCorePlugin.PLUGIN_ID);
20+
setTypeScript(
21+
isMatchPaths(super.getStringPreferencesValue(TypeScriptCorePreferenceConstants.NATURE_TYPESCRIPT_PATHS,
22+
TypeScriptCorePreferenceConstants.DEFAULT_NATURE_TYPESCRIPT_PATHS)));
23+
setSalsa(isMatchPaths(super.getStringPreferencesValue(TypeScriptCorePreferenceConstants.NATURE_SALSA_PATHS,
24+
TypeScriptCorePreferenceConstants.DEFAULT_NATURE_SALSA_PATHS)));
25+
update();
26+
}
27+
28+
private void update() {
29+
if (tsProject != null) {
30+
try {
31+
tsProject.dispose();
32+
} catch (TypeScriptException e) {
33+
Trace.trace(Trace.SEVERE, "Error while disposing TypeScript project", e);
34+
}
35+
}
36+
if (isSalsa() || isTypeScript()) {
37+
this.tsProject = new IDETypeScriptProject(getProject());
38+
} else {
39+
this.tsProject = null;
40+
}
41+
}
42+
43+
boolean isTypeScript() {
44+
return typeScript;
45+
}
46+
47+
void setTypeScript(boolean typeScript) {
48+
this.typeScript = typeScript;
49+
}
50+
51+
boolean isSalsa() {
52+
return salsa;
53+
}
54+
55+
void setSalsa(boolean salsa) {
56+
this.salsa = salsa;
57+
}
58+
59+
IDETypeScriptProject getTypeScriptProject() {
60+
return tsProject;
61+
}
62+
63+
void setTypeScriptProject(IDETypeScriptProject tsProject) {
64+
this.tsProject = tsProject;
65+
}
66+
67+
@Override
68+
public void preferenceChange(PreferenceChangeEvent event) {
69+
if (TypeScriptCorePreferenceConstants.NATURE_TYPESCRIPT_PATHS.equals(event.getKey())) {
70+
setTypeScript(isMatchPaths(event.getNewValue().toString()));
71+
} else if (TypeScriptCorePreferenceConstants.NATURE_SALSA_PATHS.equals(event.getKey())) {
72+
setSalsa(isMatchPaths(event.getNewValue().toString()));
73+
}
74+
}
75+
76+
private boolean isMatchPaths(String paths) {
77+
String[] p = paths.split(",");
78+
for (int i = 0; i < p.length; i++) {
79+
if (getProject().getFile(p[i]).exists()) {
80+
return true;
81+
}
82+
}
83+
return false;
84+
}
85+
86+
}

eclipse/ts.eclipse.ide.core/src/ts/eclipse/ide/core/TypeScriptCorePlugin.java

+12-7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.eclipse.core.resources.ICommand;
1717
import org.eclipse.core.resources.IProject;
1818
import org.eclipse.core.resources.IProjectDescription;
19+
import org.eclipse.core.resources.IResource;
1920
import org.eclipse.core.runtime.CoreException;
2021
import org.eclipse.core.runtime.FileLocator;
2122
import org.eclipse.core.runtime.IStatus;
@@ -73,12 +74,7 @@ public static File getTypeScriptRepositoryBaseDir() throws IOException {
7374

7475
@Override
7576
public void stop(BundleContext context) throws Exception {
76-
// TSServerTypeManager.getManager().destroy();
77-
// TSNatureAdaptersManager.getManager().destroy();
78-
// TSFileConfigurationManager.getManager().destroy();
79-
// IDETSProjectSynchronizer.getInstance().dispose();
80-
// TSModuleInstallManager.getManager().destroy();
81-
// TSRepositoryManager.getManager().dispose();
77+
IDEResourcesManager.getInstance().dispose();
8278

8379
plugin = null;
8480
super.stop(context);
@@ -94,7 +90,15 @@ public void stop(BundleContext context) throws Exception {
9490
* false otherwise.
9591
*/
9692
public static boolean hasTypeScriptNature(IProject project) {
97-
return true; // return IDETSProject.hasTypeScriptNature(project);
93+
return IDEResourcesManager.getInstance().hasTypeScriptNature(project);
94+
}
95+
96+
public static boolean canConsumeTsserver(IProject project, Object fileObject) {
97+
return IDEResourcesManager.getInstance().canConsumeTsserver(project, fileObject);
98+
}
99+
100+
public static boolean canConsumeTsserver(IResource resource) {
101+
return canConsumeTsserver(resource.getProject(), resource);
98102
}
99103

100104
public static boolean hasTypeScriptBuilder(IProject project) {
@@ -197,4 +201,5 @@ public static INodejsInstallManager getNodejsInstallManager() {
197201
public static IIDETypeScriptRepositoryManager getTypeScriptRepositoryManager() {
198202
return IDETypeScriptRepositoryManager.INSTANCE;
199203
}
204+
200205
}

eclipse/ts.eclipse.ide.core/src/ts/eclipse/ide/core/preferences/TypeScriptCorePreferenceConstants.java

+17
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
*/
1111
package ts.eclipse.ide.core.preferences;
1212

13+
import static ts.utils.FileUtils.JSCONFIG_JSON;
14+
import static ts.utils.FileUtils.TSCONFIG_JSON;
15+
1316
/**
1417
* TypeScript preferences core constants.
1518
*
@@ -42,6 +45,20 @@ public class TypeScriptCorePreferenceConstants {
4245

4346
public static final String TSC_INSTALLED_TYPESCRIPT_PATH = "tscInstalledTypeScriptPath"; //$NON-NLS-1$
4447

48+
// Salsa
49+
50+
public static final String USE_SALSA_AS_JS_INFERENCE = "useSalsaAsJSInference"; //$NON-NLS-1$
51+
52+
// TypeScript/Salsa nature paths
53+
54+
// public static final String NATURE_TYPESCRIPT_PATHS = "natureTypescriptPaths"; //$NON-NLS-1$
55+
//
56+
// public static final String DEFAULT_NATURE_TYPESCRIPT_PATHS = TSCONFIG_JSON + ",src/" + TSCONFIG_JSON; //$NON-NLS-1$
57+
//
58+
// public static final String NATURE_SALSA_PATHS = "natureSalsaPaths"; //$NON-NLS-1$
59+
//
60+
// public static final String DEFAULT_NATURE_SALSA_PATHS = JSCONFIG_JSON + ",src/" + JSCONFIG_JSON; //$NON-NLS-1$
61+
4562
private TypeScriptCorePreferenceConstants() {
4663
}
4764
}

eclipse/ts.eclipse.ide.core/src/ts/eclipse/ide/core/resources/AbstractTypeScriptSettings.java

+8-13
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
*/
1111
package ts.eclipse.ide.core.resources;
1212

13+
import org.eclipse.core.resources.IProject;
1314
import org.eclipse.core.resources.ProjectScope;
14-
import org.eclipse.core.runtime.preferences.DefaultScope;
1515
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
1616
import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener;
1717
import org.eclipse.core.runtime.preferences.InstanceScope;
@@ -24,13 +24,13 @@
2424
*/
2525
public abstract class AbstractTypeScriptSettings implements IPreferenceChangeListener {
2626

27-
private final IIDETypeScriptProject tsProject;
27+
private final IProject project;
2828
private final ProjectScope projectScope;
2929
private final String pluginId;
3030

31-
public AbstractTypeScriptSettings(IIDETypeScriptProject tsProject, String pluginId) {
32-
this.tsProject = tsProject;
33-
this.projectScope = new ProjectScope(tsProject.getProject());
31+
public AbstractTypeScriptSettings(IProject project, String pluginId) {
32+
this.project = project;
33+
this.projectScope = new ProjectScope(project);
3434
this.pluginId = pluginId;
3535
getProjectPreferences().addPreferenceChangeListener(this);
3636
getWorkspacePreferences().addPreferenceChangeListener(this);
@@ -93,7 +93,7 @@ protected IEclipsePreferences getProjectPreferences() {
9393
}
9494

9595
protected IEclipsePreferences getWorkspacePreferences() {
96-
return getWorkspacePreferences(pluginId);
96+
return TypeScriptSettingsHelper.getWorkspacePreferences(pluginId);
9797
}
9898

9999
public void dispose() {
@@ -102,12 +102,7 @@ public void dispose() {
102102
InstanceScope.INSTANCE.getNode(pluginId).removePreferenceChangeListener(this);
103103
}
104104

105-
public IIDETypeScriptProject getTypeScriptProject() {
106-
return tsProject;
105+
public IProject getProject() {
106+
return project;
107107
}
108-
109-
public static IEclipsePreferences getWorkspacePreferences(String pluginId) {
110-
return DefaultScope.INSTANCE.getNode(pluginId);
111-
}
112-
113108
}

0 commit comments

Comments
 (0)