Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Not Ready] [typespec-vscode] Improvements for the intellisense of tspconfig.yaml #5186

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,56 +1,70 @@
import { joinPaths } from "../core/path-utils.js";
import { NpmPackage, NpmPackageProvider } from "./npm-package-provider.js";

export class EmitterProvider {
export class LibraryProvider {
private isEmitterPackageCache = new Map<string, boolean>();
private isLinterPackageCache = new Map<string, boolean>();
mzhongl524 marked this conversation as resolved.
Show resolved Hide resolved
private isGetEmitter: boolean = false;
constructor(private npmPackageProvider: NpmPackageProvider) {}

/**
* Set whether to get the emitter library or the linter library
* @param isGetEmitter true if you want to get the emitter library, false if you want to get the linter library
*/
setIsGetEmitterVal(isGetEmitter: boolean): void {
this.isGetEmitter = isGetEmitter;
}

/**
*
* @param startFolder folder starts to search for package.json with emitters defined as dependencies
* @param startFolder folder starts to search for package.json with emitters/linters defined as dependencies
mzhongl524 marked this conversation as resolved.
Show resolved Hide resolved
* @returns
*/
async listEmitters(startFolder: string): Promise<Record<string, NpmPackage>> {
async listLibraries(startFolder: string): Promise<Record<string, NpmPackage>> {
const packageJsonFolder = await this.npmPackageProvider.getPackageJsonFolder(startFolder);
if (!packageJsonFolder) return {};

const pkg = await this.npmPackageProvider.get(packageJsonFolder);
const data = await pkg?.getPackageJsonData();
if (!data) return {};

const emitters: Record<string, NpmPackage> = {};
const libs: Record<string, NpmPackage> = {};
const allDep = {
...(data.dependencies ?? {}),
...(data.devDependencies ?? {}),
};
for (const dep of Object.keys(allDep)) {
const depPkg = await this.getEmitterFromDep(packageJsonFolder, dep);
const depPkg = await this.getLibraryFromDep(packageJsonFolder, dep);
if (depPkg) {
emitters[dep] = depPkg;
libs[dep] = depPkg;
}
}
return emitters;
return libs;
}

/**
*
* @param startFolder folder starts to search for package.json with emitters defined as dependencies
* @param startFolder folder starts to search for package.json with emitters/linter defined as dependencies
* @param emitterName
* @returns
*/
async getEmitter(startFolder: string, emitterName: string): Promise<NpmPackage | undefined> {
async getLibrary(startFolder: string, emitterName: string): Promise<NpmPackage | undefined> {
const packageJsonFolder = await this.npmPackageProvider.getPackageJsonFolder(startFolder);
if (!packageJsonFolder) {
return undefined;
}
return this.getEmitterFromDep(packageJsonFolder, emitterName);
return this.getLibraryFromDep(packageJsonFolder, emitterName);
}

private async isEmitter(depName: string, pkg: NpmPackage) {
if (this.isEmitterPackageCache.has(depName)) {
if (this.isGetEmitter && this.isEmitterPackageCache.has(depName)) {
return this.isEmitterPackageCache.get(depName);
}

if (!this.isGetEmitter && this.isLinterPackageCache.has(depName)) {
return this.isLinterPackageCache.get(depName);
}

const data = await pkg.getPackageJsonData();
// don't add to cache when failing to load package.json which is unexpected
if (!data) return false;
Expand All @@ -61,16 +75,27 @@ export class EmitterProvider {
const exports = await pkg.getModuleExports();
// don't add to cache when failing to load exports which is unexpected
if (!exports) return false;
const isEmitter = exports.$onEmit !== undefined;
this.isEmitterPackageCache.set(depName, isEmitter);
const isEmitter = this.isGetEmitter
? exports.$onEmit !== undefined
: exports.$linter !== undefined;
if (this.isGetEmitter) {
this.isEmitterPackageCache.set(depName, isEmitter);
} else {
this.isLinterPackageCache.set(depName, isEmitter);
}
return isEmitter;
} else {
this.isEmitterPackageCache.set(depName, false);
if (this.isGetEmitter) {
this.isEmitterPackageCache.set(depName, false);
} else {
this.isLinterPackageCache.set(depName, false);
}

return false;
}
}

private async getEmitterFromDep(packageJsonFolder: string, depName: string) {
private async getLibraryFromDep(packageJsonFolder: string, depName: string) {
const depFolder = joinPaths(packageJsonFolder, "node_modules", depName);
const depPkg = await this.npmPackageProvider.get(depFolder);
if (depPkg && (await this.isEmitter(depName, depPkg))) {
Expand Down
6 changes: 3 additions & 3 deletions packages/compiler/src/server/serverlib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ import { createCompileService } from "./compile-service.js";
import { resolveCompletion } from "./completion.js";
import { Commands } from "./constants.js";
import { convertDiagnosticToLsp } from "./diagnostics.js";
import { EmitterProvider } from "./emitter-provider.js";
import { createFileService } from "./file-service.js";
import { createFileSystemCache } from "./file-system-cache.js";
import { LibraryProvider } from "./lib-provider.js";
import { NpmPackageProvider } from "./npm-package-provider.js";
import { getSymbolStructure } from "./symbol-structure.js";
import { provideTspconfigCompletionItems } from "./tspconfig/completion.js";
Expand Down Expand Up @@ -114,7 +114,7 @@ export function createServer(host: ServerHost): Server {
});
const compilerHost = createCompilerHost();
const npmPackageProvider = new NpmPackageProvider(compilerHost);
const emitterProvider = new EmitterProvider(npmPackageProvider);
const libProvider = new LibraryProvider(npmPackageProvider);
mzhongl524 marked this conversation as resolved.
Show resolved Hide resolved

const compileService = createCompileService({
fileService,
Expand Down Expand Up @@ -703,7 +703,7 @@ export function createServer(host: ServerHost): Server {
if (doc) {
const items = await provideTspconfigCompletionItems(doc, params.position, {
fileService,
emitterProvider,
libProvider: libProvider,
log,
});
return CompletionList.create(items);
Expand Down
Loading
Loading