Skip to content

Commit

Permalink
Moves diagnostics set up to diagnostics module
Browse files Browse the repository at this point in the history
  • Loading branch information
LuqueDaniel committed Feb 23, 2023
1 parent 3746787 commit 056cd7f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
25 changes: 22 additions & 3 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,12 +52,31 @@ 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");
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;
}
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

0 comments on commit 056cd7f

Please sign in to comment.