From 8b1c94db6f00026485b873afe2705711c87cc745 Mon Sep 17 00:00:00 2001 From: DenisPower1 Date: Mon, 9 Sep 2024 22:50:50 +0100 Subject: [PATCH] Upgrade to v1.2.0 --- README.md | 3 +- package.json | 2 +- src/extension.ts | 167 ++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 160 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 1f47a93..e74bc41 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ The extension will be activated as soon as an HTML file is opened. You can Downl from [Vscode market place](https://marketplace.visualstudio.com/items?itemName=interjs.inter-intellisense) or from [releases](https://github.com/interjs/inter-intellisense/releases) of this repository. Keep in mind that if you download the extension from the releases, you have to install it directly in vscode. -Check out the video demo by clicking on: video demo. +Check out the video demos by clicking on: video demo 1 +and video demo 2. diff --git a/package.json b/package.json index 40411b7..8d93dd9 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "url": "https://github.com/interjs/inter-intellisense.git" }, "module": "esnext", - "version": "1.1.1", + "version": "1.2.0", "engines": { "vscode": "^1.57.1" }, diff --git a/src/extension.ts b/src/extension.ts index 707b221..ea19229 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,8 +1,100 @@ import * as vscode from "vscode"; import { commentsParser, parserInfoInterface } from "./htmlcommentsparser"; -const extensionVersion: string = "1.1.1"; +const extensionVersion: string = "1.2.0"; const openTagRegExp = /<(:?[A-Z]+)>/gi; +interface configI { + text: string; + start: number; + end: number; + justWarning?: boolean; + type: registeredT; +} + +type registeredT = "ref" | "conditional"; + +function getRegistered( + document: string, + type: registeredT +): parserInfoInterface | undefined { + const registered = new commentsParser().parse(document); + + let returnValue; + if (registered[0].type == type) returnValue = registered[0]; + else if (registered[1].type == type) returnValue = registered[1]; + + return returnValue; +} + +function findUnregisteredRef( + documentContent: string, + document: vscode.TextDocument +): configI[] { + const refReg = /\{(:?\s+)*(:?[A-Z]+)(:?\s+)*\}/gi; + const allRefs = documentContent.match(refReg) || []; + const unregistered = []; + const registered = getRegistered(documentContent, "ref"); + + for (const ref of allRefs) { + const text = ref.replace("{", "").replace("}", "").trim(); + const start = documentContent.indexOf(ref) + 1; + const position = document.positionAt(start); + + const refSet = new Set(registered?.content); + + if (refSet.has(text)) continue; + else if (outOfCompletionArea(document, position)) continue; + + const config: configI = { + text: text, + start: start, + end: documentContent.indexOf(ref) + ref.length, + justWarning: true, + type: "ref", + }; + + unregistered.push(config); + } + + return unregistered; +} + +function findUnregisteredConditionalProps(documentContent: string): configI[] { + const attrsReg: RegExp = + /_if="(:?\s)*(:?[A-Z]+)(:?\s)*"|_elseIf="(:?\s)*(:?[A-Z]+)(:?\s)*"|_ifNot="(:?\s)*(:?[A-Z]+)(:?\s)*"|_else="(:?\s)*(:?[A-Z]+)(:?\s)*"/gi; + const allAttrs = documentContent.match(attrsReg) || []; + const unregistered = []; + const registered = getRegistered(documentContent, "conditional"); + + for (const attr of allAttrs) { + let theConditionalAttrsLength: number = 0; + let attrName: string = ""; + + const text = attr + .replace(/_if="|_elseIf="|_ifNot="|_else="/, (prop) => { + theConditionalAttrsLength = prop.length; + attrName = prop.replace('="', ""); + + return ""; + }) + .replace('"', "") + .trim(); + const conditionalSet = new Set(registered?.content); + if (conditionalSet.has(text) && attrName !== "_else") continue; + + const config: configI = { + text: text, + start: documentContent.indexOf(attr) + theConditionalAttrsLength, + end: documentContent.indexOf(attr) + attr.length, + justWarning: attrName == "_else", + type: "conditional", + }; + + unregistered.push(config); + } + + return unregistered; +} function getHoveredToken(cursorPosition: number, lineText: string): string { let body: string = ""; @@ -256,14 +348,6 @@ class onhoverInformation implements vscode.HoverProvider { If ${hoverContent} is set to true, the element whose its conditional property is ${hoverContent} will be rendered. - `; - - const nullDescription = ` - ${hoverContent} is neither a reference's name nor a conditional property. - If it was supposed to be one of them, then it was not registered, register it like that: - - For reference. - For conditional property. `; if (!noHoverInfo) { @@ -281,6 +365,69 @@ class onhoverInformation implements vscode.HoverProvider { } export function activate(context: vscode.ExtensionContext) { + const diagnosticCollecction = + vscode.languages.createDiagnosticCollection("InterError"); + + function runDiagnosticLoop( + unregisteredCollection: configI[], + diagnostics: vscode.Diagnostic[], + toPosition: Function + ) { + for (const prop of unregisteredCollection) { + const start = toPosition(prop.start); + const end = toPosition(prop.end); + const diagnosticSeverity = vscode.DiagnosticSeverity; + const notRegisteredPropError = !prop.justWarning + ? ` + ${prop.text} is not a registered conditional property. Register it like: + + + + ` + : "Nothing is wrong here, but _else does not expect a value!"; + const notRegisteredRefWarning = ` + ${prop.text} is not a registered reference's name. Register it like: + + + + `; + + const diagnostic = new vscode.Diagnostic( + new vscode.Range(start, end), + prop.type == "ref" ? notRegisteredRefWarning : notRegisteredPropError, + prop.justWarning ? diagnosticSeverity.Warning : diagnosticSeverity.Error + ); + + diagnostics.push(diagnostic); + } + } + + function runDiagnosticCode(document: vscode.TextDocument) { + const documentContent = document.getText(); + const toPosition = (offset: number) => document.positionAt(offset); + const diagnostics: vscode.Diagnostic[] = []; + const unregisteredConditional = + findUnregisteredConditionalProps(documentContent); + const unregisteredRef = findUnregisteredRef(documentContent, document); + + runDiagnosticLoop(unregisteredConditional, diagnostics, toPosition); + runDiagnosticLoop(unregisteredRef, diagnostics, toPosition); + + diagnosticCollecction.set(document.uri, diagnostics); + } + + vscode.workspace.onDidOpenTextDocument((document) => { + runDiagnosticCode(document); + }); + + vscode.workspace.onDidChangeTextDocument((event) => { + runDiagnosticCode(event.document); + }); + + vscode.window.onDidChangeActiveTextEditor(() => { + console.log("Here!"); + }); + vscode.window.showInformationMessage(` Inter HTML intellisense is now activated, have a nice coding section! `); @@ -302,5 +449,5 @@ export function activate(context: vscode.ExtensionContext) { new onhoverInformation() ); - context.subscriptions.push(disp, disp2, disp3); + context.subscriptions.push(disp, disp2, disp3, diagnosticCollecction); }