|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-2017 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 | + */ |
1 | 11 | package ts.eclipse.ide.core.builder;
|
2 | 12 |
|
3 | 13 | import java.util.ArrayList;
|
|
14 | 24 | import org.eclipse.core.resources.IncrementalProjectBuilder;
|
15 | 25 | import org.eclipse.core.runtime.CoreException;
|
16 | 26 | import org.eclipse.core.runtime.IProgressMonitor;
|
| 27 | +import org.eclipse.core.runtime.IStatus; |
| 28 | +import org.eclipse.core.runtime.Status; |
17 | 29 |
|
18 | 30 | import ts.TypeScriptException;
|
19 | 31 | import ts.client.CommandNames;
|
20 |
| -import ts.eclipse.ide.core.resources.IIDETypeScriptFile; |
| 32 | +import ts.eclipse.ide.core.TypeScriptCorePlugin; |
21 | 33 | import ts.eclipse.ide.core.resources.IIDETypeScriptProject;
|
22 | 34 | import ts.eclipse.ide.core.resources.buildpath.ITsconfigBuildPath;
|
23 | 35 | import ts.eclipse.ide.core.resources.buildpath.ITypeScriptBuildPath;
|
24 | 36 | import ts.eclipse.ide.core.resources.jsconfig.IDETsconfigJson;
|
25 | 37 | import ts.eclipse.ide.core.utils.TypeScriptResourceUtil;
|
26 |
| -import ts.eclipse.ide.core.utils.WorkbenchResourceUtil; |
27 | 38 | import ts.eclipse.ide.internal.core.Trace;
|
28 | 39 |
|
29 | 40 | /**
|
30 |
| - * Builder to transpiles TypeScript files into JavaScript files and source map |
31 |
| - * if needed. |
| 41 | + * Builder to transpile TypeScript files into JavaScript files and source map if |
| 42 | + * needed. |
32 | 43 | *
|
33 | 44 | */
|
34 | 45 | public class TypeScriptBuilder extends IncrementalProjectBuilder {
|
35 | 46 |
|
36 | 47 | public static final String ID = "ts.eclipse.ide.core.typeScriptBuilder";
|
37 | 48 |
|
38 |
| -// private static final ITypeScriptDiagnosticsCollector DIAGNOSTICS_COLLECTOR = new ITypeScriptDiagnosticsCollector() { |
39 |
| -// |
40 |
| -// @Override |
41 |
| -// public void addDiagnostic(String event, String filename, String text, int startLine, int startOffset, |
42 |
| -// int endLine, int endOffset, String category, int code) { |
43 |
| -// try { |
44 |
| -// IFile f = WorkbenchResourceUtil.findFileFromWorkspace(filename); |
45 |
| -// if (f != null && f.exists()) { |
46 |
| -// TypeScriptResourceUtil.addTscMarker(f, text, IMarker.SEVERITY_ERROR, startLine); |
47 |
| -// } |
48 |
| -// } catch (CoreException e) { |
49 |
| -// TypeScriptCorePlugin.logError(e); |
50 |
| -// } |
51 |
| -// } |
52 |
| -// }; |
53 |
| - |
54 | 49 | @Override
|
55 | 50 | protected IProject[] build(int kind, Map<String, String> args, final IProgressMonitor monitor)
|
56 | 51 | throws CoreException {
|
@@ -90,6 +85,25 @@ private void fullBuild(IIDETypeScriptProject tsProject, IProgressMonitor monitor
|
90 | 85 |
|
91 | 86 | private void incrementalBuild(IIDETypeScriptProject tsProject, IResourceDelta delta, IProgressMonitor monitor)
|
92 | 87 | throws CoreException {
|
| 88 | + if (tsProject.canSupport(CommandNames.CompileOnSaveEmitFile)) { |
| 89 | + // compile with tsserver (since TypeScript 2.0.5) |
| 90 | + compileWithTsserver(tsProject, delta, monitor); |
| 91 | + } else { |
| 92 | + // compile with tsc (more slow than tsserver). |
| 93 | + compileWithTsc(tsProject, delta, monitor); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Compile files with tsc. |
| 99 | + * |
| 100 | + * @param tsProject |
| 101 | + * @param delta |
| 102 | + * @param monitor |
| 103 | + * @throws CoreException |
| 104 | + */ |
| 105 | + private void compileWithTsc(IIDETypeScriptProject tsProject, IResourceDelta delta, IProgressMonitor monitor) |
| 106 | + throws CoreException { |
93 | 107 |
|
94 | 108 | final ITypeScriptBuildPath buildPath = tsProject.getTypeScriptBuildPath();
|
95 | 109 | final Map<ITsconfigBuildPath, List<IFile>> tsFilesToCompile = new HashMap<ITsconfigBuildPath, List<IFile>>();
|
@@ -153,7 +167,7 @@ private void addTsFile(final ITypeScriptBuildPath buildPath,
|
153 | 167 | if (!tsconfig.isBuildOnSave() && tsconfig.isCompileOnSave()
|
154 | 168 | && tsProject.canSupport(CommandNames.CompileOnSaveEmitFile)) {
|
155 | 169 | // TypeScript >=2.0.5: compile is done with tsserver
|
156 |
| - //compileWithTsserver(tsProject, tsFiles, tsconfig); |
| 170 | + // compileWithTsserver(tsProject, tsFiles, tsconfig); |
157 | 171 | compileWithTsc(tsProject, tsFiles, tsconfig);
|
158 | 172 | } else {
|
159 | 173 | // TypeScript < 2.0.5: compile is done with tsc which is not
|
@@ -189,56 +203,64 @@ private void addTsFile(final ITypeScriptBuildPath buildPath,
|
189 | 203 | * @throws TypeScriptException
|
190 | 204 | * @throws CoreException
|
191 | 205 | */
|
192 |
| - public void compileWithTsc(IIDETypeScriptProject tsProject, List<IFile> tsFiles, IDETsconfigJson tsconfig) |
| 206 | + private void compileWithTsc(IIDETypeScriptProject tsProject, List<IFile> tsFiles, IDETsconfigJson tsconfig) |
193 | 207 | throws TypeScriptException, CoreException {
|
194 | 208 | tsProject.getCompiler().compile(tsconfig, tsFiles);
|
195 | 209 | }
|
196 | 210 |
|
197 | 211 | /**
|
198 |
| - * Compile the given ts files with tsserver by consumming |
199 |
| - * "compileOnSaveEmitFile" tsserver command. |
| 212 | + * Compile files with tsserver (since TypeScript 2.0.5). |
200 | 213 | *
|
201 | 214 | * @param tsProject
|
202 |
| - * @param tsFiles |
203 |
| - * @param tsconfig |
| 215 | + * @param delta |
| 216 | + * @param monitor |
204 | 217 | * @throws CoreException
|
205 | 218 | */
|
206 |
| - private void compileWithTsserver(IIDETypeScriptProject tsProject, List<IFile> tsFiles, IDETsconfigJson tsconfig) |
207 |
| - throws CoreException { |
208 |
| - for (final IFile file : tsFiles) { |
209 |
| - try { |
210 |
| - IIDETypeScriptFile tsFile = tsProject.getOpenedFile(file); |
211 |
| - // delete marker for the given ts file |
212 |
| - TypeScriptResourceUtil.deleteTscMarker(file); |
213 |
| - // compile the current ts file with "compileOnSaveEmitFile" |
214 |
| - tsFile.compileOnSaveEmitFile(null); |
215 |
| - // Refresh of js file, map file cannot work. |
216 |
| - // See |
217 |
| - TypeScriptResourceUtil.refreshAndCollectEmittedFiles(file, tsconfig, true, null); |
218 |
| - } catch (TypeScriptException e) { |
219 |
| - Trace.trace(Trace.SEVERE, "Error while tsserver compilation", e); |
220 |
| - } |
221 |
| - } |
222 |
| - |
223 |
| - try { |
224 |
| - tsProject.geterrForProject(WorkbenchResourceUtil.getFileName(tsFiles.get(0)), 0).thenAccept(events -> { |
225 |
| - System.err.println(events); |
226 |
| - }); |
227 |
| - } catch (TypeScriptException e) { |
228 |
| - // TODO Auto-generated catch block |
229 |
| - e.printStackTrace(); |
230 |
| - } |
231 |
| - |
| 219 | + private void compileWithTsserver(IIDETypeScriptProject tsProject, IResourceDelta delta, IProgressMonitor monitor) |
| 220 | + throws CoreException { |
| 221 | + |
| 222 | + final List<IFile> updatedTsFiles = new ArrayList<>(); |
| 223 | + final List<IFile> removedTsFiles = new ArrayList<>(); |
| 224 | + delta.accept(new IResourceDeltaVisitor() { |
| 225 | + |
| 226 | + @Override |
| 227 | + public boolean visit(IResourceDelta delta) throws CoreException { |
| 228 | + IResource resource = delta.getResource(); |
| 229 | + if (resource == null) { |
| 230 | + return false; |
| 231 | + } |
| 232 | + switch (resource.getType()) { |
| 233 | + case IResource.ROOT: |
| 234 | + case IResource.FOLDER: |
| 235 | + return true; |
| 236 | + case IResource.PROJECT: |
| 237 | + return TypeScriptResourceUtil.isTypeScriptProject((IProject) resource); |
| 238 | + case IResource.FILE: |
| 239 | + if (!TypeScriptResourceUtil.isTsOrTsxFile(resource)) { |
| 240 | + return false; |
| 241 | + } |
| 242 | + int kind = delta.getKind(); |
| 243 | + switch (kind) { |
| 244 | + case IResourceDelta.ADDED: |
| 245 | + case IResourceDelta.CHANGED: |
| 246 | + updatedTsFiles.add((IFile) resource); |
| 247 | + break; |
| 248 | + case IResourceDelta.REMOVED: |
| 249 | + removedTsFiles.add((IFile) resource); |
| 250 | + break; |
| 251 | + } |
| 252 | + return false; |
| 253 | + default: |
| 254 | + return false; |
| 255 | + } |
| 256 | + }; |
| 257 | + }); |
| 258 | + |
232 | 259 | try {
|
233 |
| - tsProject.geterrForProject(WorkbenchResourceUtil.getFileName(tsFiles.get(0)), 0); |
| 260 | + tsProject.compileWithTsserver(updatedTsFiles, removedTsFiles, monitor); |
234 | 261 | } catch (TypeScriptException e) {
|
235 |
| - Trace.trace(Trace.SEVERE, "Error while tsserver compilation", e); |
| 262 | + throw new CoreException(new Status(IStatus.ERROR, TypeScriptCorePlugin.PLUGIN_ID, |
| 263 | + "Error while compiling with tsserver", e)); |
236 | 264 | }
|
237 |
| -// try { |
238 |
| -// tsProject.getClient().projectInfo("", WorkbenchResourceUtil.getFileName(tsconfig.getTsconfigFile()), true); |
239 |
| -// } catch (TypeScriptException e) { |
240 |
| -// // TODO Auto-generated catch block |
241 |
| -// e.printStackTrace(); |
242 |
| -// } |
243 | 265 | }
|
244 | 266 | }
|
0 commit comments