Skip to content

Commit

Permalink
Fix a number of strict null check issues in lsp code
Browse files Browse the repository at this point in the history
  • Loading branch information
corasaurus-hex committed May 1, 2022
1 parent a58fbf1 commit 30cc621
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 25 deletions.
23 changes: 18 additions & 5 deletions src/lsp/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,25 @@ async function getLatestVersion(): Promise<string> {
}
}

const zipFileNames = {
darwin: 'clojure-lsp-native-macos-amd64.zip',
linux: 'clojure-lsp-native-static-linux-amd64.zip',
win32: 'clojure-lsp-native-windows-amd64.zip',
};

const validPlatforms = Object.keys(zipFileNames);
type ValidPlatform = keyof typeof zipFileNames;

function assertValidPlatform(platform: string): asserts platform is ValidPlatform {
if (!validPlatforms.includes(platform)) {
throw new Error(`Expected a valid clojure-lsp platform but got ${platform}`);
}
}

function getZipFileName(platform: string): string {
return {
darwin: 'clojure-lsp-native-macos-amd64.zip',
linux: 'clojure-lsp-native-static-linux-amd64.zip',
win32: 'clojure-lsp-native-windows-amd64.zip',
}[platform];
assertValidPlatform(platform);

return zipFileNames[platform];
}

function getZipFilePath(extensionPath: string, platform: string): string {
Expand Down
55 changes: 35 additions & 20 deletions src/lsp/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const SERVER_NOT_RUNNING_OR_INITIALIZED_MESSAGE =
const lspStatusItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 0);
let serverVersion: string;
let extensionContext: vscode.ExtensionContext;
let clojureLspPath: string;
let clojureLspPath: string | undefined;
let testTreeHandler: TestTreeHandler;
let lspCommandsRegistered = false;

Expand Down Expand Up @@ -124,7 +124,11 @@ function createClient(clojureLspPath: string, fallbackFolder: FallbackFolder): L
provideCodeActions(document, range, context, token, next) {
return next(document, range, context, token);
},
provideCodeLenses: async (document, token, next): Promise<vscode.CodeLens[]> => {
provideCodeLenses: async (
document,
token,
next
): Promise<vscode.CodeLens[] | null | undefined> => {
if (config.getConfig().referencesCodeLensEnabled) {
return await next(document, token);
}
Expand All @@ -137,7 +141,7 @@ function createClient(clojureLspPath: string, fallbackFolder: FallbackFolder): L
return null;
},
async provideHover(document, position, token, next) {
let hover: vscode.Hover;
let hover: vscode.Hover | null | undefined;
try {
hover = await provideHover(document, position);
} catch {
Expand Down Expand Up @@ -266,7 +270,7 @@ function registerLspCommand(command: ClojureLspCommand): vscode.Disposable {
const docUri = `${document.uri.scheme}://${document.uri.path}`;
const params = [docUri, line, column];
const extraParam = command.extraParamFn ? await command.extraParamFn() : undefined;
if (!command.extraParamFn || (command.extraParamFn && extraParam)) {
if (!command.extraParamFn || extraParam) {
sendCommandRequest(command.command, extraParam ? [...params, extraParam] : params);
}
}
Expand Down Expand Up @@ -360,10 +364,12 @@ enum FolderType {
FROM_NO_CLOJURE_FILE = 3,
}

type FallbackFolder = {
uri: vscode.Uri;
type: FolderType;
};
type FallbackFolder =
| { uri: undefined; type: FolderType.VSCODE }
| {
uri: vscode.Uri;
type: FolderType;
};

/**
* Figures out a ”best fit” rootUri for use when starting the clojure-lsp
Expand All @@ -382,8 +388,8 @@ async function getFallbackFolder(): Promise<FallbackFolder> {
}

const activeEditor = vscode.window.activeTextEditor;
let clojureFilePath: string;
let folderType: FolderType;
let clojureFilePath: string | undefined;
let folderType: FolderType | undefined;
if (activeEditor && activeEditor.document?.languageId === 'clojure') {
folderType = activeEditor.document.isUntitled
? FolderType.FROM_UNTITLED_FILE
Expand All @@ -394,8 +400,10 @@ async function getFallbackFolder(): Promise<FallbackFolder> {
} else {
for (const document of vscode.workspace.textDocuments) {
if (document.languageId === 'clojure') {
folderType = document.isUntitled ? FolderType.FROM_UNTITLED_FILE : FolderType.FROM_FS_FILE;
if (!document.isUntitled) {
if (document.isUntitled) {
folderType = FolderType.FROM_UNTITLED_FILE;
} else {
folderType = FolderType.FROM_FS_FILE;
clojureFilePath = document.uri.fsPath;
}
}
Expand All @@ -422,6 +430,8 @@ async function getFallbackFolder(): Promise<FallbackFolder> {
}
}

util.assertIsDefined(folderType, 'Expected there to be a folderType at this point!');

return {
uri: fallbackFolder,
type: folderType,
Expand Down Expand Up @@ -477,6 +487,7 @@ async function startClient(fallbackFolder: FallbackFolder): Promise<boolean> {
});
}
setStateValue(LSP_CLIENT_KEY, undefined);
util.assertIsDefined(clojureLspPath, 'Expected there to be a clojure LSP path!');
const client = createClient(clojureLspPath, fallbackFolder);
console.log('Starting clojure-lsp at', clojureLspPath);

Expand Down Expand Up @@ -515,11 +526,14 @@ async function startClient(fallbackFolder: FallbackFolder): Promise<boolean> {

// A quickPick that expects the same input as showInformationMessage does
// TODO: How do we make it satisfy the messageFunc interface above?
function quickPick(message: string, actions: { title: string }[]): Promise<{ title: string }> {
function quickPick(
message: string,
actions: { title: string }[]
): Promise<{ title: string } | undefined> {
const qp = vscode.window.createQuickPick();
qp.items = actions.map((item) => ({ label: item.title }));
qp.title = message;
return new Promise<{ title: string }>((resolve, _reject) => {
return new Promise<{ title: string } | undefined>((resolve, _reject) => {
qp.show();
qp.onDidAccept(() => {
if (qp.selectedItems.length > 0) {
Expand Down Expand Up @@ -649,7 +663,7 @@ async function activate(context: vscode.ExtensionContext, handler: TestTreeHandl
}
}

async function maybeDownloadLspServer(forceDownLoad = false): Promise<string> {
async function maybeDownloadLspServer(forceDownLoad = false): Promise<string | undefined> {
const userConfiguredClojureLspPath = config.getConfig().clojureLspPath;
if (userConfiguredClojureLspPath !== '') {
clojureLspPath = userConfiguredClojureLspPath;
Expand All @@ -672,11 +686,12 @@ async function downloadLSPServerCommand() {

async function ensureServerDownloaded(forceDownLoad = false): Promise<string> {
const currentVersion = readVersionFile(extensionContext.extensionPath);
const configuredVersion: string = config.getConfig().clojureLspVersion;
const configuredVersion: string | undefined = config.getConfig().clojureLspVersion;
clojureLspPath = getClojureLspPath(extensionContext.extensionPath, util.isWindows);
const downloadVersion = ['', 'latest'].includes(configuredVersion)
? await getLatestVersion()
: configuredVersion;
const downloadVersion =
configuredVersion === undefined || ['', 'latest'].includes(configuredVersion)
? await getLatestVersion()
: configuredVersion;
if (
(currentVersion !== downloadVersion && downloadVersion !== '') ||
forceDownLoad ||
Expand Down Expand Up @@ -787,7 +802,7 @@ export async function getCljFmtConfig(): Promise<string | undefined> {

function showMenu(items: vscode.QuickPickItem[], commands: Record<string, string>) {
void vscode.window.showQuickPick(items, { title: 'clojure-lsp' }).then((v) => {
if (commands[v.label]) {
if (v && commands[v.label]) {
void vscode.commands.executeCommand(commands[v.label]);
}
});
Expand Down

0 comments on commit 30cc621

Please sign in to comment.