Skip to content

Commit

Permalink
improve error handling and add status bar icons
Browse files Browse the repository at this point in the history
Signed-off-by: Jade Abraham <[email protected]>
  • Loading branch information
jabraham17 committed May 23, 2024
1 parent 70b2e93 commit abf17ae
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 22 deletions.
24 changes: 21 additions & 3 deletions src/ChapelLanguageClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
findPossibleChplHomes,
getWorkspaceFolder,
} from "./ChplPaths";
import {showChplHomeMissingError} from "./extension";
import { showChplHomeMissingError } from "./extension";
import * as path from "path";

export enum LanguageClientState {
Expand Down Expand Up @@ -76,6 +76,7 @@ export abstract class ChapelLanguageClient {
tool_path: string;
client: ErrorHandlingClient | undefined;
logger: vscode.LogOutputChannel;
statusBarItem: vscode.StatusBarItem;

constructor(
config: ToolConfig,
Expand All @@ -90,6 +91,21 @@ export abstract class ChapelLanguageClient {
this.tool_path = this.getToolPath();
this.client = undefined;
this.logger = logger;
this.statusBarItem = vscode.window.createStatusBarItem(
vscode.StatusBarAlignment.Right,
1000
);
// render the text using vscode codicons
this.statusBarItem.text = `$(error) ${this.name}`;
this.statusBarItem.tooltip = `${this.name} is stopped. Click to restart.`;
this.statusBarItem.color = new vscode.ThemeColor(
"statusBarItem.errorForeground"
);
this.statusBarItem.backgroundColor = new vscode.ThemeColor(
"statusBarItem.errorBackground"
);
this.statusBarItem.command = `${this.name}.restart`;
this.statusBarItem.hide();
}

protected abstract getToolPath(): string;
Expand All @@ -109,13 +125,15 @@ export abstract class ChapelLanguageClient {

setErrorState() {
this.state = LanguageClientState.ERRORED;
this.statusBarItem.show();
}

clearError(): void {
this.state = LanguageClientState.STOPPED;
}

private errorFindTools() {
this.setErrorState();
// if invalid chplhome, prompt user to set it
// if missing tool path, warn user that we can't find it, tell them to not override the path or upgrade their chapel version
// otherwise, its likely the tools arent built, so prompt the user to build them
Expand Down Expand Up @@ -172,11 +190,11 @@ export abstract class ChapelLanguageClient {
return Promise.resolve();
}
this.state = LanguageClientState.STARTING;
this.statusBarItem.hide();
let toolPathError = checkToolPath(this.tool_path);
if (toolPathError !== undefined) {
this.logger.error(toolPathError);
this.errorFindTools();
this.state = LanguageClientState.STOPPED;
return Promise.reject();
}

Expand Down Expand Up @@ -251,7 +269,7 @@ export abstract class ChapelLanguageClient {
}

this.stop().finally(() => {
this.state = LanguageClientState.ERRORED;
this.setErrorState();

vscode.window
.showErrorMessage(
Expand Down
20 changes: 12 additions & 8 deletions src/ChplPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import * as path from "path";
import * as fs from "fs";
import * as vscode from "vscode";
import * as cfg from "./configuration";
import { showInvalidPathWarning } from "./extension";
import assert from "assert";

export function checkChplHome(
Expand Down Expand Up @@ -66,7 +67,10 @@ export function checkChplHome(

// take a callback to be called on each file found
// if the callback returns true, stop searching
function searchPATH(file: string, callback: (file_path: string) => boolean | void) {
function searchPATH(
file: string,
callback: (file_path: string) => boolean | void
) {
const PATH = process.env["PATH"];
const paths_to_check = PATH?.split(path.delimiter) ?? [];
for (const p of paths_to_check) {
Expand Down Expand Up @@ -100,12 +104,12 @@ export function findToolPath(
// 2. if there is a chplhome, use that
// 3. otherwise, search PATH

const cfg_tool_path = tool_name === "chplcheck" ? cfg.getChplCheckConfig().path : cfg.getCLSConfig().path;
if (cfg_tool_path !== undefined) {
const error = checkToolPath(cfg_tool_path);
if (error === undefined) {
return cfg_tool_path;
}
const cfg_tool_path =
tool_name === "chplcheck"
? cfg.getChplCheckConfig().path
: cfg.getCLSConfig().path;
if (cfg_tool_path !== undefined && cfg_tool_path !== "") {
return cfg_tool_path;
}

if (chplhome !== undefined && checkChplHome(chplhome) === undefined) {
Expand Down Expand Up @@ -133,7 +137,7 @@ function searchDirectoryForChplHome(dir: string, depth: number = 1): string[] {
try {
// sadly this is the only way synchronous way to check if the directory is readable
fs.accessSync(dir, fs.constants.R_OK);
} catch(err) {
} catch (err) {
return chplhomes;
}
fs.readdirSync(dir).forEach((subdir) => {
Expand Down
15 changes: 10 additions & 5 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,19 @@ export interface ToolConfig {
enable: boolean;
path: string | undefined;
}
const ToolConfigDefault: ToolConfig = {args: [], enable: false, path: undefined};
const ToolConfigDefault: ToolConfig = {
args: [],
enable: false,
path: undefined,
};

export type ChplCheckConfig = ToolConfig;
const ChplCheckConfigDefault: ChplCheckConfig = {...ToolConfigDefault };
const ChplCheckConfigDefault: ChplCheckConfig = { ...ToolConfigDefault };

export interface CLSConfig extends ToolConfig {
resolver: boolean;
}
const CLSConfigDefault: CLSConfig = {...ToolConfigDefault, resolver: false};
const CLSConfigDefault: CLSConfig = { ...ToolConfigDefault, resolver: false };

const configScope = "chapel";

Expand All @@ -50,7 +54,7 @@ export function setChplHome(chplhome: string) {
// global local settings over the global remote settings. This is a known
// issue with the vscode API:
// https://github.com/microsoft/vscode/issues/182696
if(config.inspect("CHPL_HOME")?.workspaceValue !== undefined) {
if (config.inspect("CHPL_HOME")?.workspaceValue !== undefined) {
config.update("CHPL_HOME", chplhome, vscode.ConfigurationTarget.Workspace);
} else {
config.update("CHPL_HOME", chplhome, vscode.ConfigurationTarget.Global);
Expand All @@ -64,7 +68,8 @@ export function getChplDeveloper(): boolean {

export function getChplCheckConfig(): ChplCheckConfig {
const config = vscode.workspace.getConfiguration(configScope);
const chplcheck = config.get<ChplCheckConfig>("chplcheck") ?? ChplCheckConfigDefault;
const chplcheck =
config.get<ChplCheckConfig>("chplcheck") ?? ChplCheckConfigDefault;
return chplcheck;
}

Expand Down
30 changes: 24 additions & 6 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,27 @@ let chplcheckClient: ChplCheckClient;
let clsClient: CLSClient;
let logger: vscode.LogOutputChannel;

export function showInvalidPathWarning(
tool: string,
path: string,
errorString?: string
) {
if (errorString) {
logger.warn(errorString);
}

let msg = `The path '${path}' for ${tool} is invalid, errors may occur. Please double check that this is the correct path.`;
if (path === "") {
msg = `The path for ${tool} is not set, errors may occur. Please either set the path or remove the empty path.`;
}

vscode.window.showWarningMessage(msg, "Show Log", "Ok").then((value) => {
if (value === "Show Log") {
logger.show();
}
});
}

export function showChplHomeMissingError(errorString?: string) {
if (errorString) {
logger.error(errorString);
Expand All @@ -54,7 +75,8 @@ export function showChplHomeMissingError(errorString?: string) {
function pickMyOwnChplHome() {
vscode.window
.showInputBox({
placeHolder: "Enter the path to CHPL_HOME (possibly run `chpl --print-chpl-home` in the terminal to find it)",
placeHolder:
"Enter the path to CHPL_HOME (possibly run `chpl --print-chpl-home` in the terminal to find it)",
})
.then((selection) => {
if (selection !== undefined) {
Expand Down Expand Up @@ -100,11 +122,7 @@ export function activate(context: vscode.ExtensionContext) {
"chplcheck",
logger
);
clsClient = new CLSClient(
getCLSConfig(),
"chpl-language-server",
logger
);
clsClient = new CLSClient(getCLSConfig(), "chpl-language-server", logger);

// Restart language server command
context.subscriptions.push(
Expand Down

0 comments on commit abf17ae

Please sign in to comment.