Skip to content

Commit

Permalink
Cheating a bit to get the project files when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
Matsuuu committed Nov 7, 2023
1 parent cc4e8b3 commit 11ca11f
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export async function analyzeLocalProject(project: tss.server.Project): Promise<
.map(rf => project.getSourceFile(rf as tss.Path))
.filter(sf => sf !== undefined) as tss.SourceFile[];


// TODO: Is this step necessary? Might be to hold analyzer ts version mismatch errors from happening.
const modifiedSourceFiles: ts.SourceFile[] = sourceFiles.map(sf => {
return ts.createSourceFile(
Expand Down
40 changes: 31 additions & 9 deletions lib/server/src/language-services/language-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,37 @@ import { getProjectService } from "./project-service.js";

const projectService = getProjectService();

export enum ProjectSpecificity {
FILE_LEVEL = "file_level",
PROJECT_LEVEL = "project_level"
}

export class LanguageServiceManager {
static _instance?: LanguageServiceManager;

public refreshFileForLanguageService(fileName: string, fileContent: string | undefined) {
const project = projectService.openAndGetProjectForFile(fileName, fileContent);
if (!project) {
// TODO: Do something?
return;
}
public async refreshFileForLanguageService(fileName: string, fileContent: string | undefined) {
getProjectForCurrentFile(fileName, fileContent ?? '');
}

public getProjectForCurrentFile(fileName: string, fileContent: string) {
return projectService.openAndGetProjectForFile(fileName, fileContent);
public getProjectForCurrentFile(fileName: string, fileContent: string, specificity: ProjectSpecificity) {
let project = projectService.openAndGetProjectForFile(fileName, fileContent);
// TODO: Okay so this hack doesn't really function as it breaks import checks. Either
// Rewrite import checks or make this better
if (project && !isConfiguredProject(project) && specificity === ProjectSpecificity.PROJECT_LEVEL) {
const configuredProjects = [...projectService.configuredProjects.entries()];
// This is a hack to support other file formats, which don't automatically have a
// configuredproject. The `projectService.configFileForOpenFiles` API is marked as internal
// and therefore shouldn't be accessed. Needs more research on this but this works for now.
// @ts-ignore
const configFileMap = projectService.configFileForOpenFiles;
const currentFileConfigFile = configFileMap.get(fileName);
const matchingProject = configuredProjects.find(entry => {
const configPath = entry[0];
return configPath === currentFileConfigFile
})
project = matchingProject?.[1];
}
return project;
}

}
Expand Down Expand Up @@ -67,7 +85,11 @@ export function refreshLanguageServiceForFile(fileName: string, fileContent: str
}

export function getProjectForCurrentFile(fileName: string, fileContent: string) {
return getLanguageServiceManagerInstance().getProjectForCurrentFile(fileName, fileContent);
return getLanguageServiceManagerInstance().getProjectForCurrentFile(fileName, fileContent, ProjectSpecificity.FILE_LEVEL);
}

export function getProjectForCurrentContext(fileName: string, fileContent: string) {
return getLanguageServiceManagerInstance().getProjectForCurrentFile(fileName, fileContent, ProjectSpecificity.PROJECT_LEVEL);
}

export function isConfiguredProject(project: tss.server.Project): project is tss.server.ConfiguredProject {
Expand Down
4 changes: 2 additions & 2 deletions lib/server/src/text-documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import tss from "typescript/lib/tsserverlibrary.js";
import url from "url";
import { getCapabilities, getGlobalSettings, LanguageServerSettings } from "./settings.js";
import { TextDocuments, _Connection } from "vscode-languageserver";
import { getProjectForCurrentFile, refreshLanguageServiceForFile } from "./language-services/language-services.js";
import { getProjectForCurrentContext, getProjectForCurrentFile, refreshLanguageServiceForFile } from "./language-services/language-services.js";

Check warning on line 6 in lib/server/src/text-documents.ts

View workflow job for this annotation

GitHub Actions / Qodana for JS

Unused import

Unused import specifier getProjectForCurrentFile
import { runDiagnostics } from "./diagnostics.js";
import { connection } from "./connection.js";
import { textDocumentDataToUsableDataFromUri, UsableTextDocumentData } from "./transformers.js";
Expand All @@ -14,7 +14,7 @@ export let documents: TextDocuments<TextDocument> = new TextDocuments(TextDocume

function refreshCEM(usableData: UsableTextDocumentData) {
refreshLanguageServiceForFile(usableData.fileName, usableData.fileContent);
const project = getProjectForCurrentFile(usableData.fileName, usableData.fileContent);
const project = getProjectForCurrentContext(usableData.fileName, usableData.fileContent);

if (project) {
refreshCEMData(project.getCurrentDirectory());
Expand Down
2 changes: 1 addition & 1 deletion usage-testing-project/src/card.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
---
import "./example-project.js";
interface Props {
title: string;
body: string;
Expand All @@ -13,6 +12,7 @@ const { href, title, body } = Astro.props;
<a href={href}>
<h2>
{title}
<example-project>
<example-project></example-project>
<span>&rarr;</span>
</h2>
Expand Down
1 change: 0 additions & 1 deletion usage-testing-project/src/foo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { html } from "lit-html";
import "@shoelace-style/shoelace/dist/components/input/input.js";
import "./example-project.js";

export const temp = html`
<example-project my-attribute="f"
Expand Down
3 changes: 0 additions & 3 deletions usage-testing-project/src/foo.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@

<script setup>
import "@shoelace-style/shoelace/dist/components/button/button.js";
import "./example-project.js";
import { ref } from 'vue'
const greeting = ref('Hello World!')
</script>
Expand Down
1 change: 1 addition & 0 deletions usage-testing-project/src/mock_file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('foo');

0 comments on commit 11ca11f

Please sign in to comment.