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

Improve diagnostic configuration #247

Merged
merged 6 commits into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,32 +115,32 @@
"default": true,
"description": "Show Automatic Images in the displayable auto-completion list. If not checked (false), only images defined in the script will be shown. If checked (true), both script-defined images and images detected in the images folders will be shown."
},
"renpy.warnOnObsoleteMethods": {
"renpy.diagnostics.warnOnObsoleteMethods": {
"type": "boolean",
"default": true,
"description": "Enable obsolete method warnings. If checked (true), obsolete methods (e.g., im.Crop) will be marked with a warning in the editor."
},
"renpy.warnOnUndefinedPersistents": {
"renpy.diagnostics.warnOnUndefinedPersistents": {
"type": "boolean",
"default": true,
"description": "Enable undefined persistent warnings. If checked (true), persistent variables will be marked with a warning in the editor if they haven't been defaulted/defined."
},
"renpy.warnOnUndefinedStoreVariables": {
"renpy.diagnostics.warnOnUndefinedStoreVariables": {
"type": "boolean",
"default": true,
"description": "Enable undefined store variable warnings. If checked (true), store variables will be marked with a warning in the editor if they haven't been defaulted/defined."
},
"renpy.warnOnReservedVariableNames": {
"renpy.diagnostics.warnOnReservedVariableNames": {
"type": "boolean",
"default": true,
"description": "Enable reserved variable warnings. If checked (true), variables will be marked with an error in the editor if they are in the list of names reserved by Python."
},
"renpy.warnOnInvalidVariableNames": {
"renpy.diagnostics.warnOnInvalidVariableNames": {
"type": "boolean",
"default": true,
"description": "Enable invalid variable errors. Variables must begin with a letter or number. They may contain a '_' but may not begin with '_'. If set to true, variables will be flagged in the editor if they do not meet Ren'Py's specifications."
},
"renpy.warnOnIndentationAndSpacingIssues": {
"renpy.diagnostics.warnOnIndentationAndSpacingIssues": {
"type": "string",
"default": "Error",
"enum": [
Expand All @@ -155,7 +155,7 @@
],
"description": "Enable indentation and inconsistent spacing checks. If set to Error or Warning, tab characters and inconsistent indentation spacing will be marked in the editor. If set to Disabled, indentation issues will be ignored."
},
"renpy.warnOnInvalidFilenameIssues": {
"renpy.diagnostics.warnOnInvalidFilenameIssues": {
"type": "string",
"default": "Error",
"enum": [
Expand Down
27 changes: 23 additions & 4 deletions src/diagnostics.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Diagnostics (warnings and errors)
"use strict";

import { Diagnostic, DiagnosticCollection, DiagnosticSeverity, ExtensionContext, Range, TextDocument, window, workspace } from "vscode";
import { commands, Diagnostic, DiagnosticCollection, DiagnosticSeverity, ExtensionContext, languages, Range, TextDocument, window, workspace } from "vscode";
import { NavigationData } from "./navigation-data";
import { extractFilename } from "./workspace";

Expand Down Expand Up @@ -52,18 +52,37 @@ const rxStoreCheck = /\s+store\.(\w+)[^a-zA-Z_]?/g;
const rxTabCheck = /^(\t+)/g;
const rsComparisonCheck = /\s+(if|while)\s+(\w+)\s*(=)\s*(\w+)\s*/g;

/**
* Set up diagnostics
* @param context extension context
*/
export function diagnosticsInit(context: ExtensionContext) {
const diagnostics = languages.createDiagnosticCollection("renpy");
duckdoom4 marked this conversation as resolved.
Show resolved Hide resolved
context.subscriptions.push(diagnostics);

// custom command - refresh diagnostics
const refreshDiagnosticsCommand = commands.registerCommand("renpy.refreshDiagnostics", () => {
if (window.activeTextEditor) {
refreshDiagnostics(window.activeTextEditor.document, diagnostics);
}
});
context.subscriptions.push(refreshDiagnosticsCommand);

subscribeToDocumentChanges(context, diagnostics);
}

/**
* Analyzes the text document for problems.
* @param doc text document to analyze
* @param diagnostics diagnostic collection
*/
export function refreshDiagnostics(doc: TextDocument, diagnosticCollection: DiagnosticCollection): void {
function refreshDiagnostics(doc: TextDocument, diagnosticCollection: DiagnosticCollection): void {
if (doc.languageId !== "renpy") {
return;
}

const diagnostics: Diagnostic[] = [];
const config = workspace.getConfiguration("renpy");
const config = workspace.getConfiguration("renpy.diagnostics");

//Filenames must begin with a letter or number,
//and may not begin with "00", as Ren'Py uses such files for its own purposes.
Expand Down Expand Up @@ -167,7 +186,7 @@ export function refreshDiagnostics(doc: TextDocument, diagnosticCollection: Diag
diagnosticCollection.set(doc.uri, diagnostics);
}

export function subscribeToDocumentChanges(context: ExtensionContext, diagnostics: DiagnosticCollection): void {
function subscribeToDocumentChanges(context: ExtensionContext, diagnostics: DiagnosticCollection): void {
if (window.activeTextEditor) {
refreshDiagnostics(window.activeTextEditor.document, diagnostics);
}
Expand Down
14 changes: 2 additions & 12 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import {
import { RenpyColorProvider } from "./color";
import { getStatusBarText, NavigationData } from "./navigation-data";
import { cleanUpPath, getAudioFolder, getImagesFolder, getNavigationJsonFilepath, getWorkspaceFolder, stripWorkspaceFromFile } from "./workspace";
import { refreshDiagnostics, subscribeToDocumentChanges } from "./diagnostics";
import { diagnosticsInit } from "./diagnostics";
import { getSemanticTokens } from "./semantics";
import { getHover } from "./hover";
import { getCompletionList } from "./completion";
Expand Down Expand Up @@ -212,9 +212,7 @@ export async function activate(context: ExtensionContext): Promise<void> {
);

// diagnostics (errors and warnings)
const diagnostics = languages.createDiagnosticCollection("renpy");
context.subscriptions.push(diagnostics);
subscribeToDocumentChanges(context, diagnostics);
diagnosticsInit(context);

// custom command - refresh data
const refreshCommand = commands.registerCommand("renpy.refreshNavigationData", async () => {
Expand All @@ -241,14 +239,6 @@ export async function activate(context: ExtensionContext): Promise<void> {
});
context.subscriptions.push(gotoFileLocationCommand);

// custom command - refresh diagnostics
const refreshDiagnosticsCommand = commands.registerCommand("renpy.refreshDiagnostics", () => {
if (window.activeTextEditor) {
refreshDiagnostics(window.activeTextEditor.document, diagnostics);
}
});
context.subscriptions.push(refreshDiagnosticsCommand);

// custom command - toggle token debug view
let isShowingTokenDebugView = false;
const toggleTokenDebugViewCommand = commands.registerCommand("renpy.toggleTokenDebugView", () => {
Expand Down