Skip to content

Commit

Permalink
Bring back teal.compilerPath setting
Browse files Browse the repository at this point in the history
Should fix #66
  • Loading branch information
pdesaulniers committed Dec 13, 2023
1 parent f8116b2 commit ef05561
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 26 deletions.
9 changes: 2 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,10 @@
"configuration": {
"title": "Teal",
"properties": {
"teal.compilerPath.unix": {
"teal.compilerPath": {
"type": "string",
"default": "tl",
"description": "The path of the tl compiler on Unix-like platforms."
},
"teal.compilerPath.window": {
"type": "string",
"default": "tl.bat",
"description": "The path of the tl compiler on Windows."
"description": "The path of the tl compiler."
}
}
},
Expand Down
4 changes: 2 additions & 2 deletions server/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { TreeSitterDocument } from "./tree-sitter-document";
import { EOL } from "os";

export namespace TealLS {
export async function validateTextDocument(textDocument: TreeSitterDocument): Promise<Map<string, Diagnostic[]>> {
export async function validateTextDocument(textDocument: TreeSitterDocument, compilerPath: string): Promise<Map<string, Diagnostic[]>> {
const projectRoot = await textDocument.getProjectRoot();

const checkResult = await Teal.runCommandOnText(Teal.TLCommand.Check, textDocument.getText(), projectRoot);
const checkResult = await Teal.runCommandOnText(Teal.TLCommand.Check, compilerPath, textDocument.getText(), projectRoot);

const crashPattern = /stack traceback:/m;

Expand Down
32 changes: 22 additions & 10 deletions server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,28 @@ connection.onInitialized(() => {
}
});

interface TealServerSettings { };
interface TealServerSettings {
compilerPath: string
};

const defaultSettings: TealServerSettings = {};
const defaultSettings: TealServerSettings = {
compilerPath: "tl"
};

let globalSettings: TealServerSettings = defaultSettings;

function getCompilerPath() {
return globalSettings.compilerPath;
}

// Cache the settings of all open documents
let settingsCache: Map<string, TealServerSettings> = new Map();

// Cache "tl types" queries of all open documents
let typesCommandCache: Map<string, Teal.TLTypesCommandResult> = new Map();

async function verifyMinimumTLVersion(): Promise<boolean> {
const tlVersion = await Teal.getVersion();
async function verifyMinimumTLVersion(compilerPath: string): Promise<boolean> {
const tlVersion = await Teal.getVersion(compilerPath);

if (tlVersion !== null) {
const targetVersion = new MajorMinorPatch(0, 12, 0);
Expand All @@ -123,14 +131,16 @@ connection.onDidChangeConfiguration((change) => {
// Reset all cached document settings
settingsCache.clear();
} else {
console.log("onDidChangeConfiguration", change.settings);

globalSettings = <TealServerSettings>(
(change.settings.teal || defaultSettings)
);
}

// Revalidate all open text documents
documents.forEach(async function (x: TreeSitterDocument) {
const validVersion = await verifyMinimumTLVersion();
const validVersion = await verifyMinimumTLVersion(globalSettings.compilerPath);

if (validVersion) {
validateTextDocument(x.uri);
Expand All @@ -140,7 +150,7 @@ connection.onDidChangeConfiguration((change) => {
typesCommandCache.clear();
});

async function getDocumentSettings(uri: string): Promise<TealServerSettings | null> {
async function getDocumentSettings(uri: string): Promise<TealServerSettings> {
if (!hasConfigurationCapability) {
return Promise.resolve(globalSettings);
}
Expand All @@ -154,7 +164,7 @@ async function getDocumentSettings(uri: string): Promise<TealServerSettings | nu
});

if (result === undefined) {
return null;
return defaultSettings;
}

settingsCache.set(uri, result);
Expand Down Expand Up @@ -223,7 +233,7 @@ async function _feedTypeInfoCache(uri: string) {
let typesCmdResult: Teal.TLCommandIOInfo;

try {
typesCmdResult = await Teal.runCommandOnText(Teal.TLCommand.Types, documentText, await textDocument.getProjectRoot());
typesCmdResult = await Teal.runCommandOnText(Teal.TLCommand.Types, settings.compilerPath, documentText, await textDocument.getProjectRoot());
} catch (error: any) {
showErrorMessage("[Error]\n" + error.message);
return null;
Expand Down Expand Up @@ -271,7 +281,9 @@ export function getTypeInfoFromCache(uri: string): Teal.TLTypesCommandResult | n
}

connection.onDidOpenTextDocument(async (params) => {
const validVersion = await verifyMinimumTLVersion();
const settings = await getDocumentSettings(params.textDocument.uri);

const validVersion = await verifyMinimumTLVersion(settings.compilerPath);

if (!validVersion) {
return;
Expand Down Expand Up @@ -346,7 +358,7 @@ async function _validateTextDocument(uri: string): Promise<void> {
let settings = await getDocumentSettings(textDocument.uri);

try {
const diagnosticsByPath = await TealLS.validateTextDocument(textDocument);
const diagnosticsByPath = await TealLS.validateTextDocument(textDocument, settings.compilerPath);

for (let [uri, diagnostics] of diagnosticsByPath.entries()) {
connection.sendDiagnostics({ uri: uri, diagnostics: diagnostics });
Expand Down
14 changes: 7 additions & 7 deletions server/teal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ export namespace Teal {
* @param text The text.
* @param projectRoot The directory which contains the tlconfig.lua file to use for running the command.
*/
export async function runCommandOnText(command: TLCommand, text: string, projectRoot?: string): Promise<TLCommandIOInfo> {
export async function runCommandOnText(command: TLCommand, compilerPath: string, text: string, projectRoot?: string): Promise<TLCommandIOInfo> {
try {
return await withFile(async ({ path, fd }) => {
await writeFile(fd, text);

try {
let result = await runCommand(command, path, projectRoot);
let result = await runCommand(command, compilerPath, path, projectRoot);
return result;
} catch (error) {
throw error;
Expand All @@ -165,9 +165,9 @@ export namespace Teal {
}
}

export const tlNotFoundErrorMessage = "Could not find the tl executable. Please make sure that it is available in the PATH.";
export const tlNotFoundErrorMessage = "Could not find the tl executable or one of its dependencies. Please make sure that they are available in the PATH.";

export async function runCommand(command: TLCommand, filePath?: string, cwd?: string): Promise<TLCommandIOInfo> {
export async function runCommand(command: TLCommand, compilerPath: string, filePath?: string, cwd?: string): Promise<TLCommandIOInfo> {
let child: any;

let isWindows = process.platform == "win32";
Expand All @@ -182,7 +182,7 @@ export namespace Teal {
args.push(filePath);
}

child = spawn("tl", args, {
child = spawn(compilerPath, args, {
shell: isWindows,
cwd: cwd
});
Expand Down Expand Up @@ -213,11 +213,11 @@ export namespace Teal {
});
}

export async function getVersion(): Promise<MajorMinorPatch | null> {
export async function getVersion(compilerPath: string): Promise<MajorMinorPatch | null> {
let commandResult: TLCommandIOInfo;

try {
commandResult = await Teal.runCommand(Teal.TLCommand.Version);
commandResult = await Teal.runCommand(Teal.TLCommand.Version, compilerPath);
} catch (e) {
return null;
}
Expand Down

0 comments on commit ef05561

Please sign in to comment.