Skip to content

Commit

Permalink
Codebase maintenance (#95)
Browse files Browse the repository at this point in the history
This PR is for codebase maintenance:

- change debugChannel by logger, nb. for errors use logger.error.
- delete all traces, if you need to debug you can use, e.g.:
logger.trace|warn or the built-in debugger. Remember also set up the
`developer log level`.
- remove dead code in several places
- simplify several (small) functions
- run js prettier
- fix a bug for module name discovery.
  • Loading branch information
jonaprieto authored May 31, 2023
1 parent 522925b commit f3138af
Show file tree
Hide file tree
Showing 25 changed files with 340 additions and 413 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ repos:
exclude: assets/

- repo: https://github.com/pre-commit/mirrors-prettier
rev: 'v3.0.0-alpha.6' # Use the sha or tag you want to point at
rev: 'v3.0.0-alpha.9-for-vscode' # Use the sha or tag you want to point at
hooks:
- id: prettier
types_or: [css, javascript, markdown, yaml, toml]
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -555,13 +555,13 @@
"command": "juvix-mode.enableCodeLens",
"title": "Enable CodeLens",
"category": "Juvix",
"when": "editorLangId == juvix && editorTextFocus"
"when": "editorLangId == juvix && editorTextFocus"
},
{
{
"command": "juvix-mode.disableCodeLens",
"title": "Disable CodeLens",
"category": "Juvix",
"when": "editorLangId == juvix && editorTextFocus"
"when": "editorLangId == juvix && editorTextFocus"
}
],
"submenus": [
Expand Down
42 changes: 8 additions & 34 deletions snippets/Juvix.json
Original file line number Diff line number Diff line change
@@ -1,67 +1,41 @@
{
"module": {
"prefix": [
"mod",
"module"
],
"prefix": ["mod", "module"],
"body": "module ${1:moduleName};\n ${2:moduleBody}\n",
"description": "module type"
},
"data types": {
"prefix": [
"type",
"inductive",
"data"
],
"prefix": ["type", "inductive", "data"],
"body": "type ${1:typeName} := \n | ${3:constructorName} : ${4:constructorArgs} -> ${5:returnType}\n | ${6:constructorName} : ${7:constructorArgs} -> ${8:returnType};\n",
"description": "inductive data type declaration"
},
"axiom": {
"prefix": [
"axiom",
"postulate",
"definition"
],
"prefix": ["axiom", "postulate", "definition"],
"body": "\naxiom ${1:axiomName} : ${2:axiomType};\n",
"description": "Axiom"
},
"function": {
"prefix": [
"function"
],
"prefix": ["function"],
"body": "${1:functionName} : ${2:functionType};\n$1 := ${3:functionBody};\n",
"description": "Axiom"
},
"comment": {
"prefix": [
"comment",
"--"
],
"prefix": ["comment", "--"],
"body": "-- ${1:commentBody}\n",
"description": "comment inline"
},
"comment block": {
"prefix": [
"comment",
"--",
"{-"
],
"prefix": ["comment", "--", "{-"],
"body": "{-\n ${1:commentBody}\n-}",
"description": "comment block"
},
"judoc inline": {
"prefix": [
"judoc",
"---"
],
"prefix": ["judoc", "---"],
"body": "--- ${1:judoc inline}",
"description": "Judoc inline documentation"
},
"judoc block": {
"prefix": [
"judoc",
"{--"
],
"prefix": ["judoc", "{--"],
"body": "{--\n ${1:Judoc documenation}\n--}",
"description": "Judoc documenation"
}
Expand Down
4 changes: 2 additions & 2 deletions src/abbreviation/rewriter/AbbreviationRewriterFeature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { observable } from 'mobx';
import { Disposable, languages, TextEditor, window } from 'vscode';
import { autorunDisposable } from '../../utils/autorunDisposable';
import { debugChannel } from '../../utils/debug';
import { logger } from '../../utils/debug';
import { AbbreviationProvider } from '../AbbreviationProvider';
import { AbbreviationConfig } from '../config';
import { AbbreviationRewriter } from './AbbreviationRewriter';
Expand Down Expand Up @@ -42,7 +42,7 @@ export class AbbreviationRewriterFeature {
config,
abbreviationProvider,
this.activeTextEditor,
debugChannel
logger.outputChannel
)
);
}
Expand Down
14 changes: 6 additions & 8 deletions src/check.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 { isJuvixFile } from './utils/base';
import { debugChannel } from './utils/debug';
import { logger } from './utils/debug';
import { spawnSync } from 'child_process';

export async function activate(context: vscode.ExtensionContext) {
Expand All @@ -22,19 +22,14 @@ export async function activate(context: vscode.ExtensionContext) {
if (activeEditor && activeEditor.document == doc) {
if (doc && isJuvixFile(doc)) {
const filePath = doc.fileName;
debugChannel.info(`Checking... ${filePath}!`);
const typecheckerCall = [
config.getJuvixExec(),
config.getGlobalFlags(),
'--only-errors',
'typecheck',
filePath,
// '--stdin',
// content,
].join(' ');

debugChannel.info('Typecheker call: ' + typecheckerCall);

const ls = spawnSync(typecheckerCall, {
input: content,
shell: true,
Expand All @@ -43,10 +38,13 @@ export async function activate(context: vscode.ExtensionContext) {

if (ls.status !== 0) {
const errMsg: string = "Juvix's Error: " + ls.stderr.toString();
debugChannel.error('typechecking-silent provider error', errMsg);
logger.error(
'typechecking-silent provider error' + errMsg,
'check.ts'
);
throw new Error(errMsg);
}
const stdout = ls.stdout;
return ls.stdout;
}
}
};
Expand Down
158 changes: 87 additions & 71 deletions src/codelens.ts
Original file line number Diff line number Diff line change
@@ -1,74 +1,87 @@
import * as vscode from 'vscode';
import * as statusbar from './statusbar';
import { getModuleName } from './module';
import { isJuvixFile } from './utils/base';

/**
* CodelensProvider
*/

export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(
vscode.languages.registerCodeLensProvider(
{ scheme: 'file', language: 'Juvix' },
new CodelensProvider()
)
);
context.subscriptions.push(
vscode.commands.registerCommand("juvix-mode.enableCodeLens", () => {
vscode.workspace.getConfiguration("juvix-mode").update("enableCodeLens", true, true);
})
);
context.subscriptions.push(
vscode.commands.registerCommand("juvix-mode.disableCodeLens", () => {
vscode.workspace.getConfiguration("juvix-mode").update("enableCodeLens", false, true);
})
);
context.subscriptions.push(
vscode.languages.registerCodeLensProvider(
{ scheme: 'file', language: 'Juvix' },
new CodelensProvider()
)
);
context.subscriptions.push(
vscode.commands.registerCommand('juvix-mode.enableCodeLens', () => {
vscode.workspace
.getConfiguration('juvix-mode')
.update('enableCodeLens', true, true);
})
);
context.subscriptions.push(
vscode.commands.registerCommand('juvix-mode.disableCodeLens', () => {
vscode.workspace
.getConfiguration('juvix-mode')
.update('enableCodeLens', false, true);
})
);

context.subscriptions.push(
vscode.commands.registerCommand("juvix-mode.aux.prependText", (args: any) => {
const editor = vscode.window.activeTextEditor;
if (editor) {
const position = new vscode.Position(0, 0);
editor.edit((editBuilder) => {
editBuilder.insert(position, args.text);
});
}
})
);
context.subscriptions.push(
vscode.commands.registerCommand(
'juvix-mode.aux.prependText',
(args: any) => {
const editor = vscode.window.activeTextEditor;
if (editor) {
const position = new vscode.Position(0, 0);
editor.edit(editBuilder => {
editBuilder.insert(position, args.text);
});
}
}
)
);
}

export class CodelensProvider implements vscode.CodeLensProvider {
private codeLenses: vscode.CodeLens[] = [];
private _onDidChangeCodeLenses: vscode.EventEmitter<void> =
new vscode.EventEmitter<void>();
public readonly onDidChangeCodeLenses: vscode.Event<void> =
this._onDidChangeCodeLenses.event;

private codeLenses: vscode.CodeLens[] = [];
private _onDidChangeCodeLenses: vscode.EventEmitter<void> = new vscode.EventEmitter<void>();
public readonly onDidChangeCodeLenses: vscode.Event<void> = this._onDidChangeCodeLenses.event;
constructor() {
vscode.workspace.onDidChangeConfiguration(_ => {
this._onDidChangeCodeLenses.fire();
});
}

constructor() {
vscode.workspace.onDidChangeConfiguration((_) => {
this._onDidChangeCodeLenses.fire();
});
public provideCodeLenses(
document: vscode.TextDocument,
_token: vscode.CancellationToken
): vscode.CodeLens[] | Thenable<vscode.CodeLens[]> {
if (!isJuvixFile(document)) {
return [];
}

public provideCodeLenses(
document: vscode.TextDocument
, _token: vscode.CancellationToken)
: vscode.CodeLens[] | Thenable<vscode.CodeLens[]> {
if (vscode.workspace.getConfiguration('juvix-mode').get('codeLens', true)) {
this.codeLenses = [];

if (vscode.workspace.getConfiguration("juvix-mode").get("codeLens", true)) {
this.codeLenses = [];
const text = document.getText();
let firstLineRange = document.lineAt(0).range;
/*
const text = document.getText();
let firstLineRange = document.lineAt(0).range;
/*
Add a code lenses to show the Juvix version
in the first line of the document.
*/
let juvixVersionCodeLenses = new vscode.CodeLens(firstLineRange, {
title: "Powered by " + statusbar.juvixStatusBarItemVersion.text,
command: ""
});
this.codeLenses.push(juvixVersionCodeLenses);
let juvixVersionCodeLenses = new vscode.CodeLens(firstLineRange, {
title: 'Powered by ' + statusbar.juvixStatusBarItemVersion.text,
command: '',
});
this.codeLenses.push(juvixVersionCodeLenses);

/*
/*
Add a code lenses that checks if the document is empty.
If it is, it suggests to insert the module header
relative to juvixRoot.
Expand All @@ -78,29 +91,32 @@ export class CodelensProvider implements vscode.CodeLensProvider {
and the slashes are replaced by dots.
*/

let regex = /module\s+([\w.]+);/;
let match = text.match(regex);
let moduleName: string | undefined = getModuleName(document);
if (moduleName && (text.length === 0 || match === null)) {
let moduleTopHeader: string = `module ${moduleName};`;
let insertModuleCodeLenses = new vscode.CodeLens(
new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 0)),
{
title: `Insert "${moduleTopHeader}"`,
command: "juvix-mode.aux.prependText",
arguments: [
{
text: `${moduleTopHeader}\n\n`
}
]
}
);
let regex = /module\s+([\w.]+);/;
let match = text.match(regex);
let moduleName: string | undefined = getModuleName(document);
if (moduleName && (text.length === 0 || match === null)) {
let moduleTopHeader: string = `module ${moduleName};`;
let insertModuleCodeLenses = new vscode.CodeLens(
new vscode.Range(
new vscode.Position(0, 0),
new vscode.Position(0, 0)
),
{
title: `Insert "${moduleTopHeader}"`,
command: 'juvix-mode.aux.prependText',
arguments: [
{
text: `${moduleTopHeader}\n\n`,
},
],
}
);

this.codeLenses = [insertModuleCodeLenses].concat(this.codeLenses);
}
this.codeLenses = [insertModuleCodeLenses].concat(this.codeLenses);
}

return this.codeLenses;
}
return [];
return this.codeLenses;
}
return [];
}
}
Loading

0 comments on commit f3138af

Please sign in to comment.