Skip to content

Commit

Permalink
Refactor LintManager (mshr-h#383)
Browse files Browse the repository at this point in the history
* cleanup

* cleanup LintManager
  • Loading branch information
mshr-h authored Jan 6, 2023
1 parent 76e891e commit 4cb9ee5
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 92 deletions.
8 changes: 2 additions & 6 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';
import * as vscode from 'vscode';
import { SemVer } from 'semver';
import { LanguageClient, LanguageClientOptions, ServerOptions } from 'vscode-languageclient/node';

import LintManager from './linter/LintManager';
Expand Down Expand Up @@ -39,9 +37,6 @@ export function activate(context: vscode.ExtensionContext) {
ctagsManager = new CtagsManager(logger);
ctagsManager.configure();

// Configure lint manager
lintManager = new LintManager(logger);

// Configure Document Symbol Provider
let verilogDocumentSymbolProvider = new DocumentSymbolProvider.VerilogDocumentSymbolProvider(
logger
Expand Down Expand Up @@ -164,6 +159,7 @@ export function activate(context: vscode.ExtensionContext) {
);

// Register command for manual linting
lintManager = new LintManager(logger);
vscode.commands.registerCommand('verilog.lint', lintManager.runLintTool, lintManager);

// Configure language server
Expand Down Expand Up @@ -240,7 +236,7 @@ function stopAllLanguageClients(): Promise<any> {
return Promise.all(p);
}

export function deactivate(): Thenable<void> {
export function deactivate(): Promise<void> {
logger.info('Deactivated');
return stopAllLanguageClients();
}
166 changes: 80 additions & 86 deletions src/linter/LintManager.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as vscode from 'vscode';
import BaseLinter from './BaseLinter';
import IcarusLinter from './IcarusLinter';
import ModelsimLinter from './ModelsimLinter';
import VerilatorLinter from './VerilatorLinter';
import XvlogLinter from './XvlogLinter';
import ModelsimLinter from './ModelsimLinter';

export default class LintManager {
private subscriptions: vscode.Disposable[];
Expand All @@ -13,12 +13,12 @@ export default class LintManager {
private logger: vscode.LogOutputChannel;

constructor(logger: vscode.LogOutputChannel) {
this.linter = null;
this.diagnosticCollection = vscode.languages.createDiagnosticCollection();
this.logger = logger;
vscode.workspace.onDidOpenTextDocument(this.lint, this, this.subscriptions);
vscode.workspace.onDidSaveTextDocument(this.lint, this, this.subscriptions);
vscode.workspace.onDidCloseTextDocument(this.removeFileDiagnostics, this, this.subscriptions);

vscode.workspace.onDidChangeConfiguration(this.configLinter, this, this.subscriptions);
this.configLinter();

Expand All @@ -28,48 +28,58 @@ export default class LintManager {
});
}

getLinterFromString(name: string): BaseLinter {
switch (name) {
case 'iverilog':
return new IcarusLinter(this.diagnosticCollection, this.logger);
case 'xvlog':
return new XvlogLinter(this.diagnosticCollection, this.logger);
case 'modelsim':
return new ModelsimLinter(this.diagnosticCollection, this.logger);
case 'verilator':
return new VerilatorLinter(this.diagnosticCollection, this.logger);
default:
return null;
}
}

configLinter() {
let linterName;
linterName = vscode.workspace.getConfiguration('verilog.linting').get<string>('linter');
let linterName = vscode.workspace.getConfiguration('verilog.linting').get<string>('linter');

if (this.linter == null || this.linter.name != linterName) {
switch (linterName) {
case 'iverilog':
this.linter = new IcarusLinter(this.diagnosticCollection, this.logger);
break;
case 'xvlog':
this.linter = new XvlogLinter(this.diagnosticCollection, this.logger);
break;
case 'modelsim':
this.linter = new ModelsimLinter(this.diagnosticCollection, this.logger);
break;
case 'verilator':
this.linter = new VerilatorLinter(this.diagnosticCollection, this.logger);
break;
default:
this.logger.warn('[Lint Manager] Invalid linter name.');
this.linter = null;
break;
if (this.linter !== null) {
if (this.linter.name === linterName) {
return;
}
}

if (this.linter != null) {
this.logger.info('[lint-manager] Using linter ' + this.linter.name);
this.linter = this.getLinterFromString(linterName);
if (this.linter === null) {
this.logger.warn('[Lint Manager] Invalid linter name.');
return;
}

this.logger.info('[lint-manager] Using linter ' + this.linter.name);
}

lint(doc: vscode.TextDocument) {
// Check for language id
let lang: string = doc.languageId;
if (this.linter != null && (lang === 'verilog' || lang === 'systemverilog')) {
this.linter.startLint(doc);
if (this.linter === null) {
return;
}
switch (doc.languageId) {
case 'verilog':
case 'systemverilog':
this.linter.startLint(doc);
break;
default:
break;
}
}

removeFileDiagnostics(doc: vscode.TextDocument) {
if (this.linter != null) {
this.linter.removeFileDiagnostics(doc);
if (this.linter === null) {
return;
}
this.linter.removeFileDiagnostics(doc);
}

async runLintTool() {
Expand All @@ -80,66 +90,50 @@ export default class LintManager {
(lang !== 'verilog' && lang !== 'systemverilog')
) {
vscode.window.showErrorMessage('Verilog-HDL/SystemVerilog: No document opened');
return;
}
// else if(window.activeTextEditor.document.languageId !== "verilog")
// window.showErrorMessage("Verilog-HDL/SystemVerilog: No Verilog document opened");
else {
// Show the available linters
let linterStr: vscode.QuickPickItem = await vscode.window.showQuickPick(
[
{
label: 'iverilog',
description: 'Icarus Verilog',
},
{
label: 'xvlog',
description: 'Vivado Logical Simulator',
},
{
label: 'modelsim',
description: 'Modelsim',
},
{
label: 'verilator',
description: 'Verilator',
},
],

let linterStr: vscode.QuickPickItem = await vscode.window.showQuickPick(
[
{
matchOnDescription: true,
placeHolder: 'Choose a linter to run',
}
);
if (linterStr === undefined) {
return;
}
// Create and run the linter with progress bar
let tempLinter: BaseLinter;
switch (linterStr.label) {
case 'iverilog':
tempLinter = new IcarusLinter(this.diagnosticCollection, this.logger);
break;
case 'xvlog':
tempLinter = new XvlogLinter(this.diagnosticCollection, this.logger);
break;
case 'modelsim':
tempLinter = new ModelsimLinter(this.diagnosticCollection, this.logger);
break;
case 'verilator':
tempLinter = new VerilatorLinter(this.diagnosticCollection, this.logger);
break;
default:
return;
}
await vscode.window.withProgress(
label: 'iverilog',
description: 'Icarus Verilog',
},
{
location: vscode.ProgressLocation.Notification,
title: 'Verilog-HDL/SystemVerilog: Running lint tool...',
label: 'xvlog',
description: 'Vivado Logical Simulator',
},
async (_progress, _token) => {
tempLinter.removeFileDiagnostics(vscode.window.activeTextEditor.document);
tempLinter.startLint(vscode.window.activeTextEditor.document);
}
);
{
label: 'modelsim',
description: 'Modelsim',
},
{
label: 'verilator',
description: 'Verilator',
},
],
{
matchOnDescription: true,
placeHolder: 'Choose a linter to run',
}
);
if (linterStr === undefined) {
return;
}
// Create and run the linter with progress bar
await vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
title: 'Verilog-HDL/SystemVerilog: Running lint tool...',
},
async (_progress, _token) => {
let l: BaseLinter = this.getLinterFromString(linterStr.label);
if (l === null) {
return;
}
l.removeFileDiagnostics(vscode.window.activeTextEditor.document);
l.startLint(vscode.window.activeTextEditor.document);
}
);
}
}

0 comments on commit 4cb9ee5

Please sign in to comment.