Skip to content

Commit

Permalink
workspace syntax checking
Browse files Browse the repository at this point in the history
  • Loading branch information
F4r3n committed Apr 4, 2024
1 parent f1e1685 commit a971f92
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 6 deletions.
14 changes: 14 additions & 0 deletions editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Release numbers indicate the minimal 4D release where the feature is available.
- [Indentation](https://blog.4d.com/new-vs-code-editor-features-with-4d-v20/) (4D 20)
- [Code formatting](https://blog.4d.com/format-your-4d-code-in-visual-studio-code/) (4D 20 R2)
- [Show Documentation](https://blog.4d.com/vs-code-extension-show-4d-documentation/) (4D 20 R3)
- [Workspace syntax checking](https://blog.4d.com/workspace-syntax-checking-in-vs-code-editor/) (4D 20 R5)


## Quick start

Expand All @@ -28,6 +30,7 @@ All 4D blog posts about this extension are available [here](https://blog.4d.com/
This extension provides configurations through VSCode's configuration settings.
All configurations are under __4D-Analyzer.*__.
See the VSCode manual for more information on specific configurations.

More precision on automatic **tool4d** download and use in [this 4D blog post](https://blog.4d.com/auto-tool4d-download-in-4d-analyzer-extension-for-vs-code).

### Extension Settings
Expand Down Expand Up @@ -70,6 +73,17 @@ More precision on automatic **tool4d** download and use in [this 4D blog post](h
- Values: true (default) / false
- Enables/disables the automatic Syntax Checking.

- **Diagnostics: Scope**
- Setting: 4D-Analyzer.diagnostics.scope
- Values: "Workspace" (default) / "Document"
- Specifies if the Syntax Checking is performed on the current workspace or on the current document only.

- **Server: Path**
- Setting: 4D-Analyzer.server.path
- Values: path
- Path to the **4D**, **4D Server** or **tool4d** application to use as LSP server.


- **Trace: Server**
- Setting: 4D-Analyzer.trace.server
- Values: "off" (default) / "messages" / "verbose"
Expand Down
12 changes: 12 additions & 0 deletions editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@
"default": "",
"markdownDescription": "tool4d download folder (only has an impact when the \"Automatic download and use of tool4d\" parameter is activated).",
"order": 3
},
"4D-Analyzer.diagnostics.scope": {
"type": "string",
"scope": "machine",
"enum": ["Document", "Workspace"],
"default": "Workspace",
"order": 6,
"markdownDescription": "Precise the scope of the diagnostics. If 'workspace' is selected, every methods will be checked. If 'Document' is selected, only the current document will be checked"
}
}
},
Expand All @@ -136,6 +144,10 @@
{
"command": "4d-analyzer.cleanUnusedToolVersions",
"title": "4D Analyzer: Clean all the unused tool4d versions previously downloaded"
},
{
"command": "4d-analyzer.checkWorkspaceSyntax",
"title": "4D Analyzer: Check workspace syntax"
}
]
},
Expand Down
55 changes: 54 additions & 1 deletion editor/src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import * as ext from "./lsp_ext";
import { Ctx } from "./ctx";
import * as vscode from "vscode";
import { WorkspaceFullDocumentDiagnosticReport } from "vscode-languageclient";
import { LabeledVersion } from "./labeledVersion";
import { Logger } from "./logger";

export type Cmd = (...args: any[]) => unknown;

export function filesStatus(ctx: Ctx): Cmd {
Expand Down Expand Up @@ -85,8 +89,57 @@ export function display4DVersion(ctx: Ctx): Cmd {
export function cleanUnusedToolVersions(ctx: Ctx): Cmd {

return async () => {

return await ctx.cleanUnusedToolVersions();
};
}

export function checkWorkspaceSyntax(ctx: Ctx): Cmd {
if (ctx.config.diagnosticEnabled && ctx.config.diagnosticScope === "Workspace") {
return async () => {
const userResponse = await vscode.window.showErrorMessage(
`Workspace syntax checking is already running`
);
};
}



return async () => {
if (ctx.config.get4DVersion().compare(new LabeledVersion(20, 5, 0, 0, true, "stable", false)) < 0) {
vscode.window.showErrorMessage(`The workspace syntax checking is available with at least a 20R5`);
return;
}
vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
title: "Workspace syntax check running",
cancellable: false
}, async (progress, token) => {

const client = ctx.client;
const params = client.code2ProtocolConverter.asTextDocumentIdentifier(
vscode.window.activeTextEditor.document
);

const response = await client.sendRequest(ext.checkWorkspaceSyntax, params);
const diagnosticCollection = ctx.workspaceDiagnostic;
diagnosticCollection.clear();
response.items.forEach(diagWorkspace => {
const diagnostics: vscode.Diagnostic[] = [];
const currentItem = diagWorkspace as WorkspaceFullDocumentDiagnosticReport;
const currentDiagnostics = vscode.languages.getDiagnostics(vscode.Uri.parse(currentItem.uri));
for (const diag of currentItem.items) {
const range: vscode.Range = new vscode.Range(diag.range.start.line, diag.range.start.character, diag.range.end.line, diag.range.end.character);
const diagnostic = new vscode.Diagnostic(range, diag.message, diag.severity - 1);
if (!currentDiagnostics.find((cdiagnostic => {
return cdiagnostic.range.isEqual(diagnostic.range) && cdiagnostic.message === diagnostic.message;
}))) {
diagnostics.push(diagnostic);
}
}
diagnosticCollection.set(vscode.Uri.parse(currentItem.uri), diagnostics);
});
});
};
}

24 changes: 22 additions & 2 deletions editor/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { InfoPlistManager } from './infoplist';
export class Config {

readonly rootSection = "4D-Analyzer";

_tool4DPath: string;
_ctx: Ctx;
private readonly requiresReloadOpts = [
Expand All @@ -19,17 +20,19 @@ export class Config {
"server.tool4d.location",
"server.tool4d.enable",
"server.tool4d.channel",
"diagnostics.enable"
"diagnostics.enable",
"diagnostics.scope"
]

.map(opt => `${this.rootSection}.${opt}`);

constructor(ctx: vscode.ExtensionContext) {
vscode.window.onDidChangeActiveTextEditor(this.onDidChangeActiveTextEditor, this, ctx.subscriptions);
vscode.workspace.onDidChangeConfiguration(this.onDidChangeConfiguration, this, ctx.subscriptions);
}

init(ctx: Ctx) {
this._ctx = ctx;
vscode.workspace.onDidChangeConfiguration(this.onDidChangeConfiguration, this, ctx.extensionContext.subscriptions);
}

get cfg(): vscode.WorkspaceConfiguration {
Expand All @@ -40,6 +43,14 @@ export class Config {
this._tool4DPath = inPath;
}

public get diagnosticScope() : string{
return this.get<string>("diagnostics.scope");
}

public get diagnosticEnabled() : string{
return this.get<string>("diagnostics.enable");
}

private get<T>(path: string): T {
return this.cfg.get<T>(path)!;
}
Expand Down Expand Up @@ -136,6 +147,15 @@ export class Config {
}
}

private async onDidChangeActiveTextEditor(event: vscode.TextEditor) {
if(event)
{
await this._ctx?.client.sendNotification("experimental/didChangeActiveTextEditor",
this._ctx?.client.code2ProtocolConverter.asTextDocumentIdentifier(
event.document));
}
}


private async onDidChangeConfiguration(event: vscode.ConfigurationChangeEvent) {

Expand Down
25 changes: 22 additions & 3 deletions editor/src/ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,27 @@ export type CommandCallback = {
call: (ctx: Ctx) => Commands.Cmd;
};


export class Ctx {
private _client: LanguageClient;
private _extensionContext: vscode.ExtensionContext;
private _commands: Record<string, CommandCallback>;
private _config: Config;
private _workspaceDiagnostic : vscode.DiagnosticCollection;

constructor(ctx: vscode.ExtensionContext) {
this._client = null;
this._extensionContext = ctx;
this._commands = {};
this._config = null;
this._workspaceDiagnostic = vscode.languages.createDiagnosticCollection("4d_workspace");
}

public get config(): Config {
return this._config;
}

public get workspaceDiagnostic(): vscode.DiagnosticCollection {
return this._workspaceDiagnostic;
}

public get extensionContext(): vscode.ExtensionContext {
Expand Down Expand Up @@ -132,6 +141,7 @@ export class Ctx {
}

private _launch4D() {
this._config.init(this);
this._config.checkSettings();
let isDebug: boolean;
isDebug = false;
Expand Down Expand Up @@ -210,7 +220,14 @@ export class Ctx {
fileEvents: workspace.createFileSystemWatcher('**/.4DSettings')
},
initializationOptions: this._config.cfg,
diagnosticCollectionName: "4d"
diagnosticCollectionName: "4d",
middleware: {
provideDiagnostics: (document, previousResultId, token, next) => {
if(this._config.diagnosticEnabled)
this._workspaceDiagnostic.set(document instanceof vscode.Uri ? document : document.uri, undefined);
return next(document, previousResultId, token);
}
}
};
// Create the language client and start the client.
this._client = new LanguageClient(
Expand Down Expand Up @@ -250,7 +267,8 @@ export class Ctx {
filesStatus: { call: Commands.filesStatus },
updateTool4D: { call: Commands.updateTool4D },
display4DVersion: { call: Commands.display4DVersion },
cleanUnusedToolVersions: { call: Commands.cleanUnusedToolVersions }
cleanUnusedToolVersions: { call: Commands.cleanUnusedToolVersions },
checkWorkspaceSyntax: { call: Commands.checkWorkspaceSyntax }
};

for (const [name, command] of Object.entries(this._commands)) {
Expand All @@ -277,3 +295,4 @@ export class Ctx {
export interface Disposable {
dispose(): void;
}

5 changes: 5 additions & 0 deletions editor/src/lsp_ext.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import * as lc from "vscode-languageclient";
import { WorkspaceDiagnosticReport } from "vscode-languageclient";

export const filesStatus = new lc.RequestType0<object, void>(
"experimental/filesStatus"
);

export const checkWorkspaceSyntax = new lc.RequestType<lc.TextDocumentIdentifier, WorkspaceDiagnosticReport, void>(
"experimental/checkSyntax"
);

0 comments on commit a971f92

Please sign in to comment.