Skip to content

Commit

Permalink
search for non top level config / monorepo support (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
larshp committed Sep 11, 2024
1 parent be1d54b commit 6346362
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 28 deletions.
1 change: 1 addition & 0 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export function activate(context: ExtensionContext) {
codeLens: JSON.parse(JSON.stringify(workspace.getConfiguration("abaplint").get("codeLens"))),
inlayHints: JSON.parse(JSON.stringify(workspace.getConfiguration("abaplint").get("inlayHints"))),
formatting: JSON.parse(JSON.stringify(workspace.getConfiguration("abaplint").get("formatting"))),
activeTextEditorUri: vscode.window.activeTextEditor?.document.uri.toString(),
},
synchronize: {
fileEvents: workspace.createFileSystemWatcher("**/abaplint.json*"),
Expand Down
5 changes: 5 additions & 0 deletions server/src/extra_settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type ExtraSettings = {
codeLens?: any,
inlayHints?: any,
activeTextEditorUri?: string | undefined;
};
14 changes: 5 additions & 9 deletions server/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {FileOperations} from "./file_operations";
import {GitOperations} from "./git";
import {UnitTests} from "./handlers/unit_test";
import {Formatting} from "./handlers/formatting";
import {ExtraSettings} from "./extra_settings";

export interface IFolder {
root: string;
Expand Down Expand Up @@ -58,11 +59,6 @@ class Progress implements abaplint.IProgress {
}
}

type ExtraSettings = {
codeLens?: any,
inlayHints?: any,
};

export class Handler {
private readonly folders: IFolder[] = [];
private readonly reg: abaplint.IRegistry;
Expand All @@ -71,23 +67,23 @@ export class Handler {
private readonly settings: ExtraSettings;
private timeouts: {[index: string]: any} = {};

public static async create(connection: LServer.Connection, params: LServer.InitializeParams & ExtraSettings) {
public static async create(connection: LServer.Connection, params: LServer.InitializeParams) {
const handler = new Handler(connection, params);
await handler.readAndSetConfig();
return handler;
}

private constructor(connection: LServer.Connection, params: LServer.InitializeParams & ExtraSettings) {
private constructor(connection: LServer.Connection, params: LServer.InitializeParams) {
this.reg = new abaplint.Registry();
this.connection = connection;

this.setup = new Setup(connection);
this.folders = this.setup.determineFolders(params.workspaceFolders || []);
this.settings = params;
this.settings = params.initializationOptions;
}

private async readAndSetConfig() {
const config = await this.setup.readConfig(this.folders);
const config = await this.setup.readConfig(this.folders, this.settings);
this.reg.setConfig(config);
this.setup.dumpFolders(this.folders);
}
Expand Down
70 changes: 51 additions & 19 deletions server/src/setup.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {URI} from "vscode-uri";
import {URI, Utils} from "vscode-uri";
import {IFolder} from "./handler";
import {FileOperations} from "./file_operations";
import * as LServer from "vscode-languageserver";
import * as abaplint from "@abaplint/core";
import {ExtraSettings} from "./extra_settings";

export class Setup {
private readonly connection: LServer.Connection;
Expand Down Expand Up @@ -37,44 +38,35 @@ export class Setup {
}
}

public async readConfig(folders: IFolder[]) {
const raw = await this.findCustomConfig(folders);
if (raw && raw !== "") {
public async readConfig(folders: IFolder[], settings: ExtraSettings) {
const found = await this.findCustomConfig(folders, settings.activeTextEditorUri);
if (found) {
this.connection.console.log("custom abaplint configuration found");
const config = new abaplint.Config(raw);
folders[0].glob = config.get().global.files;
const config = new abaplint.Config(found.config);
folders[0].glob = found.prefix + config.get().global.files;
return config;
}

this.connection.console.log("no custom abaplint config, using defaults");
return abaplint.Config.getDefault();
}

private async findCustomConfig(folders: IFolder[]): Promise<string | undefined> {
if (folders.length === 0 || folders[0] === undefined) {
return undefined;
}

const prefix = folders[0].root + "/";
this.connection.console.log("prefix: " + prefix);
this.connection.console.log("scheme: " + folders[0].scheme);
// this.connection.console.log(URI.from({scheme: folders[0].scheme, path: prefix + "abaplint.json"}).toString());

let uri = URI.from({scheme: folders[0].scheme, authority: folders[0].authority, path: prefix + "abaplint.json"});
private async searchFolderForConfig(scheme: string, authority: string, prefix: string): Promise<string | undefined> {
let uri = URI.from({scheme: scheme, authority: authority, path: prefix + "abaplint.json"});
try {
this.connection.console.log("search: " + uri.toString());
return await FileOperations.readFile(uri.toString());
// eslint-disable-next-line no-empty
} catch {}

uri = URI.from({scheme: folders[0].scheme, authority: folders[0].authority, path: prefix + "abaplint.jsonc"});
uri = URI.from({scheme: scheme, authority: authority, path: prefix + "abaplint.jsonc"});
try {
this.connection.console.log("search: " + uri.toString());
return await FileOperations.readFile(uri.toString());
// eslint-disable-next-line no-empty
} catch {}

uri = URI.from({scheme: folders[0].scheme, authority: folders[0].authority, path: prefix + "abaplint.json5"});
uri = URI.from({scheme: scheme, authority: authority, path: prefix + "abaplint.json5"});
try {
this.connection.console.log("search: " + uri.toString());
return await FileOperations.readFile(uri.toString());
Expand All @@ -84,4 +76,44 @@ export class Setup {
return undefined;
}

private async findCustomConfig(folders: IFolder[], activeTextEditorUri: string | undefined):
Promise<{config: string, prefix: string} | undefined> {
if (folders.length === 0 || folders[0] === undefined) {
return undefined;
}

const prefix = folders[0].root + "/";
this.connection.console.log("prefix: " + prefix);
this.connection.console.log("activeTextEditorUri: " + activeTextEditorUri);
this.connection.console.log("scheme: " + folders[0].scheme);
// this.connection.console.log(URI.from({scheme: folders[0].scheme, path: prefix + "abaplint.json"}).toString());

if (activeTextEditorUri !== undefined) {
const start = URI.parse(activeTextEditorUri);
let current = Utils.dirname(start).path + "/";
while (current !== prefix) {
const found = await this.searchFolderForConfig(folders[0].scheme, folders[0].authority, current);
if (found) {
return {
config: found,
prefix: current.substring(prefix.length - 1, current.length - 1),
};
}

current = Utils.joinPath(URI.parse(current), "..").path + "/";
}
}

// root folder
const found = await this.searchFolderForConfig(folders[0].scheme, folders[0].authority, prefix);
if (found) {
return {
config: found,
prefix: "",
};
}

return undefined;
}

}

0 comments on commit 6346362

Please sign in to comment.