Skip to content

Commit

Permalink
Automatically pick subfolders when editor is opened on plugin-dir (ad…
Browse files Browse the repository at this point in the history
…dresses #130)
  • Loading branch information
untoldwind committed Mar 8, 2024
1 parent b7be02b commit 665f1be
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
6 changes: 6 additions & 0 deletions Tools/vscode/to2-syntax/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
"default": "C:\\Program Files (x86)\\Steam\\steamapps\\common\\Kerbal Space Program 2\\BepInEx\\plugins\\KontrolSystem2\\to2",
"description": "TO2 library folder"
},
"to2LspServer.localLibraryPath": {
"scope": "window",
"type": "string",
"default": "",
"description": "TO2 local library folder (if somewhere else than to2Local)"
},
"to2LspServer.maxNumberOfProblems": {
"scope": "resource",
"type": "number",
Expand Down
37 changes: 25 additions & 12 deletions Tools/vscode/to2-syntax/server/src/lsp-server-singleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
import { Registry } from "./to2/ast/registry";
import { TO2ModuleNode, isTO2ModuleNode } from "./to2/ast/to2-module";
import { module } from "./to2/parser-module";
import { pathToUri, uriToPath } from "./utils";
import { isDirectory, pathToUri, uriToPath } from "./utils";

export class LspServerSingleton {
private readonly registry = new Registry();
Expand Down Expand Up @@ -168,16 +168,32 @@ export class LspServerSingleton {

async indexWorkspace(workspaceUri: string) {
const workspacePath = uriToPath(workspaceUri);
if (!workspacePath) return;
if (!workspacePath || !(await isDirectory(workspacePath))) return;

this.workspaceFolders.add(workspaceUri);

let pathsToIndex = [workspacePath];
if (path.basename(workspacePath) === "KontrolSystem2") {
// Editor has been opened on plugin folder, try to find "to2" and "to2Local" subdirectories
const files = await fs.readdir(workspacePath, { withFileTypes: true });
const subDirs = files
.filter(
(f) => f.isDirectory() && (f.name === "to2" || f.name === "to2Local"),
)
.map((f) => path.join(workspacePath, f.name));

if (subDirs.length > 0) {
pathsToIndex = subDirs;
}
}

const progress = this.hasWorkDonwCapability
? await this.connection.window.createWorkDoneProgress()
: undefined;

progress?.begin("Indexing", 0, workspacePath, true);
progress?.begin("Indexing", 0, pathsToIndex.join(", "), true);

const stack: string[] = [workspacePath];
const stack: string[] = pathsToIndex;
const indexedModules: TO2ModuleNode[] = [];

while (progress === undefined || !progress.token.isCancellationRequested) {
Expand Down Expand Up @@ -222,16 +238,13 @@ export class LspServerSingleton {
async updateConfig(config: To2LspSettings) {
this.globalSettings = config;

if (config.libraryPath.length === 0) return;
try {
const stat = await fs.stat(config.libraryPath);
if (!stat.isDirectory()) return;
const libraryUri = pathToUri(config.libraryPath, this.documents);
for (const libraryPath of [config.libraryPath, config.localLibraryPath]) {
if (libraryPath.length === 0 || !(await isDirectory(libraryPath)))
continue;
const libraryUri = pathToUri(libraryPath, this.documents);
if (!this.workspaceFolders.has(libraryUri)) {
this.indexWorkspace(libraryUri);
await this.indexWorkspace(libraryUri);
}
} catch {
return;
}
}

Expand Down
2 changes: 2 additions & 0 deletions Tools/vscode/to2-syntax/server/src/settings.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export interface To2LspSettings {
libraryPath: string;
localLibraryPath: string;
maxNumberOfProblems: number;
}

export const defaultSettings: To2LspSettings = {
libraryPath: "",
localLibraryPath: "",
maxNumberOfProblems: 1000,
};
10 changes: 10 additions & 0 deletions Tools/vscode/to2-syntax/server/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TextDocuments } from "vscode-languageserver";
import { DocumentUri, TextDocument } from "vscode-languageserver-textdocument";
import { URI } from "vscode-uri";
import * as fs from "fs/promises";

export function uriToPath(stringUri: DocumentUri): string | undefined {
if (stringUri.startsWith("zipfile:")) {
Expand Down Expand Up @@ -47,3 +48,12 @@ const RE_PATHSEP_WINDOWS = /\\/g;
export function normalizeFsPath(fsPath: string): string {
return fsPath.replace(RE_PATHSEP_WINDOWS, "/");
}

export async function isDirectory(fsPath: string): Promise<boolean> {
try {
const stat = await fs.stat(fsPath);
return stat.isDirectory();
} catch {
return false;
}
}

0 comments on commit 665f1be

Please sign in to comment.