Skip to content

Commit

Permalink
revert typecheck-silent, add diagonistic (multiple files)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaprieto committed Aug 30, 2024
1 parent e6a67a4 commit 3d32a30
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 15 deletions.
57 changes: 54 additions & 3 deletions src/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,64 @@

import * as vscode from 'vscode';
import * as user from './config';
import { isJuvixFile, runShellCommand } from './utils/base';
import { isJuvixFile, runShellCommand, getDiagnosticFromError } from './utils/base';
import { logger } from './utils/debug';
import {setJuvixCommandStatusBarItem, inProgressJuvixCommandStatusBar, showExecResultJuvixStatusBar } from './statusbar';
import { setJuvixCommandStatusBarItem, inProgressJuvixCommandStatusBar, showExecResultJuvixStatusBar } from './statusbar';

export async function activate(context: vscode.ExtensionContext) {
export async function activate(context: vscode.ExtensionContext, diagnosticCollection: vscode.DiagnosticCollection) {
const config = new user.JuvixConfig();

const command = 'juvix-mode.typecheck-silent';

const commandHandler = async (doc: vscode.TextDocument, content: string) => {
const activeEditor = vscode.window.activeTextEditor;

if (activeEditor && activeEditor.document == doc) {
if (doc && isJuvixFile(doc)) {
const filePath = doc.fileName;

const typecheckerCall = [
config.getJuvixExec(),
config.getGlobalFlags(),
'typecheck',
config.getTypeckeckFlags(),
filePath,
].join(' ');

inProgressJuvixCommandStatusBar('Typecheck');
const { stdout, stderr, status } = await runShellCommand(typecheckerCall, content);

if (status !== 0) {
showExecResultJuvixStatusBar(false, 'Typecheck', stderr);
const diag = getDiagnosticFromError(stderr);
if (diag)
diagnosticCollection.set(doc.uri, [diag]);
}
else {
showExecResultJuvixStatusBar(true, 'Typecheck', stdout);
diagnosticCollection.delete(doc.uri);
}
return { stdout, stderr, status };
}
}
return undefined;
};

context.subscriptions.push(
vscode.commands.registerCommand(command, commandHandler),
);


context.subscriptions.push(
vscode.workspace.onDidCloseTextDocument(doc => diagnosticCollection.delete(doc.uri))
);

context.subscriptions.push(
vscode.window.onDidChangeActiveTextEditor(_ => {
setJuvixCommandStatusBarItem();
}
));

switch (config.typecheckOn()) {
case 'change':
context.subscriptions.push(
Expand Down
6 changes: 5 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@ export async function activate(context: vscode.ExtensionContext) {
tasks,
repl,
judoc,
check,
formatter,
];
modules.forEach(module => module.activate(context));

let juvixDiagnosticCollection = vscode.languages.createDiagnosticCollection('juvix');
check.activate(context, juvixDiagnosticCollection);
context.subscriptions.push(juvixDiagnosticCollection);

vscode.commands.executeCommand('setContext', 'juvix-mode:ready', true);
}
}
12 changes: 1 addition & 11 deletions src/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import * as vscode from 'vscode';
import * as user from './config';
import { logger } from './utils/debug';
import { setJuvixCommandStatusBarItem, inProgressJuvixCommandStatusBar, showExecResultJuvixStatusBar } from './statusbar';
import { inProgressJuvixCommandStatusBar, showExecResultJuvixStatusBar } from './statusbar';

export const TASK_TYPE = 'Juvix';

Expand Down Expand Up @@ -109,12 +109,6 @@ export class JuvixTaskProvider implements vscode.TaskProvider {
group: vscode.TaskGroup.Build,
reveal: vscode.TaskRevealKind.Silent,
},
{
command: 'typecheck-silent',
args: [config.getTypeckeckFlags(), '${file}'],
group: vscode.TaskGroup.Build,
reveal: vscode.TaskRevealKind.Never,
},
{
command: 'compile',
args: [config.getCompilationFlags(), '${file}'],
Expand Down Expand Up @@ -233,10 +227,6 @@ export async function JuvixTask(
case 'update-dependencies':
exec = new vscode.ShellExecution(JuvixExec + `dependencies update`);
break;
case 'typecheck-silent':
args[0] = "typecheck";
exec = new vscode.ShellExecution(JuvixExec + ` ${args.join(' ')}`);
break;
default:
exec = new vscode.ShellExecution(JuvixExec + ` ${input}`);
break;
Expand Down
26 changes: 26 additions & 0 deletions src/utils/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,29 @@ export async function runShellCommand(command: string, input?: string): Promise<
});
});
}

const regexJuvixError = /(?<file>.*):(?<line>\d+):(?<begin_col>\d+)-?(?<end_col>\d+)?:\s(?<err_type>.*):\s(?<msg>((\n|.)*))/g;

export function getDiagnosticFromError(output: string): vscode.Diagnostic | undefined {
const { file, line, begin_col, end_col, err_type, msg } = regexJuvixError.exec(output)!.groups!;
if (!msg || !line || !begin_col) return undefined;
const range = new vscode.Range(
new vscode.Position(parseInt(line) - 1 , parseInt(begin_col) - 1 ),
new vscode.Position(parseInt(line) - 1 , parseInt(end_col ?? begin_col) - 1 ),
);
let severity;
switch (err_type) {
case 'error':
severity = vscode.DiagnosticSeverity.Error;
break;
case 'warning':
severity = vscode.DiagnosticSeverity.Warning;
break;
case 'info':
severity = vscode.DiagnosticSeverity.Information;
break;
}
let diag = new vscode.Diagnostic(range, msg, severity);
diag.source = 'Juvix';
return diag;
}

0 comments on commit 3d32a30

Please sign in to comment.