2016-11-22 12:10:23 -05:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2019-04-16 19:36:47 -04:00
|
|
|
import * as ts from 'typescript/lib/tsserverlibrary';
|
2016-11-22 12:10:23 -05:00
|
|
|
|
|
|
|
import {createLanguageService} from './language_service';
|
2019-01-14 20:13:06 -05:00
|
|
|
import {Completion, Diagnostic, DiagnosticMessageChain} from './types';
|
2016-11-22 12:10:23 -05:00
|
|
|
import {TypeScriptServiceHost} from './typescript_host';
|
|
|
|
|
2019-04-16 19:36:47 -04:00
|
|
|
const projectHostMap = new WeakMap<ts.server.Project, TypeScriptServiceHost>();
|
2017-04-25 15:13:06 -04:00
|
|
|
|
2019-04-16 19:36:47 -04:00
|
|
|
export function getExternalFiles(project: ts.server.Project): string[]|undefined {
|
2017-04-25 15:13:06 -04:00
|
|
|
const host = projectHostMap.get(project);
|
|
|
|
if (host) {
|
2019-04-16 19:36:47 -04:00
|
|
|
const externalFiles = host.getTemplateReferences();
|
|
|
|
return externalFiles;
|
2017-04-25 15:13:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-14 20:13:06 -05:00
|
|
|
function completionToEntry(c: Completion): ts.CompletionEntry {
|
|
|
|
return {
|
|
|
|
// TODO: remove any and fix type error.
|
|
|
|
kind: c.kind as any,
|
|
|
|
name: c.name,
|
|
|
|
sortText: c.sort,
|
|
|
|
kindModifiers: ''
|
|
|
|
};
|
|
|
|
}
|
2017-04-25 15:13:06 -04:00
|
|
|
|
2019-01-14 20:13:06 -05:00
|
|
|
function diagnosticChainToDiagnosticChain(chain: DiagnosticMessageChain):
|
|
|
|
ts.DiagnosticMessageChain {
|
|
|
|
return {
|
|
|
|
messageText: chain.message,
|
|
|
|
category: ts.DiagnosticCategory.Error,
|
|
|
|
code: 0,
|
|
|
|
next: chain.next ? diagnosticChainToDiagnosticChain(chain.next) : undefined
|
|
|
|
};
|
|
|
|
}
|
2016-11-22 12:10:23 -05:00
|
|
|
|
2019-01-14 20:13:06 -05:00
|
|
|
function diagnosticMessageToDiagnosticMessageText(message: string | DiagnosticMessageChain): string|
|
|
|
|
ts.DiagnosticMessageChain {
|
|
|
|
if (typeof message === 'string') {
|
|
|
|
return message;
|
2017-11-14 20:49:47 -05:00
|
|
|
}
|
2019-01-14 20:13:06 -05:00
|
|
|
return diagnosticChainToDiagnosticChain(message);
|
|
|
|
}
|
2017-11-14 20:49:47 -05:00
|
|
|
|
2019-01-14 20:13:06 -05:00
|
|
|
function diagnosticToDiagnostic(d: Diagnostic, file: ts.SourceFile): ts.Diagnostic {
|
|
|
|
const result = {
|
|
|
|
file,
|
|
|
|
start: d.span.start,
|
|
|
|
length: d.span.end - d.span.start,
|
|
|
|
messageText: diagnosticMessageToDiagnosticMessageText(d.message),
|
|
|
|
category: ts.DiagnosticCategory.Error,
|
|
|
|
code: 0,
|
|
|
|
source: 'ng'
|
|
|
|
};
|
|
|
|
return result;
|
|
|
|
}
|
2017-11-14 20:49:47 -05:00
|
|
|
|
2019-04-16 19:36:47 -04:00
|
|
|
export function create(info: ts.server.PluginCreateInfo): ts.LanguageService {
|
2019-01-14 20:13:06 -05:00
|
|
|
const oldLS: ts.LanguageService = info.languageService;
|
|
|
|
const proxy: ts.LanguageService = Object.assign({}, oldLS);
|
|
|
|
const logger = info.project.projectService.logger;
|
2016-11-22 12:10:23 -05:00
|
|
|
|
2017-04-25 15:13:06 -04:00
|
|
|
function tryOperation<T>(attempting: string, callback: () => T): T|null {
|
2017-01-06 23:43:17 -05:00
|
|
|
try {
|
2017-04-25 15:13:06 -04:00
|
|
|
return callback();
|
2017-01-06 23:43:17 -05:00
|
|
|
} catch (e) {
|
2019-01-14 20:13:06 -05:00
|
|
|
logger.info(`Failed to ${attempting}: ${e.toString()}`);
|
|
|
|
logger.info(`Stack trace: ${e.stack}`);
|
2017-04-25 15:13:06 -04:00
|
|
|
return null;
|
2017-01-06 23:43:17 -05:00
|
|
|
}
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|
|
|
|
|
2019-01-14 20:13:06 -05:00
|
|
|
const serviceHost = new TypeScriptServiceHost(info.languageServiceHost, oldLS);
|
2019-04-16 19:36:47 -04:00
|
|
|
const ls = createLanguageService(serviceHost);
|
2017-01-06 23:43:17 -05:00
|
|
|
serviceHost.setSite(ls);
|
2017-04-25 15:13:06 -04:00
|
|
|
projectHostMap.set(info.project, serviceHost);
|
2017-01-06 23:43:17 -05:00
|
|
|
|
2017-12-22 12:36:47 -05:00
|
|
|
proxy.getCompletionsAtPosition = function(
|
|
|
|
fileName: string, position: number, options: ts.GetCompletionsAtPositionOptions|undefined) {
|
|
|
|
let base = oldLS.getCompletionsAtPosition(fileName, position, options) || {
|
2017-04-25 15:13:06 -04:00
|
|
|
isGlobalCompletion: false,
|
|
|
|
isMemberCompletion: false,
|
|
|
|
isNewIdentifierLocation: false,
|
|
|
|
entries: []
|
|
|
|
};
|
2017-01-06 23:43:17 -05:00
|
|
|
tryOperation('get completions', () => {
|
|
|
|
const results = ls.getCompletionsAt(fileName, position);
|
|
|
|
if (results && results.length) {
|
|
|
|
if (base === undefined) {
|
2017-01-24 12:05:34 -05:00
|
|
|
base = {
|
|
|
|
isGlobalCompletion: false,
|
|
|
|
isMemberCompletion: false,
|
|
|
|
isNewIdentifierLocation: false,
|
|
|
|
entries: []
|
|
|
|
};
|
2017-01-06 23:43:17 -05:00
|
|
|
}
|
|
|
|
for (const entry of results) {
|
|
|
|
base.entries.push(completionToEntry(entry));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return base;
|
|
|
|
};
|
|
|
|
|
2018-08-05 11:31:27 -04:00
|
|
|
proxy.getQuickInfoAtPosition = function(fileName: string, position: number): ts.QuickInfo |
|
|
|
|
undefined {
|
|
|
|
let base = oldLS.getQuickInfoAtPosition(fileName, position);
|
|
|
|
// TODO(vicb): the tags property has been removed in TS 2.2
|
|
|
|
tryOperation('get quick info', () => {
|
|
|
|
const ours = ls.getHoverAt(fileName, position);
|
|
|
|
if (ours) {
|
|
|
|
const displayParts: ts.SymbolDisplayPart[] = [];
|
|
|
|
for (const part of ours.text) {
|
|
|
|
displayParts.push({kind: part.language || 'angular', text: part.text});
|
|
|
|
}
|
|
|
|
const tags = base && (<any>base).tags;
|
|
|
|
base = <any>{
|
|
|
|
displayParts,
|
|
|
|
documentation: [],
|
|
|
|
kind: 'angular',
|
|
|
|
kindModifiers: 'what does this do?',
|
|
|
|
textSpan: {start: ours.span.start, length: ours.span.end - ours.span.start},
|
|
|
|
};
|
|
|
|
if (tags) {
|
|
|
|
(<any>base).tags = tags;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2017-01-06 23:43:17 -05:00
|
|
|
|
2018-08-05 11:31:27 -04:00
|
|
|
return base;
|
|
|
|
};
|
2017-01-06 23:43:17 -05:00
|
|
|
|
|
|
|
proxy.getSemanticDiagnostics = function(fileName: string) {
|
2017-04-25 15:13:06 -04:00
|
|
|
let result = oldLS.getSemanticDiagnostics(fileName);
|
|
|
|
const base = result || [];
|
2017-01-06 23:43:17 -05:00
|
|
|
tryOperation('get diagnostics', () => {
|
2019-01-14 20:13:06 -05:00
|
|
|
logger.info(`Computing Angular semantic diagnostics...`);
|
2017-01-06 23:43:17 -05:00
|
|
|
const ours = ls.getDiagnostics(fileName);
|
|
|
|
if (ours && ours.length) {
|
2018-08-05 11:31:27 -04:00
|
|
|
const file = oldLS.getProgram() !.getSourceFile(fileName);
|
2018-02-08 11:59:25 -05:00
|
|
|
if (file) {
|
|
|
|
base.push.apply(base, ours.map(d => diagnosticToDiagnostic(d, file)));
|
|
|
|
}
|
2017-01-06 23:43:17 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return base;
|
|
|
|
};
|
2016-11-22 12:10:23 -05:00
|
|
|
|
2017-01-06 23:43:17 -05:00
|
|
|
proxy.getDefinitionAtPosition = function(
|
2018-12-07 02:01:08 -05:00
|
|
|
fileName: string, position: number): ReadonlyArray<ts.DefinitionInfo> {
|
2017-01-06 23:43:17 -05:00
|
|
|
let base = oldLS.getDefinitionAtPosition(fileName, position);
|
|
|
|
if (base && base.length) {
|
|
|
|
return base;
|
2016-11-22 12:10:23 -05:00
|
|
|
}
|
2017-01-06 23:43:17 -05:00
|
|
|
|
2017-04-25 15:13:06 -04:00
|
|
|
return tryOperation('get definition', () => {
|
|
|
|
const ours = ls.getDefinitionAt(fileName, position);
|
2018-12-07 02:01:08 -05:00
|
|
|
let combined;
|
|
|
|
|
2017-04-25 15:13:06 -04:00
|
|
|
if (ours && ours.length) {
|
2018-12-07 02:01:08 -05:00
|
|
|
combined = base && base.concat([]) || [];
|
2017-04-25 15:13:06 -04:00
|
|
|
for (const loc of ours) {
|
2018-12-07 02:01:08 -05:00
|
|
|
combined.push({
|
2017-04-25 15:13:06 -04:00
|
|
|
fileName: loc.fileName,
|
|
|
|
textSpan: {start: loc.span.start, length: loc.span.end - loc.span.start},
|
|
|
|
name: '',
|
2017-10-26 20:23:30 -04:00
|
|
|
// TODO: remove any and fix type error.
|
|
|
|
kind: 'definition' as any,
|
2017-04-25 15:13:06 -04:00
|
|
|
containerName: loc.fileName,
|
2017-10-26 20:23:30 -04:00
|
|
|
containerKind: 'file' as any,
|
2017-04-25 15:13:06 -04:00
|
|
|
});
|
|
|
|
}
|
2018-12-07 02:01:08 -05:00
|
|
|
} else {
|
|
|
|
combined = base;
|
2017-04-25 15:13:06 -04:00
|
|
|
}
|
2018-12-07 02:01:08 -05:00
|
|
|
return combined;
|
2017-04-25 15:13:06 -04:00
|
|
|
}) || [];
|
2017-01-06 23:43:17 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
return proxy;
|
|
|
|
}
|