From f128d116f8e8265b21afd37bfe6fe6839c03587d Mon Sep 17 00:00:00 2001 From: Jonathan Cubides Date: Fri, 26 May 2023 14:34:45 +0200 Subject: [PATCH 01/19] Add codelenses --- src/codelens.ts | 109 +++++++++++++++++++++++++++++++++++++++++++++++ src/extension.ts | 4 ++ src/judoc.ts | 29 +++---------- src/root.ts | 32 ++++++++++++++ src/statusbar.ts | 1 - 5 files changed, 152 insertions(+), 23 deletions(-) create mode 100644 src/codelens.ts create mode 100644 src/root.ts diff --git a/src/codelens.ts b/src/codelens.ts new file mode 100644 index 0000000..239e285 --- /dev/null +++ b/src/codelens.ts @@ -0,0 +1,109 @@ +import * as vscode from 'vscode'; +import * as statusbar from './statusbar'; +import { juvixRoot } from './root'; +import { isJuvixFile } from './utils/base'; +/** + * CodelensProvider + */ + +export function activate(context: vscode.ExtensionContext) { + statusbar.activate(context); + 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.codelensAction", (args: any) => { + vscode.window.showInformationMessage(`CodeLens action clicked with args=${args}`); + }) + ); +} + +export class CodelensProvider implements vscode.CodeLensProvider { + + private codeLenses: vscode.CodeLens[] = []; + private _onDidChangeCodeLenses: vscode.EventEmitter = new vscode.EventEmitter(); + public readonly onDidChangeCodeLenses: vscode.Event = this._onDidChangeCodeLenses.event; + + constructor() { + vscode.workspace.onDidChangeConfiguration((_) => { + this._onDidChangeCodeLenses.fire(); + }); + } + + public provideCodeLenses( + document: vscode.TextDocument + , _token: vscode.CancellationToken) + : vscode.CodeLens[] | Thenable { + + if (vscode.workspace + .getConfiguration("JuvixCodeLens") + .get("enableCodeLens", true)) { + + this.codeLenses = []; + const text = document.getText(); + + /* + Add a code lenses that checks if the document is empty. + If it is, it suggests to insert the module header + relative to juvixRoot. + The content inserted by the code lenses should be: + "module ;" + where document filepath relative to juvixRoot + and the slashes are replaced by dots. + */ + let firstLineRange = document.lineAt(0).range; + // check if the file doesnn't define a module. This check is not perfe + const regex = /module\s+([\w.]+);/; + const match = text.match(regex); + const noModule = text.length === 0 || match === null; + if (noModule && + !document.isUntitled && + isJuvixFile(document) && + juvixRoot !== undefined) { + let moduleName = document.fileName; + moduleName = moduleName + .replace(juvixRoot, "") + .replace(".juvix", "") + .replace(/\\/g, ".") + .replace(/\//g, "."); + // insert the snippet in the first line of the document + + let insertModuleCodeLenses = new vscode.CodeLens( + new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 0)) + , { + title: "Insert \"module " + moduleName + ";\"", + command: "" + }); + + this.codeLenses.push(insertModuleCodeLenses); + } + + /* + 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); + + return this.codeLenses; + } + return []; + } + +} diff --git a/src/extension.ts b/src/extension.ts index d061f75..ac4de66 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -17,10 +17,14 @@ import * as dev from './dev'; import * as check from './check'; import * as formatter from './formatter'; import * as vampir from './vampir/tasks'; +import * as codelens from './codelens'; +import * as root from './root'; export async function activate(context: vscode.ExtensionContext) { debugChannel.clear(); statusBar.activate(context); + root.activate(context); + codelens.activate(context); syntaxHighlighter.activate(context); goToDefinition.activate(context); hoverInfo.activate(context); diff --git a/src/judoc.ts b/src/judoc.ts index fc7dfa4..c8ef0ea 100644 --- a/src/judoc.ts +++ b/src/judoc.ts @@ -2,11 +2,11 @@ * Copyright (C) Microsoft Corporation. All rights reserved. *--------------------------------------------------------*/ import * as vscode from 'vscode'; -import * as path from 'path'; import * as fs from 'fs'; import { debugChannel } from './utils/debug'; import { JuvixConfig } from './config'; import { isJuvixFile } from './utils/base'; +import { juvixRoot } from './root'; export function activate(context: vscode.ExtensionContext) { context.subscriptions.push( @@ -249,21 +249,6 @@ export class JudocPanel { throw new Error(errMsg); } - const juvixRootCall = [ - config.getJuvixExec(), - config.getGlobalFlags(), - 'dev', - 'root', - doc.uri.fsPath, - ].join(' '); - - const rootRun = spawnSync(juvixRootCall, { shell: true, encoding: 'utf8' }); - if (rootRun.status !== 0) { - const errMsg: string = "Juvix's Error: " + rootRun.stderr.toString(); - debugChannel.error('Juvix root failed for Judoc gen:', errMsg); - throw new Error(errMsg); - } - const juvixRoot = rootRun.stdout.toString().trim(); const htmlFilename = doc.uri.fsPath .replace(juvixRoot, '') .replace('/', '.') @@ -284,12 +269,12 @@ export class JudocPanel { return contentDisk.replace( '', '' + webview.cspSource + + '; img-src ' + + webview.cspSource + + " https:; script-src 'nonce-" + + nonce + + '\';">' ); } } diff --git a/src/root.ts b/src/root.ts new file mode 100644 index 0000000..ab63a63 --- /dev/null +++ b/src/root.ts @@ -0,0 +1,32 @@ +/*--------------------------------------------------------- + * Copyright (C) Microsoft Corporation. All rights reserved. + *--------------------------------------------------------*/ +import * as vscode from 'vscode'; +import { debugChannel } from './utils/debug'; +import { JuvixConfig } from './config'; + +export let juvixRoot: string; + +export function activate(_context: vscode.ExtensionContext) { + const config = new JuvixConfig(); + const { spawnSync } = require('child_process'); + const doc = vscode.window.activeTextEditor?.document; + juvixRoot = ''; + if (doc) { + const juvixRootCall = [ + config.getJuvixExec(), + config.getGlobalFlags(), + 'dev', + 'root', + doc.uri.fsPath, + ].join(' '); + + const rootRun = spawnSync(juvixRootCall, { shell: true, encoding: 'utf8' }); + if (rootRun.status !== 0) { + const errMsg: string = "Juvix's Error: " + rootRun.stderr.toString(); + debugChannel.error('Juvix root failed for Judoc gen:', errMsg); + throw new Error(errMsg); + } + juvixRoot = rootRun.stdout.toString().trim(); + } +} \ No newline at end of file diff --git a/src/statusbar.ts b/src/statusbar.ts index 2061114..7fa671f 100644 --- a/src/statusbar.ts +++ b/src/statusbar.ts @@ -11,7 +11,6 @@ import * as utils from './utils/base'; export let juvixStatusBarItemVersion: vscode.StatusBarItem; export function activate(context: vscode.ExtensionContext) { - const config = new user.JuvixConfig(); juvixStatusBarItemVersion = vscode.window.createStatusBarItem( vscode.StatusBarAlignment.Right ); From af3653ee6631e30c0e57610af24a0e4b37b208de Mon Sep 17 00:00:00 2001 From: Jonathan Cubides Date: Fri, 26 May 2023 14:37:38 +0200 Subject: [PATCH 02/19] make pre-commit happy --- src/codelens.ts | 2 +- src/root.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/codelens.ts b/src/codelens.ts index 239e285..40c6779 100644 --- a/src/codelens.ts +++ b/src/codelens.ts @@ -92,7 +92,7 @@ export class CodelensProvider implements vscode.CodeLensProvider { } /* - Add a code lenses to show the Juvix version + Add a code lenses to show the Juvix version in the first line of the document. */ let juvixVersionCodeLenses = new vscode.CodeLens(firstLineRange, { diff --git a/src/root.ts b/src/root.ts index ab63a63..4ab055e 100644 --- a/src/root.ts +++ b/src/root.ts @@ -29,4 +29,4 @@ export function activate(_context: vscode.ExtensionContext) { } juvixRoot = rootRun.stdout.toString().trim(); } -} \ No newline at end of file +} From 8dfd4470f9b318b0e950e3651cafc65c32b809e1 Mon Sep 17 00:00:00 2001 From: Jonathan Cubides Date: Fri, 26 May 2023 17:36:26 +0200 Subject: [PATCH 03/19] Fixed aux command --- src/codelens.ts | 26 +++++++++++++++++++++----- src/extension.ts | 2 +- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/codelens.ts b/src/codelens.ts index 40c6779..41545a0 100644 --- a/src/codelens.ts +++ b/src/codelens.ts @@ -7,7 +7,6 @@ import { isJuvixFile } from './utils/base'; */ export function activate(context: vscode.ExtensionContext) { - statusbar.activate(context); context.subscriptions.push( vscode.languages.registerCodeLensProvider( { scheme: 'file', language: 'Juvix' }, @@ -29,6 +28,18 @@ export function activate(context: vscode.ExtensionContext) { vscode.window.showInformationMessage(`CodeLens action clicked with args=${args}`); }) ); + + 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 { @@ -65,7 +76,7 @@ export class CodelensProvider implements vscode.CodeLensProvider { and the slashes are replaced by dots. */ let firstLineRange = document.lineAt(0).range; - // check if the file doesnn't define a module. This check is not perfe + const regex = /module\s+([\w.]+);/; const match = text.match(regex); const noModule = text.length === 0 || match === null; @@ -79,13 +90,18 @@ export class CodelensProvider implements vscode.CodeLensProvider { .replace(".juvix", "") .replace(/\\/g, ".") .replace(/\//g, "."); - // insert the snippet in the first line of the document + const moduleTopHeader = "module " + moduleName + ";"; let insertModuleCodeLenses = new vscode.CodeLens( new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 0)) , { - title: "Insert \"module " + moduleName + ";\"", - command: "" + title: "Insert \"" + moduleTopHeader + "\"", + command: "juvix-mode.aux.prependText", + arguments: [ + { + text: moduleTopHeader + "\n" + } + ] }); this.codeLenses.push(insertModuleCodeLenses); diff --git a/src/extension.ts b/src/extension.ts index ac4de66..6221a71 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -24,7 +24,6 @@ export async function activate(context: vscode.ExtensionContext) { debugChannel.clear(); statusBar.activate(context); root.activate(context); - codelens.activate(context); syntaxHighlighter.activate(context); goToDefinition.activate(context); hoverInfo.activate(context); @@ -36,6 +35,7 @@ export async function activate(context: vscode.ExtensionContext) { formatter.activate(context); vampir.activate(context); dev.activate(context); + codelens.activate(context); debugChannel.info('Juvix extension is ready!'); } From 7c76bf3fe806741bc534ad198519233d2222cd03 Mon Sep 17 00:00:00 2001 From: Jonathan Cubides Date: Fri, 26 May 2023 17:36:47 +0200 Subject: [PATCH 04/19] Make pre-commit happy --- src/codelens.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/codelens.ts b/src/codelens.ts index 41545a0..a941414 100644 --- a/src/codelens.ts +++ b/src/codelens.ts @@ -76,7 +76,7 @@ export class CodelensProvider implements vscode.CodeLensProvider { and the slashes are replaced by dots. */ let firstLineRange = document.lineAt(0).range; - + const regex = /module\s+([\w.]+);/; const match = text.match(regex); const noModule = text.length === 0 || match === null; From 8cc3264c7bbbd6c9310ba4738ddc5794cd849a33 Mon Sep 17 00:00:00 2001 From: Jonathan Cubides Date: Sat, 27 May 2023 13:20:25 +0200 Subject: [PATCH 05/19] forgot to push this one --- src/codelens.ts | 18 ++++++++---------- src/extension.ts | 2 -- src/judoc.ts | 4 ++-- src/root.ts | 8 +++----- 4 files changed, 13 insertions(+), 19 deletions(-) diff --git a/src/codelens.ts b/src/codelens.ts index a941414..2c4e663 100644 --- a/src/codelens.ts +++ b/src/codelens.ts @@ -2,6 +2,7 @@ import * as vscode from 'vscode'; import * as statusbar from './statusbar'; import { juvixRoot } from './root'; import { isJuvixFile } from './utils/base'; +import { debugChannel } from './utils/debug'; /** * CodelensProvider */ @@ -76,25 +77,24 @@ export class CodelensProvider implements vscode.CodeLensProvider { and the slashes are replaced by dots. */ let firstLineRange = document.lineAt(0).range; - const regex = /module\s+([\w.]+);/; const match = text.match(regex); - const noModule = text.length === 0 || match === null; - if (noModule && - !document.isUntitled && + const projRoot = juvixRoot(); + if (text.length === 0 && isJuvixFile(document) && - juvixRoot !== undefined) { + projRoot !== undefined + && match === null) { let moduleName = document.fileName; moduleName = moduleName - .replace(juvixRoot, "") + .replace(projRoot, "") .replace(".juvix", "") .replace(/\\/g, ".") .replace(/\//g, "."); const moduleTopHeader = "module " + moduleName + ";"; let insertModuleCodeLenses = new vscode.CodeLens( - new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 0)) - , { + new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 0)), + { title: "Insert \"" + moduleTopHeader + "\"", command: "juvix-mode.aux.prependText", arguments: [ @@ -103,7 +103,6 @@ export class CodelensProvider implements vscode.CodeLensProvider { } ] }); - this.codeLenses.push(insertModuleCodeLenses); } @@ -116,7 +115,6 @@ export class CodelensProvider implements vscode.CodeLensProvider { command: "" }); this.codeLenses.push(juvixVersionCodeLenses); - return this.codeLenses; } return []; diff --git a/src/extension.ts b/src/extension.ts index 6221a71..3878488 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -18,12 +18,10 @@ import * as check from './check'; import * as formatter from './formatter'; import * as vampir from './vampir/tasks'; import * as codelens from './codelens'; -import * as root from './root'; export async function activate(context: vscode.ExtensionContext) { debugChannel.clear(); statusBar.activate(context); - root.activate(context); syntaxHighlighter.activate(context); goToDefinition.activate(context); hoverInfo.activate(context); diff --git a/src/judoc.ts b/src/judoc.ts index c8ef0ea..e78c8dd 100644 --- a/src/judoc.ts +++ b/src/judoc.ts @@ -248,9 +248,9 @@ export class JudocPanel { debugChannel.error('Judoc failed', errMsg); throw new Error(errMsg); } - + const projRoot = juvixRoot(); const htmlFilename = doc.uri.fsPath - .replace(juvixRoot, '') + .replace(projRoot, '') .replace('/', '.') .replace('.juvix', '.html'); diff --git a/src/root.ts b/src/root.ts index 4ab055e..dff849b 100644 --- a/src/root.ts +++ b/src/root.ts @@ -5,13 +5,10 @@ import * as vscode from 'vscode'; import { debugChannel } from './utils/debug'; import { JuvixConfig } from './config'; -export let juvixRoot: string; - -export function activate(_context: vscode.ExtensionContext) { +export function juvixRoot() { const config = new JuvixConfig(); const { spawnSync } = require('child_process'); const doc = vscode.window.activeTextEditor?.document; - juvixRoot = ''; if (doc) { const juvixRootCall = [ config.getJuvixExec(), @@ -27,6 +24,7 @@ export function activate(_context: vscode.ExtensionContext) { debugChannel.error('Juvix root failed for Judoc gen:', errMsg); throw new Error(errMsg); } - juvixRoot = rootRun.stdout.toString().trim(); + return rootRun.stdout.toString().trim(); } + return undefined; } From 801c2dc9b6e2d93f3fd4b4cd7b76fb28c188cb3a Mon Sep 17 00:00:00 2001 From: Jonathan Cubides Date: Tue, 30 May 2023 13:01:46 +0200 Subject: [PATCH 06/19] Added refactor and fixes --- package.json | 17 +++++++++ src/codelens.ts | 89 ++++++++++++++++++++++++------------------------ src/root.ts | 15 ++++++++ src/statusbar.ts | 2 -- 4 files changed, 77 insertions(+), 46 deletions(-) diff --git a/package.json b/package.json index 804ae00..9be38ce 100644 --- a/package.json +++ b/package.json @@ -550,6 +550,18 @@ "args": "Juvix: [dev] geb-compile", "when": "editorLangId == JuvixGeb && editorTextFocus", "icon": "$(debug-start)" + }, + { + "command": "juvix-mode.enableCodeLens", + "title": "Enable CodeLens", + "category": "Juvix", + "when": "editorLangId == juvix && editorTextFocus" + }, + { + "command": "juvix-mode.disableCodeLens", + "title": "Disable CodeLens", + "category": "Juvix", + "when": "editorLangId == juvix && editorTextFocus" } ], "submenus": [ @@ -726,6 +738,11 @@ "default": "false", "description": "Reload REPL on save" }, + "juvix-mode.codeLens": { + "type": "boolean", + "default": true, + "description": "Enable code lens" + }, "juvix-mode.opts": { "type": "object", "order": 1, diff --git a/src/codelens.ts b/src/codelens.ts index 2c4e663..145e1f2 100644 --- a/src/codelens.ts +++ b/src/codelens.ts @@ -1,8 +1,11 @@ import * as vscode from 'vscode'; import * as statusbar from './statusbar'; -import { juvixRoot } from './root'; +import { juvixRoot, globalJuvixRoot } from './root'; import { isJuvixFile } from './utils/base'; import { debugChannel } from './utils/debug'; +import * as path from 'path'; +import { debug } from 'console'; + /** * CodelensProvider */ @@ -24,11 +27,6 @@ export function activate(context: vscode.ExtensionContext) { vscode.workspace.getConfiguration("juvix-mode").update("enableCodeLens", false, true); }) ); - context.subscriptions.push( - vscode.commands.registerCommand("juvix-mode.codelensAction", (args: any) => { - vscode.window.showInformationMessage(`CodeLens action clicked with args=${args}`); - }) - ); context.subscriptions.push( vscode.commands.registerCommand("juvix-mode.aux.prependText", (args: any) => { @@ -60,61 +58,64 @@ export class CodelensProvider implements vscode.CodeLensProvider { , _token: vscode.CancellationToken) : vscode.CodeLens[] | Thenable { - if (vscode.workspace - .getConfiguration("JuvixCodeLens") - .get("enableCodeLens", true)) { + + if (vscode.workspace.getConfiguration("juvix-mode").get("codeLens", true)) { this.codeLenses = []; const text = document.getText(); + const parsedFilepath = path.parse(document.fileName); + + let firstLineRange = document.lineAt(0).range; /* - Add a code lenses that checks if the document is empty. - If it is, it suggests to insert the module header - relative to juvixRoot. - The content inserted by the code lenses should be: - "module ;" - where document filepath relative to juvixRoot - and the slashes are replaced by dots. + Add a code lenses to show the Juvix version + in the first line of the document. */ - let firstLineRange = document.lineAt(0).range; - const regex = /module\s+([\w.]+);/; - const match = text.match(regex); - const projRoot = juvixRoot(); - if (text.length === 0 && - isJuvixFile(document) && - projRoot !== undefined - && match === null) { - let moduleName = document.fileName; - moduleName = moduleName - .replace(projRoot, "") - .replace(".juvix", "") - .replace(/\\/g, ".") - .replace(/\//g, "."); - const moduleTopHeader = "module " + moduleName + ";"; + 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. + The content inserted by the code lenses should be: + "module ;" + where document filepath relative to juvixRoot + and the slashes are replaced by dots. + */ + + + let regex = /module\s+([\w.]+);/; + let match = text.match(regex); + let projRoot = juvixRoot(); + + if (isJuvixFile(document) && (text.length === 0 || match === null)) { + let relativeModulePath: string = path.relative(projRoot, parsedFilepath.dir).replace(path.sep, "."); + let moduleName: string = + (projRoot === globalJuvixRoot()) ? + parsedFilepath.name : + `${relativeModulePath}${relativeModulePath.length > 0 ? '.' : ''}${parsedFilepath.name}`; + 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 + "\"", + title: `Insert "${moduleTopHeader}"`, command: "juvix-mode.aux.prependText", arguments: [ { - text: moduleTopHeader + "\n" + text: `${moduleTopHeader}\n\n` } ] - }); - this.codeLenses.push(insertModuleCodeLenses); + } + ); + + this.codeLenses = [insertModuleCodeLenses].concat(this.codeLenses); } - /* - 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); + return this.codeLenses; } return []; diff --git a/src/root.ts b/src/root.ts index dff849b..be9dfb5 100644 --- a/src/root.ts +++ b/src/root.ts @@ -4,6 +4,8 @@ import * as vscode from 'vscode'; import { debugChannel } from './utils/debug'; import { JuvixConfig } from './config'; +import { getInstalledNumericVersion } from './juvixVersion'; +import * as path from 'path'; export function juvixRoot() { const config = new JuvixConfig(); @@ -28,3 +30,16 @@ export function juvixRoot() { } return undefined; } + + +export function globalJuvixRoot() : string { + const juvixVersion = getInstalledNumericVersion(); + const homeUserPath = process.env.HOME; + if (!homeUserPath) { + throw new Error('Cannot find home directory'); + } + if (!juvixVersion) { + throw new Error('Cannot find Juvix version'); + } + return path.join(homeUserPath, '.config', 'juvix', juvixVersion, 'global-project'); +} diff --git a/src/statusbar.ts b/src/statusbar.ts index 7fa671f..533e2a9 100644 --- a/src/statusbar.ts +++ b/src/statusbar.ts @@ -3,8 +3,6 @@ *--------------------------------------------------------*/ 'use strict'; import * as vscode from 'vscode'; -import { debugChannel } from './utils/debug'; -import * as user from './config'; import * as version from './juvixVersion'; import * as utils from './utils/base'; From 923843409d7e36cd3e8b0c6b10b9104b3fad94e4 Mon Sep 17 00:00:00 2001 From: Jonathan Cubides Date: Tue, 30 May 2023 17:59:12 +0200 Subject: [PATCH 07/19] Fix when root is global --- src/codelens.ts | 25 +++++++++++++------------ src/highlighting.ts | 4 ++-- src/root.ts | 2 +- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/codelens.ts b/src/codelens.ts index 145e1f2..9a0eb9d 100644 --- a/src/codelens.ts +++ b/src/codelens.ts @@ -2,9 +2,8 @@ import * as vscode from 'vscode'; import * as statusbar from './statusbar'; import { juvixRoot, globalJuvixRoot } from './root'; import { isJuvixFile } from './utils/base'; -import { debugChannel } from './utils/debug'; import * as path from 'path'; -import { debug } from 'console'; +import { debugChannel } from './utils/debug'; /** * CodelensProvider @@ -58,15 +57,12 @@ export class CodelensProvider implements vscode.CodeLensProvider { , _token: vscode.CancellationToken) : vscode.CodeLens[] | Thenable { - - if (vscode.workspace.getConfiguration("juvix-mode").get("codeLens", true)) { this.codeLenses = []; const text = document.getText(); const parsedFilepath = path.parse(document.fileName); let firstLineRange = document.lineAt(0).range; - /* Add a code lenses to show the Juvix version in the first line of the document. @@ -87,17 +83,22 @@ export class CodelensProvider implements vscode.CodeLensProvider { and the slashes are replaced by dots. */ - let regex = /module\s+([\w.]+);/; let match = text.match(regex); let projRoot = juvixRoot(); - + let globalProjRoot = globalJuvixRoot(); + let moduleName: string; if (isJuvixFile(document) && (text.length === 0 || match === null)) { - let relativeModulePath: string = path.relative(projRoot, parsedFilepath.dir).replace(path.sep, "."); - let moduleName: string = - (projRoot === globalJuvixRoot()) ? - parsedFilepath.name : - `${relativeModulePath}${relativeModulePath.length > 0 ? '.' : ''}${parsedFilepath.name}`; + if (projRoot == globalProjRoot) { + moduleName = parsedFilepath.name; + } else { + let relativeModulePath: string = path.relative(projRoot, parsedFilepath.dir).replace(path.sep, "."); + moduleName = + (projRoot === globalProjRoot) ? + parsedFilepath.name : + `${relativeModulePath}${relativeModulePath.length > 0 ? '.' : ''}${parsedFilepath.name}`; + } + debugChannel.info(`Expected module name: ${moduleName}`); let moduleTopHeader: string = `module ${moduleName};`; let insertModuleCodeLenses = new vscode.CodeLens( new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 0)), diff --git a/src/highlighting.ts b/src/highlighting.ts index fd50d51..b0980d1 100644 --- a/src/highlighting.ts +++ b/src/highlighting.ts @@ -124,7 +124,7 @@ export class Highlighter implements vscode.DocumentSemanticTokensProvider { const stdout = ls.stdout; const output: DevHighlightOutput = JSON.parse(stdout.toString()); - debugChannel.info('Highlighting output: ' + JSON.stringify(output, null, 2)); + // debugChannel.info('Highlighting output: ' + JSON.stringify(output, null, 2)); /* Populate the location map for the Goto feature @@ -166,7 +166,7 @@ export class Highlighter implements vscode.DocumentSemanticTokensProvider { The actual tokenization and syntax highlighting */ const allTokens = output.face; - debugChannel.debug('> Tokens length: ' + allTokens.length); + // debugChannel.debug('> Tokens length: ' + allTokens.length); const builder = new vscode.SemanticTokensBuilder(legend); allTokens.forEach(entry => { diff --git a/src/root.ts b/src/root.ts index be9dfb5..db859dd 100644 --- a/src/root.ts +++ b/src/root.ts @@ -41,5 +41,5 @@ export function globalJuvixRoot() : string { if (!juvixVersion) { throw new Error('Cannot find Juvix version'); } - return path.join(homeUserPath, '.config', 'juvix', juvixVersion, 'global-project'); + return path.join(homeUserPath, '.config', 'juvix', juvixVersion, 'global-project', path.sep); } From fd5d8b8aeeca9929da3cfd7f4ee019e9234c9e35 Mon Sep 17 00:00:00 2001 From: Jonathan Cubides Date: Fri, 26 May 2023 14:34:45 +0200 Subject: [PATCH 08/19] Add codelenses --- src/codelens.ts | 109 +++++++++++++++++++++++++++++++++++++++++++++++ src/extension.ts | 4 ++ src/judoc.ts | 29 +++---------- src/root.ts | 32 ++++++++++++++ src/statusbar.ts | 1 - 5 files changed, 152 insertions(+), 23 deletions(-) create mode 100644 src/codelens.ts create mode 100644 src/root.ts diff --git a/src/codelens.ts b/src/codelens.ts new file mode 100644 index 0000000..239e285 --- /dev/null +++ b/src/codelens.ts @@ -0,0 +1,109 @@ +import * as vscode from 'vscode'; +import * as statusbar from './statusbar'; +import { juvixRoot } from './root'; +import { isJuvixFile } from './utils/base'; +/** + * CodelensProvider + */ + +export function activate(context: vscode.ExtensionContext) { + statusbar.activate(context); + 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.codelensAction", (args: any) => { + vscode.window.showInformationMessage(`CodeLens action clicked with args=${args}`); + }) + ); +} + +export class CodelensProvider implements vscode.CodeLensProvider { + + private codeLenses: vscode.CodeLens[] = []; + private _onDidChangeCodeLenses: vscode.EventEmitter = new vscode.EventEmitter(); + public readonly onDidChangeCodeLenses: vscode.Event = this._onDidChangeCodeLenses.event; + + constructor() { + vscode.workspace.onDidChangeConfiguration((_) => { + this._onDidChangeCodeLenses.fire(); + }); + } + + public provideCodeLenses( + document: vscode.TextDocument + , _token: vscode.CancellationToken) + : vscode.CodeLens[] | Thenable { + + if (vscode.workspace + .getConfiguration("JuvixCodeLens") + .get("enableCodeLens", true)) { + + this.codeLenses = []; + const text = document.getText(); + + /* + Add a code lenses that checks if the document is empty. + If it is, it suggests to insert the module header + relative to juvixRoot. + The content inserted by the code lenses should be: + "module ;" + where document filepath relative to juvixRoot + and the slashes are replaced by dots. + */ + let firstLineRange = document.lineAt(0).range; + // check if the file doesnn't define a module. This check is not perfe + const regex = /module\s+([\w.]+);/; + const match = text.match(regex); + const noModule = text.length === 0 || match === null; + if (noModule && + !document.isUntitled && + isJuvixFile(document) && + juvixRoot !== undefined) { + let moduleName = document.fileName; + moduleName = moduleName + .replace(juvixRoot, "") + .replace(".juvix", "") + .replace(/\\/g, ".") + .replace(/\//g, "."); + // insert the snippet in the first line of the document + + let insertModuleCodeLenses = new vscode.CodeLens( + new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 0)) + , { + title: "Insert \"module " + moduleName + ";\"", + command: "" + }); + + this.codeLenses.push(insertModuleCodeLenses); + } + + /* + 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); + + return this.codeLenses; + } + return []; + } + +} diff --git a/src/extension.ts b/src/extension.ts index d061f75..ac4de66 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -17,10 +17,14 @@ import * as dev from './dev'; import * as check from './check'; import * as formatter from './formatter'; import * as vampir from './vampir/tasks'; +import * as codelens from './codelens'; +import * as root from './root'; export async function activate(context: vscode.ExtensionContext) { debugChannel.clear(); statusBar.activate(context); + root.activate(context); + codelens.activate(context); syntaxHighlighter.activate(context); goToDefinition.activate(context); hoverInfo.activate(context); diff --git a/src/judoc.ts b/src/judoc.ts index fc7dfa4..c8ef0ea 100644 --- a/src/judoc.ts +++ b/src/judoc.ts @@ -2,11 +2,11 @@ * Copyright (C) Microsoft Corporation. All rights reserved. *--------------------------------------------------------*/ import * as vscode from 'vscode'; -import * as path from 'path'; import * as fs from 'fs'; import { debugChannel } from './utils/debug'; import { JuvixConfig } from './config'; import { isJuvixFile } from './utils/base'; +import { juvixRoot } from './root'; export function activate(context: vscode.ExtensionContext) { context.subscriptions.push( @@ -249,21 +249,6 @@ export class JudocPanel { throw new Error(errMsg); } - const juvixRootCall = [ - config.getJuvixExec(), - config.getGlobalFlags(), - 'dev', - 'root', - doc.uri.fsPath, - ].join(' '); - - const rootRun = spawnSync(juvixRootCall, { shell: true, encoding: 'utf8' }); - if (rootRun.status !== 0) { - const errMsg: string = "Juvix's Error: " + rootRun.stderr.toString(); - debugChannel.error('Juvix root failed for Judoc gen:', errMsg); - throw new Error(errMsg); - } - const juvixRoot = rootRun.stdout.toString().trim(); const htmlFilename = doc.uri.fsPath .replace(juvixRoot, '') .replace('/', '.') @@ -284,12 +269,12 @@ export class JudocPanel { return contentDisk.replace( '', '' + webview.cspSource + + '; img-src ' + + webview.cspSource + + " https:; script-src 'nonce-" + + nonce + + '\';">' ); } } diff --git a/src/root.ts b/src/root.ts new file mode 100644 index 0000000..ab63a63 --- /dev/null +++ b/src/root.ts @@ -0,0 +1,32 @@ +/*--------------------------------------------------------- + * Copyright (C) Microsoft Corporation. All rights reserved. + *--------------------------------------------------------*/ +import * as vscode from 'vscode'; +import { debugChannel } from './utils/debug'; +import { JuvixConfig } from './config'; + +export let juvixRoot: string; + +export function activate(_context: vscode.ExtensionContext) { + const config = new JuvixConfig(); + const { spawnSync } = require('child_process'); + const doc = vscode.window.activeTextEditor?.document; + juvixRoot = ''; + if (doc) { + const juvixRootCall = [ + config.getJuvixExec(), + config.getGlobalFlags(), + 'dev', + 'root', + doc.uri.fsPath, + ].join(' '); + + const rootRun = spawnSync(juvixRootCall, { shell: true, encoding: 'utf8' }); + if (rootRun.status !== 0) { + const errMsg: string = "Juvix's Error: " + rootRun.stderr.toString(); + debugChannel.error('Juvix root failed for Judoc gen:', errMsg); + throw new Error(errMsg); + } + juvixRoot = rootRun.stdout.toString().trim(); + } +} \ No newline at end of file diff --git a/src/statusbar.ts b/src/statusbar.ts index 2061114..7fa671f 100644 --- a/src/statusbar.ts +++ b/src/statusbar.ts @@ -11,7 +11,6 @@ import * as utils from './utils/base'; export let juvixStatusBarItemVersion: vscode.StatusBarItem; export function activate(context: vscode.ExtensionContext) { - const config = new user.JuvixConfig(); juvixStatusBarItemVersion = vscode.window.createStatusBarItem( vscode.StatusBarAlignment.Right ); From 2f0ce585ef6397d0bccaf50b90fdc5903d6ce33f Mon Sep 17 00:00:00 2001 From: Jonathan Cubides Date: Fri, 26 May 2023 14:37:38 +0200 Subject: [PATCH 09/19] make pre-commit happy --- src/codelens.ts | 2 +- src/root.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/codelens.ts b/src/codelens.ts index 239e285..40c6779 100644 --- a/src/codelens.ts +++ b/src/codelens.ts @@ -92,7 +92,7 @@ export class CodelensProvider implements vscode.CodeLensProvider { } /* - Add a code lenses to show the Juvix version + Add a code lenses to show the Juvix version in the first line of the document. */ let juvixVersionCodeLenses = new vscode.CodeLens(firstLineRange, { diff --git a/src/root.ts b/src/root.ts index ab63a63..4ab055e 100644 --- a/src/root.ts +++ b/src/root.ts @@ -29,4 +29,4 @@ export function activate(_context: vscode.ExtensionContext) { } juvixRoot = rootRun.stdout.toString().trim(); } -} \ No newline at end of file +} From 758afef020681b39bdf94086c8f36e7e7ff986ff Mon Sep 17 00:00:00 2001 From: Jonathan Cubides Date: Fri, 26 May 2023 17:36:26 +0200 Subject: [PATCH 10/19] Fixed aux command --- src/codelens.ts | 26 +++++++++++++++++++++----- src/extension.ts | 2 +- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/codelens.ts b/src/codelens.ts index 40c6779..41545a0 100644 --- a/src/codelens.ts +++ b/src/codelens.ts @@ -7,7 +7,6 @@ import { isJuvixFile } from './utils/base'; */ export function activate(context: vscode.ExtensionContext) { - statusbar.activate(context); context.subscriptions.push( vscode.languages.registerCodeLensProvider( { scheme: 'file', language: 'Juvix' }, @@ -29,6 +28,18 @@ export function activate(context: vscode.ExtensionContext) { vscode.window.showInformationMessage(`CodeLens action clicked with args=${args}`); }) ); + + 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 { @@ -65,7 +76,7 @@ export class CodelensProvider implements vscode.CodeLensProvider { and the slashes are replaced by dots. */ let firstLineRange = document.lineAt(0).range; - // check if the file doesnn't define a module. This check is not perfe + const regex = /module\s+([\w.]+);/; const match = text.match(regex); const noModule = text.length === 0 || match === null; @@ -79,13 +90,18 @@ export class CodelensProvider implements vscode.CodeLensProvider { .replace(".juvix", "") .replace(/\\/g, ".") .replace(/\//g, "."); - // insert the snippet in the first line of the document + const moduleTopHeader = "module " + moduleName + ";"; let insertModuleCodeLenses = new vscode.CodeLens( new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 0)) , { - title: "Insert \"module " + moduleName + ";\"", - command: "" + title: "Insert \"" + moduleTopHeader + "\"", + command: "juvix-mode.aux.prependText", + arguments: [ + { + text: moduleTopHeader + "\n" + } + ] }); this.codeLenses.push(insertModuleCodeLenses); diff --git a/src/extension.ts b/src/extension.ts index ac4de66..6221a71 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -24,7 +24,6 @@ export async function activate(context: vscode.ExtensionContext) { debugChannel.clear(); statusBar.activate(context); root.activate(context); - codelens.activate(context); syntaxHighlighter.activate(context); goToDefinition.activate(context); hoverInfo.activate(context); @@ -36,6 +35,7 @@ export async function activate(context: vscode.ExtensionContext) { formatter.activate(context); vampir.activate(context); dev.activate(context); + codelens.activate(context); debugChannel.info('Juvix extension is ready!'); } From f8b5c45e8550bab14ec9dabdfcc6fb1252d202c7 Mon Sep 17 00:00:00 2001 From: Jonathan Cubides Date: Fri, 26 May 2023 17:36:47 +0200 Subject: [PATCH 11/19] Make pre-commit happy --- src/codelens.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/codelens.ts b/src/codelens.ts index 41545a0..a941414 100644 --- a/src/codelens.ts +++ b/src/codelens.ts @@ -76,7 +76,7 @@ export class CodelensProvider implements vscode.CodeLensProvider { and the slashes are replaced by dots. */ let firstLineRange = document.lineAt(0).range; - + const regex = /module\s+([\w.]+);/; const match = text.match(regex); const noModule = text.length === 0 || match === null; From 1e29603db6cf3ecc4df83f8e0871b89329e7262f Mon Sep 17 00:00:00 2001 From: Jonathan Cubides Date: Sat, 27 May 2023 13:20:25 +0200 Subject: [PATCH 12/19] forgot to push this one --- src/codelens.ts | 18 ++++++++---------- src/extension.ts | 2 -- src/judoc.ts | 4 ++-- src/root.ts | 8 +++----- 4 files changed, 13 insertions(+), 19 deletions(-) diff --git a/src/codelens.ts b/src/codelens.ts index a941414..2c4e663 100644 --- a/src/codelens.ts +++ b/src/codelens.ts @@ -2,6 +2,7 @@ import * as vscode from 'vscode'; import * as statusbar from './statusbar'; import { juvixRoot } from './root'; import { isJuvixFile } from './utils/base'; +import { debugChannel } from './utils/debug'; /** * CodelensProvider */ @@ -76,25 +77,24 @@ export class CodelensProvider implements vscode.CodeLensProvider { and the slashes are replaced by dots. */ let firstLineRange = document.lineAt(0).range; - const regex = /module\s+([\w.]+);/; const match = text.match(regex); - const noModule = text.length === 0 || match === null; - if (noModule && - !document.isUntitled && + const projRoot = juvixRoot(); + if (text.length === 0 && isJuvixFile(document) && - juvixRoot !== undefined) { + projRoot !== undefined + && match === null) { let moduleName = document.fileName; moduleName = moduleName - .replace(juvixRoot, "") + .replace(projRoot, "") .replace(".juvix", "") .replace(/\\/g, ".") .replace(/\//g, "."); const moduleTopHeader = "module " + moduleName + ";"; let insertModuleCodeLenses = new vscode.CodeLens( - new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 0)) - , { + new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 0)), + { title: "Insert \"" + moduleTopHeader + "\"", command: "juvix-mode.aux.prependText", arguments: [ @@ -103,7 +103,6 @@ export class CodelensProvider implements vscode.CodeLensProvider { } ] }); - this.codeLenses.push(insertModuleCodeLenses); } @@ -116,7 +115,6 @@ export class CodelensProvider implements vscode.CodeLensProvider { command: "" }); this.codeLenses.push(juvixVersionCodeLenses); - return this.codeLenses; } return []; diff --git a/src/extension.ts b/src/extension.ts index 6221a71..3878488 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -18,12 +18,10 @@ import * as check from './check'; import * as formatter from './formatter'; import * as vampir from './vampir/tasks'; import * as codelens from './codelens'; -import * as root from './root'; export async function activate(context: vscode.ExtensionContext) { debugChannel.clear(); statusBar.activate(context); - root.activate(context); syntaxHighlighter.activate(context); goToDefinition.activate(context); hoverInfo.activate(context); diff --git a/src/judoc.ts b/src/judoc.ts index c8ef0ea..e78c8dd 100644 --- a/src/judoc.ts +++ b/src/judoc.ts @@ -248,9 +248,9 @@ export class JudocPanel { debugChannel.error('Judoc failed', errMsg); throw new Error(errMsg); } - + const projRoot = juvixRoot(); const htmlFilename = doc.uri.fsPath - .replace(juvixRoot, '') + .replace(projRoot, '') .replace('/', '.') .replace('.juvix', '.html'); diff --git a/src/root.ts b/src/root.ts index 4ab055e..dff849b 100644 --- a/src/root.ts +++ b/src/root.ts @@ -5,13 +5,10 @@ import * as vscode from 'vscode'; import { debugChannel } from './utils/debug'; import { JuvixConfig } from './config'; -export let juvixRoot: string; - -export function activate(_context: vscode.ExtensionContext) { +export function juvixRoot() { const config = new JuvixConfig(); const { spawnSync } = require('child_process'); const doc = vscode.window.activeTextEditor?.document; - juvixRoot = ''; if (doc) { const juvixRootCall = [ config.getJuvixExec(), @@ -27,6 +24,7 @@ export function activate(_context: vscode.ExtensionContext) { debugChannel.error('Juvix root failed for Judoc gen:', errMsg); throw new Error(errMsg); } - juvixRoot = rootRun.stdout.toString().trim(); + return rootRun.stdout.toString().trim(); } + return undefined; } From e7db7d1a2f7a7fb836a22529af4f2d4c2d5a857b Mon Sep 17 00:00:00 2001 From: Jonathan Cubides Date: Tue, 30 May 2023 09:58:11 +0200 Subject: [PATCH 13/19] w.i.p --- src/judoc.ts | 31 ++++++------------------------- 1 file changed, 6 insertions(+), 25 deletions(-) diff --git a/src/judoc.ts b/src/judoc.ts index e78c8dd..279b2c7 100644 --- a/src/judoc.ts +++ b/src/judoc.ts @@ -65,20 +65,17 @@ export class JudocPanel { private _panel: vscode.WebviewPanel; private _disposables: vscode.Disposable[] = []; - // public readonly htmlFolder : string; public static createOrShow(onlySource = false) { this.onlySource = onlySource; const editor = vscode.window.activeTextEditor; if (!editor || !isJuvixFile(editor.document)) return; this.juvixDocument = editor.document; - // If we already have a panel, show it. + if (JudocPanel.currentPanel) { JudocPanel.currentPanel._panel.reveal(); return; } - - // Otherwise, create a new panel. const panel = vscode.window.createWebviewPanel( JudocPanel.viewType, 'Html preview', @@ -99,7 +96,6 @@ export class JudocPanel { private constructor(panel: vscode.WebviewPanel, _htmlFolder?: string) { this._panel = panel; - // this.htmlFolder = htmlFolder; this._disposables.push( vscode.workspace.onDidSaveTextDocument(document => { @@ -112,15 +108,6 @@ export class JudocPanel { }) ); - // this._disposables.push( - // vscode.workspace.onDidCloseTextDocument((document) => { - // if (document === JudocPanel.juvixDocument) { - // this._panel.dispose(); - // // this.dispose(); - // } - // }) - // ); - this._disposables.push( vscode.workspace.onDidOpenTextDocument(document => { if (!isJuvixFile(document)) return; @@ -203,17 +190,11 @@ export class JudocPanel { private _getHtmlForWebview(webview: vscode.Webview) { const doc = JudocPanel.juvixDocument; if (!doc || (doc && !isJuvixFile(doc))) return; - - const docFolder = vscode.workspace.getWorkspaceFolder(doc.uri); - if (!docFolder) return; + // The html folder is the same as the juvixRoot folder. const config = new JuvixConfig(); - - // debugChannel.info("htmlFolder> " + this.htmlFolder); - - // const judocDocFolderUri = vscode.Uri.file(this.htmlFolder); - // const judocDocFolderUri = vscode.Uri.file(config.getInternalBuildDir()); - const judocDocFolderUri = vscode.Uri.joinPath(docFolder.uri, 'html'); - // const buildDir = config.getInternalBuildDir(); + const projRoot = juvixRoot(); + const projRootUri = vscode.Uri.file(projRoot); + const judocDocFolderUri = vscode.Uri.joinPath(projRootUri, 'html'); const { spawnSync } = require('child_process'); @@ -248,7 +229,7 @@ export class JudocPanel { debugChannel.error('Judoc failed', errMsg); throw new Error(errMsg); } - const projRoot = juvixRoot(); + const htmlFilename = doc.uri.fsPath .replace(projRoot, '') .replace('/', '.') From 5122cb6fba49f85d3f4fc40da87e34e52b5ff196 Mon Sep 17 00:00:00 2001 From: Jonathan Cubides Date: Tue, 30 May 2023 18:21:16 +0200 Subject: [PATCH 14/19] Fix compilation --- src/judoc.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/judoc.ts b/src/judoc.ts index e44943d..f11f433 100644 --- a/src/judoc.ts +++ b/src/judoc.ts @@ -6,7 +6,7 @@ import * as fs from 'fs'; import { debugChannel } from './utils/debug'; import { JuvixConfig } from './config'; import { isJuvixFile } from './utils/base'; -import { juvixRoot } from './root'; +import { juvixRoot, globalJuvixRoot } from './root'; export function activate(context: vscode.ExtensionContext) { context.subscriptions.push( @@ -229,7 +229,6 @@ export class JudocPanel { debugChannel.error('Judoc failed', errMsg); throw new Error(errMsg); } - const projRoot = juvixRoot(); const htmlFilename = doc.uri.fsPath .replace(projRoot, '') .replace('/', '.') From bf8c2b3f349162d976faaa1d0fe42d035b1795d4 Mon Sep 17 00:00:00 2001 From: Jonathan Cubides Date: Tue, 30 May 2023 18:34:31 +0200 Subject: [PATCH 15/19] Factor out moduleName --- src/codelens.ts | 25 +++---------------------- src/module.ts | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 22 deletions(-) create mode 100644 src/module.ts diff --git a/src/codelens.ts b/src/codelens.ts index 9a0eb9d..e974fcb 100644 --- a/src/codelens.ts +++ b/src/codelens.ts @@ -1,9 +1,6 @@ import * as vscode from 'vscode'; import * as statusbar from './statusbar'; -import { juvixRoot, globalJuvixRoot } from './root'; -import { isJuvixFile } from './utils/base'; -import * as path from 'path'; -import { debugChannel } from './utils/debug'; +import { getModuleName } from './module'; /** * CodelensProvider @@ -60,8 +57,6 @@ export class CodelensProvider implements vscode.CodeLensProvider { if (vscode.workspace.getConfiguration("juvix-mode").get("codeLens", true)) { this.codeLenses = []; const text = document.getText(); - const parsedFilepath = path.parse(document.fileName); - let firstLineRange = document.lineAt(0).range; /* Add a code lenses to show the Juvix version @@ -85,20 +80,8 @@ export class CodelensProvider implements vscode.CodeLensProvider { let regex = /module\s+([\w.]+);/; let match = text.match(regex); - let projRoot = juvixRoot(); - let globalProjRoot = globalJuvixRoot(); - let moduleName: string; - if (isJuvixFile(document) && (text.length === 0 || match === null)) { - if (projRoot == globalProjRoot) { - moduleName = parsedFilepath.name; - } else { - let relativeModulePath: string = path.relative(projRoot, parsedFilepath.dir).replace(path.sep, "."); - moduleName = - (projRoot === globalProjRoot) ? - parsedFilepath.name : - `${relativeModulePath}${relativeModulePath.length > 0 ? '.' : ''}${parsedFilepath.name}`; - } - debugChannel.info(`Expected module name: ${moduleName}`); + 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)), @@ -116,10 +99,8 @@ export class CodelensProvider implements vscode.CodeLensProvider { this.codeLenses = [insertModuleCodeLenses].concat(this.codeLenses); } - return this.codeLenses; } return []; } - } diff --git a/src/module.ts b/src/module.ts new file mode 100644 index 0000000..898920d --- /dev/null +++ b/src/module.ts @@ -0,0 +1,32 @@ +/*--------------------------------------------------------- + * Copyright (C) Microsoft Corporation. All rights reserved. + *--------------------------------------------------------*/ +import * as vscode from 'vscode'; +import { juvixRoot, globalJuvixRoot } from './root'; +import * as path from 'path'; +import { isJuvixFile } from './utils/base'; + + +export function getModuleName(document: vscode.TextDocument): string | undefined { + + const projRoot = juvixRoot(); + const globalProjRoot = globalJuvixRoot(); + const parsedFilepath = path.parse(document.fileName); + if (!isJuvixFile(document)) { + return undefined; + } + let moduleName: string | undefined = undefined; + if (projRoot == globalProjRoot) { + moduleName = parsedFilepath.name; + } else { + let relativeModulePath: string = + path.relative(projRoot, parsedFilepath.dir).replace(path.sep, "."); + moduleName = + (projRoot === globalProjRoot) ? + parsedFilepath.name : + `${relativeModulePath}${relativeModulePath.length > 0 ? '.' : ''}${parsedFilepath.name}`; + } + return moduleName; + + +} From 5bd73fb4cc1d7fbd0d0a0d82ebcd9e8b08cdee92 Mon Sep 17 00:00:00 2001 From: Jonathan Cubides Date: Tue, 30 May 2023 18:46:04 +0200 Subject: [PATCH 16/19] w.i.p --- src/judoc.ts | 2 +- src/module.ts | 13 ++++++------- src/root.ts | 11 ++++++++++- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/judoc.ts b/src/judoc.ts index f11f433..a16fec9 100644 --- a/src/judoc.ts +++ b/src/judoc.ts @@ -6,7 +6,7 @@ import * as fs from 'fs'; import { debugChannel } from './utils/debug'; import { JuvixConfig } from './config'; import { isJuvixFile } from './utils/base'; -import { juvixRoot, globalJuvixRoot } from './root'; +import { juvixRoot } from './root'; export function activate(context: vscode.ExtensionContext) { context.subscriptions.push( diff --git a/src/module.ts b/src/module.ts index 898920d..c9ffbff 100644 --- a/src/module.ts +++ b/src/module.ts @@ -2,21 +2,22 @@ * Copyright (C) Microsoft Corporation. All rights reserved. *--------------------------------------------------------*/ import * as vscode from 'vscode'; -import { juvixRoot, globalJuvixRoot } from './root'; +import { juvixRoot, globalJuvixRoot, isUsingGlobalRoot } from './root'; import * as path from 'path'; import { isJuvixFile } from './utils/base'; export function getModuleName(document: vscode.TextDocument): string | undefined { - const projRoot = juvixRoot(); - const globalProjRoot = globalJuvixRoot(); - const parsedFilepath = path.parse(document.fileName); if (!isJuvixFile(document)) { return undefined; } + + const projRoot = juvixRoot(); + const globalProjRoot = globalJuvixRoot(); + const parsedFilepath = path.parse(document.fileName); let moduleName: string | undefined = undefined; - if (projRoot == globalProjRoot) { + if (isUsingGlobalRoot(document)) { moduleName = parsedFilepath.name; } else { let relativeModulePath: string = @@ -27,6 +28,4 @@ export function getModuleName(document: vscode.TextDocument): string | undefined `${relativeModulePath}${relativeModulePath.length > 0 ? '.' : ''}${parsedFilepath.name}`; } return moduleName; - - } diff --git a/src/root.ts b/src/root.ts index db859dd..889c349 100644 --- a/src/root.ts +++ b/src/root.ts @@ -6,6 +6,7 @@ import { debugChannel } from './utils/debug'; import { JuvixConfig } from './config'; import { getInstalledNumericVersion } from './juvixVersion'; import * as path from 'path'; +import { isJuvixFile } from './utils/base'; export function juvixRoot() { const config = new JuvixConfig(); @@ -31,7 +32,6 @@ export function juvixRoot() { return undefined; } - export function globalJuvixRoot() : string { const juvixVersion = getInstalledNumericVersion(); const homeUserPath = process.env.HOME; @@ -43,3 +43,12 @@ export function globalJuvixRoot() : string { } return path.join(homeUserPath, '.config', 'juvix', juvixVersion, 'global-project', path.sep); } + +export function isUsingGlobalRoot(doc: vscode.TextDocument) : boolean { + if (doc && isJuvixFile(doc)) { + const projRoot = juvixRoot(); + const globalProjRoot = globalJuvixRoot(); + return projRoot == globalProjRoot; + } + return false; +} \ No newline at end of file From 132d8457f1154eae5b8083e767c7385f992d9504 Mon Sep 17 00:00:00 2001 From: Jonathan Cubides Date: Tue, 30 May 2023 18:48:46 +0200 Subject: [PATCH 17/19] Factor out common comparison --- src/module.ts | 9 +++------ src/root.ts | 10 ++++++++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/module.ts b/src/module.ts index 898920d..f354f9b 100644 --- a/src/module.ts +++ b/src/module.ts @@ -2,7 +2,7 @@ * Copyright (C) Microsoft Corporation. All rights reserved. *--------------------------------------------------------*/ import * as vscode from 'vscode'; -import { juvixRoot, globalJuvixRoot } from './root'; +import { juvixRoot, globalJuvixRoot, isUsingGlobalRoot } from './root'; import * as path from 'path'; import { isJuvixFile } from './utils/base'; @@ -10,21 +10,18 @@ import { isJuvixFile } from './utils/base'; export function getModuleName(document: vscode.TextDocument): string | undefined { const projRoot = juvixRoot(); - const globalProjRoot = globalJuvixRoot(); const parsedFilepath = path.parse(document.fileName); if (!isJuvixFile(document)) { return undefined; } let moduleName: string | undefined = undefined; - if (projRoot == globalProjRoot) { + if (isUsingGlobalRoot(document)) { moduleName = parsedFilepath.name; } else { let relativeModulePath: string = path.relative(projRoot, parsedFilepath.dir).replace(path.sep, "."); moduleName = - (projRoot === globalProjRoot) ? - parsedFilepath.name : - `${relativeModulePath}${relativeModulePath.length > 0 ? '.' : ''}${parsedFilepath.name}`; + `${relativeModulePath}${relativeModulePath.length > 0 ? '.' : ''}${parsedFilepath.name}`; } return moduleName; diff --git a/src/root.ts b/src/root.ts index db859dd..f816b4d 100644 --- a/src/root.ts +++ b/src/root.ts @@ -6,6 +6,7 @@ import { debugChannel } from './utils/debug'; import { JuvixConfig } from './config'; import { getInstalledNumericVersion } from './juvixVersion'; import * as path from 'path'; +import { isJuvixFile } from './utils/base'; export function juvixRoot() { const config = new JuvixConfig(); @@ -43,3 +44,12 @@ export function globalJuvixRoot() : string { } return path.join(homeUserPath, '.config', 'juvix', juvixVersion, 'global-project', path.sep); } + +export function isUsingGlobalRoot(doc: vscode.TextDocument) : boolean { + if (doc && isJuvixFile(doc)) { + const projRoot = juvixRoot(); + const globalProjRoot = globalJuvixRoot(); + return projRoot == globalProjRoot; + } + return false; +} From 924d61d8f654fd6cfb17c0a2d8dc662af7d73191 Mon Sep 17 00:00:00 2001 From: Jonathan Cubides Date: Tue, 30 May 2023 19:29:04 +0200 Subject: [PATCH 18/19] make pre-commit happy --- src/config.ts | 2 -- src/judoc.ts | 23 +++++++++++------------ src/module.ts | 4 ++-- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/config.ts b/src/config.ts index 03f7ac8..5818059 100644 --- a/src/config.ts +++ b/src/config.ts @@ -83,10 +83,8 @@ export class JuvixConfig { const juvixBuildDir = buildDir.toString(); try { if (fs.existsSync(juvixBuildDir)) { - debugChannel.info(`Directory exists: ${juvixBuildDir}`); return juvixBuildDir; } else { - debugChannel.info(`Directory does not exist: ${juvixBuildDir}`); const tmpJuvixDir = useTmpDir(); return tmpJuvixDir } diff --git a/src/judoc.ts b/src/judoc.ts index a16fec9..e257ce7 100644 --- a/src/judoc.ts +++ b/src/judoc.ts @@ -7,6 +7,8 @@ import { debugChannel } from './utils/debug'; import { JuvixConfig } from './config'; import { isJuvixFile } from './utils/base'; import { juvixRoot } from './root'; +import * as path from 'path'; +import { getModuleName } from './module'; export function activate(context: vscode.ExtensionContext) { context.subscriptions.push( @@ -190,17 +192,17 @@ export class JudocPanel { private _getHtmlForWebview(webview: vscode.Webview) { const doc = JudocPanel.juvixDocument; if (!doc || (doc && !isJuvixFile(doc))) return; - // The html folder is the same as the juvixRoot folder. - const config = new JuvixConfig(); - const projRoot = juvixRoot(); - const projRootUri = vscode.Uri.file(projRoot); - const judocDocFolderUri = vscode.Uri.joinPath(projRootUri, 'html'); + + const fileName = doc.fileName; + const parsedFilepath = path.parse(fileName); + const folderDocument = path.join(parsedFilepath.dir, 'docs', path.sep); + const judocDocFolderUri = vscode.Uri.file(folderDocument); const { spawnSync } = require('child_process'); const vscodePrefix = - webview.asWebviewUri(judocDocFolderUri).toString() + '/'; - + webview.asWebviewUri(judocDocFolderUri).toString(); + const config = new JuvixConfig(); const judocCall = [ config.getJuvixExec(), '--internal-build-dir', @@ -208,7 +210,6 @@ export class JudocPanel { 'html', JudocPanel.onlySource ? '--only-source' : '', '--output-dir', - // this.htmlFolder, judocDocFolderUri.fsPath, '--non-recursive', '--prefix-assets', @@ -217,6 +218,7 @@ export class JudocPanel { vscodePrefix, doc.uri.fsPath, ].join(' '); + debugChannel.info('Judoc call', judocCall); const ls = spawnSync(judocCall, { @@ -229,10 +231,7 @@ export class JudocPanel { debugChannel.error('Judoc failed', errMsg); throw new Error(errMsg); } - const htmlFilename = doc.uri.fsPath - .replace(projRoot, '') - .replace('/', '.') - .replace('.juvix', '.html'); + const htmlFilename = getModuleName(doc) + '.html'; const htmlByJudocForDoc = vscode.Uri.joinPath( judocDocFolderUri, diff --git a/src/module.ts b/src/module.ts index 07f26e1..9a21060 100644 --- a/src/module.ts +++ b/src/module.ts @@ -2,7 +2,7 @@ * Copyright (C) Microsoft Corporation. All rights reserved. *--------------------------------------------------------*/ import * as vscode from 'vscode'; -import { juvixRoot, globalJuvixRoot, isUsingGlobalRoot } from './root'; +import { juvixRoot, isUsingGlobalRoot } from './root'; import * as path from 'path'; import { isJuvixFile } from './utils/base'; @@ -14,7 +14,7 @@ export function getModuleName(document: vscode.TextDocument): string | undefined if (!isJuvixFile(document)) { return undefined; } - + let moduleName: string | undefined = undefined; if (isUsingGlobalRoot(document)) { moduleName = parsedFilepath.name; From e6df71c6ecee700e6f1b4e62784333865821c66b Mon Sep 17 00:00:00 2001 From: Jonathan Cubides Date: Tue, 30 May 2023 19:41:16 +0200 Subject: [PATCH 19/19] Make pre-commit happy --- src/extension.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/extension.ts b/src/extension.ts index 3878488..11c09d9 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -22,6 +22,7 @@ import * as codelens from './codelens'; export async function activate(context: vscode.ExtensionContext) { debugChannel.clear(); statusBar.activate(context); + codelens.activate(context); syntaxHighlighter.activate(context); goToDefinition.activate(context); hoverInfo.activate(context);