typescript#LanguageService TypeScript Examples

The following examples show how to use typescript#LanguageService. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: measureLanguageServiceWorker.ts    From DefinitelyTyped-tools with MIT License 5 votes vote down vote up
async function measureLanguageService(
  languageService: LanguageService,
  args: MeasureLanguageServiceArgs
): Promise<LanguageServiceSingleMeasurement> {
  return {
    fileName: args.fileName,
    start: args.start,
    ...(await measureAtPosition(args.fileName, args.start)),
  };

  async function measureAtPosition(
    fileName: string,
    position: number
  ): Promise<Pick<LanguageServiceSingleMeasurement, "quickInfoDuration" | "completionsDuration">> {
    let quickInfoDuration = NaN;
    let completionsDuration = NaN;
    const observer = new PerformanceObserver((list) => {
      const completionsMeasurement = list.getEntriesByName("completionsMeasurement")[0];
      const quickInfoMeasurement = list.getEntriesByName("quickInfoMeasurement")[0];
      if (completionsMeasurement) {
        completionsDuration = completionsMeasurement.duration;
      }
      if (quickInfoMeasurement) {
        quickInfoDuration = quickInfoMeasurement.duration;
      }
    });

    observer.observe({ entryTypes: ["measure"] });
    getCompletionsAtPosition(languageService, fileName, position);
    getQuickInfoAtPosition(languageService, fileName, position);
    // Node 16 changed the PerformanceObserver callback to happen async,
    // so we have to sit here and wait for it...
    await new Promise((resolve) => setTimeout(resolve, 0));
    assert.ok(!isNaN(quickInfoDuration), "No measurement was recorded for quick info");
    assert.ok(!isNaN(completionsDuration), "No measurement was recorded for completions");
    observer.disconnect();

    return {
      quickInfoDuration,
      completionsDuration,
    };
  }
}
Example #2
Source File: measureLanguageServiceWorker.ts    From DefinitelyTyped-tools with MIT License 5 votes vote down vote up
function getCompletionsAtPosition(languageService: LanguageService, fileName: string, pos: number): boolean {
  performance.mark("beforeCompletions");
  const completions = languageService.getCompletionsAtPosition(fileName, pos, undefined);
  performance.mark("afterCompletions");
  performance.measure("completionsMeasurement", "beforeCompletions", "afterCompletions");
  return !!completions && completions.entries.length > 0;
}
Example #3
Source File: measureLanguageServiceWorker.ts    From DefinitelyTyped-tools with MIT License 5 votes vote down vote up
function getQuickInfoAtPosition(languageService: LanguageService, fileName: string, pos: number): boolean {
  performance.mark("beforeQuickInfo");
  const quickInfo = languageService.getQuickInfoAtPosition(fileName, pos);
  performance.mark("afterQuickInfo");
  performance.measure("quickInfoMeasurement", "beforeQuickInfo", "afterQuickInfo");
  return !!quickInfo;
}