release: WRNexusJS 0.6.0
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
"use strict";
|
||||
|
||||
const vscode = require("vscode");
|
||||
const path = require("node:path");
|
||||
const {
|
||||
parseComponentMetadata,
|
||||
parseComponentTags,
|
||||
@@ -75,6 +76,38 @@ function openingTagAt(source, offset) {
|
||||
return match ? { name: match[1], fragment } : null;
|
||||
}
|
||||
|
||||
function componentAlreadyImported(source, name) {
|
||||
const escaped = name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
return (
|
||||
new RegExp(`\\bimport[\\s\\S]*?\\b${escaped}\\b[\\s\\S]*?\\bfrom\\b`).test(source) ||
|
||||
new RegExp(`\\b(?:component|layout)\\s+${escaped}\\b`).test(source)
|
||||
);
|
||||
}
|
||||
|
||||
function importEditForComponent(document, component) {
|
||||
if (!component?.uri || componentAlreadyImported(document.getText(), component.name)) return null;
|
||||
const target = component.uri.fsPath.replace(/\\/g, "/");
|
||||
const current = document.uri.fsPath.replace(/\\/g, "/");
|
||||
let statement;
|
||||
if (
|
||||
target.includes("/node_modules/@wrnexus/ui/components/") ||
|
||||
target.includes("/packages/ui/components/")
|
||||
) {
|
||||
statement = `import { ${component.name} } from "@wrnexus/ui"\n`;
|
||||
} else {
|
||||
const appMarker = "/app/";
|
||||
const appIndex = target.lastIndexOf(appMarker);
|
||||
if (appIndex !== -1) {
|
||||
statement = `import ${component.name} from "@/${target.slice(appIndex + appMarker.length)}"\n`;
|
||||
} else {
|
||||
let relative = path.posix.relative(path.posix.dirname(current), target);
|
||||
if (!relative.startsWith(".")) relative = `./${relative}`;
|
||||
statement = `import ${component.name} from "${relative}"\n`;
|
||||
}
|
||||
}
|
||||
return vscode.TextEdit.insert(new vscode.Position(0, 0), statement);
|
||||
}
|
||||
|
||||
function propSnippet(prop) {
|
||||
if (prop.type === "boolean") return `${prop.name}="{\${1:false}}"`;
|
||||
if (prop.type === "number") return `${prop.name}="{\${1:0}}"`;
|
||||
@@ -127,6 +160,8 @@ async function provideComponentCompletions(document, position) {
|
||||
item.insertText = new vscode.SnippetString(
|
||||
`${component.name}${required.map((prop, index) => ` ${prop.name}="\${${index + 1}}"`).join("")} />`,
|
||||
);
|
||||
const importEdit = importEditForComponent(document, component);
|
||||
if (importEdit) item.additionalTextEdits = [importEdit];
|
||||
items.push(item);
|
||||
}
|
||||
return items;
|
||||
@@ -236,7 +271,9 @@ function registerComponentIntelligence(context) {
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
componentAlreadyImported,
|
||||
componentMarkdown,
|
||||
importEditForComponent,
|
||||
openingTagAt,
|
||||
propDocumentation,
|
||||
propSnippet,
|
||||
|
||||
Reference in New Issue
Block a user