Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve debugging #179

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"--disable-extensions"
],
"env": {
"DEBUG": "vscode-groovy-lint"
"DEBUG": "vscode-groovy-lint,npm-groovy-lint"
},
"outFiles": ["${workspaceRoot}/client/out/**/*.js"],
"preLaunchTask": {
Expand Down Expand Up @@ -48,7 +48,7 @@
"port": 6009,
"restart": true,
"env": {
"DEBUG": "vscode-groovy-lint"
"DEBUG": "vscode-groovy-lint,npm-groovy-lint"
},
"outFiles": ["${workspaceRoot}/server/out/**/*.js"],
"smartStep": true,
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [Unreleased]

- Write your updates here !
- Debug sessions correctly enable debugging by default including npm-groovy-lint.

## [2.0.0] 2022-08-13

Expand Down
16 changes: 12 additions & 4 deletions server/src/DocumentsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class DocumentsManager {

// Commands execution
async executeCommand(params: any) {
debug(`Request execute command ${JSON.stringify(params)}`);
debug(`Request execute command ${JSON.stringify(params, null, 2)}`);
// Set current document URI if sent as parameter
if (params.arguments && params.arguments[0] && URI.isUri(params.arguments[0])) {
this.setCurrentDocumentUri(params.arguments[0].toString());
Expand Down Expand Up @@ -196,7 +196,7 @@ export class DocumentsManager {

// Lint again all open documents (after change of config)
async lintAgainAllOpenDocuments() {
await this.refreshDebugMode();
await this.refreshDebugMode(false);
// Reset all cached document settings
this.removeDocumentSettings('all');
// Revalidate all open text documents
Expand Down Expand Up @@ -466,18 +466,26 @@ export class DocumentsManager {
}

// Enable/Disable debug mode depending on VsCode GroovyLint setting groovyLint.debug.enable
async refreshDebugMode() {
async refreshDebugMode(onInitialized: boolean) {
if (onInitialized && process.env.DEBUG) {
// Use DEBUG env var if configured on initialization, so Run and Debug -> Play works as expected.
return;
}

const settings = await this.connection.workspace.getConfiguration({
section: 'groovyLint'
});

// Enable debug logs if setting is set
const debugLib = require("debug");
if (settings.debug && settings.debug.enable === true) {
debugLib.enable('vscode-groovy-lint');
debugLib.enable('npm-groovy-lint');
}
// Disable if not set
else {
debugLib.disable('vscode-groovy-lint');
debugLib.disable('npm-groovy-lint');
}
}
}
}
8 changes: 7 additions & 1 deletion server/src/codeActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ export function provideQuickFixCodeActions(textDocument: TextDocument, codeActio
if (isNullOrUndefined(diagnostics) || diagnostics.length === 0) {
return quickFixCodeActions;
}

debug(`docQuickFixes: ${JSON.stringify(docQuickFixes, null, 2)}`);
// Browse diagnostics to get related CodeActions
for (const diagnostic of codeActionParams.context.diagnostics) {
// Skip Diagnostics not from VsCodeGroovyLint
debug(`Diagnostic is ${JSON.stringify(diagnostic, null, 2)}`);
if (diagnostic.source !== 'GroovyLint') {
continue;
}
Expand All @@ -52,6 +55,7 @@ export function provideQuickFixCodeActions(textDocument: TextDocument, codeActio
if (docQuickFixes && docQuickFixes[diagCode]) {
for (const quickFix of docQuickFixes[diagCode]) {
const codeActions = createQuickFixCodeActions(diagnostic, quickFix, textDocument.uri);
debug(`codeActions is ${JSON.stringify(codeActions, null, 2)}`);
quickFixCodeActions.push(...codeActions);
}
}
Expand Down Expand Up @@ -225,7 +229,9 @@ export async function applyQuickFixes(diagnostics: Diagnostic[], textDocumentUri
lastFileName: textDocument.uri
});
// Apply updates to textDocument
if (docLinter.status === 0) {
if (docLinter.status !== 0) {
debug(`Error while fixing ${textDocument.uri} status: ${docLinter.status} error: ${docLinter.error ? docLinter.error.msg : 'unknown'}}`);
} else if (docLinter.lintResult.summary.totalFixedNumber > 0) {
await applyTextDocumentEditOnWorkspace(docManager, textDocument, getUpdatedSource(docLinter, textDocument.getText()));
setTimeout(() => { // Wait 500ms so we are more sure that the textDocument is already updated
const newDoc = docManager.getUpToDateTextDocument(textDocument);
Expand Down
8 changes: 7 additions & 1 deletion server/src/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,15 @@ export async function executeLinter(textDocument: TextDocument, docManager: Docu
console.info(`Start ${verb} ${textDocument.uri}`);
linter = new NpmGroovyLint(npmGroovyLintConfig, npmGroovyLintExecParam);
try {
debug(`Run npm-groovy-lint ${verb} for ${textDocument.uri}`);
debug(`Config: ${JSON.stringify(npmGroovyLintConfig, null, 2)} ExecParam: ${JSON.stringify(npmGroovyLintExecParam, null, 2)}`);
await linter.run();
debug(`Done npm-groovy-lint ${verb} for ${textDocument.uri}`);
if (!format) {
docManager.setDocLinter(textDocument.uri, linter);
}

debug(`Lint result: ${JSON.stringify(linter.lintResult)}`);
// Managed cancelled lint case
if (linter.status === 9) {
docManager.connection.sendNotification(StatusNotification.type, {
Expand Down Expand Up @@ -264,7 +269,8 @@ export async function executeLinter(textDocument: TextDocument, docManager: Docu
});
return Promise.resolve([]);
}
console.info(`Completed ${verb} ${textDocument.uri} in ${(performance.now() - perfStart).toFixed(0)} ms`);

console.info(`Completed ${verb} ${textDocument.uri} in ${(performance.now() - perfStart).toFixed(0)} ms found ${linter?.lintResult?.summary?.totalFoundNumber || 0} results`);
}

// Parse results
Expand Down
2 changes: 2 additions & 0 deletions server/src/linterParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ export function parseLinterResults(lintResultsIn: any, source: string, textDocum
label: err.fixLabel || `Fix ${err.rule}`,
errId: err.id
});
debug(`Quick Fix: ${err.fixLabel || `Fix ${err.rule}`} ${err.id}`);
}
debug(`Diagnostic: ${diagnostic}`);
diagnostics.push(diagnostic);
pos++;
}
Expand Down
11 changes: 8 additions & 3 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ connection.onInitialized(async () => {
connection.client.register(DidSaveTextDocumentNotification.type);
//connection.client.register(ActiveDocumentNotification.type);
debug('GroovyLint: initialized server');
await docManager.refreshDebugMode();
await docManager.refreshDebugMode(true);
});

// Kill CodeNarcServer when closing VsCode or deactivate extension
Expand Down Expand Up @@ -109,7 +109,8 @@ connection.onCodeAction(async (codeActionParams: CodeActionParams): Promise<Code
if (!codeActionParams.context.diagnostics.length) {
return [];
}
debug(`Code action request received from client for ${codeActionParams.textDocument.uri} with params: ${JSON.stringify(codeActionParams)}`);
debug(`Code action request received from client for ${codeActionParams.textDocument.uri}`);
debug(`codeActionParams: ${JSON.stringify(codeActionParams, null, 2)}`);
const document = docManager.getDocumentFromUri(codeActionParams.textDocument.uri);
if (document == null) {
return [];
Expand Down Expand Up @@ -157,14 +158,18 @@ docManager.documents.onDidChangeContent(async (change: TextDocumentChangeEvent<T

// Lint on save if it has been configured
docManager.documents.onDidSave(async event => {
debug(`Save event received for ${event.document.uri}`);
debug(`Save event received for: ${event.document.uri}`);
const textDocument: TextDocument = docManager.getDocumentFromUri(event.document.uri, true);
const settings = await docManager.getDocumentSettings(textDocument.uri);
if (settings.fix.trigger === 'onSave') {
debug(`Save trigger fix for: ${textDocument.uri}`);
await docManager.validateTextDocument(textDocument, { fix: true });
}
else if (settings.lint.trigger === 'onSave') {
debug(`Save trigger lint for: ${textDocument.uri}`);
await docManager.validateTextDocument(textDocument);
} else {
debug(`Save no action for: ${textDocument.uri}`);
}
});

Expand Down
Loading