fix: error cannot read 'charAt'

This commit is contained in:
kapitche 2025-07-05 17:00:55 -04:00
parent 437bbf0c9d
commit ef67a8107a
3 changed files with 109 additions and 143 deletions

2
.gitignore vendored
View file

@ -2,3 +2,5 @@ node_modules
*.vsix
/scrapper/discovered_mnemonics.txt
/scrapper/mnemonic_url_mappings.txt
*.s
*.asm

View file

@ -2,7 +2,7 @@
"name": "x86-assembly-syntax",
"displayName": "Universal Assembly Syntax Highlighting",
"description": "Syntax highlighting extension for x86, x64 and ARM assembly both for Intel and AT&T syntax",
"version": "1.2.3",
"version": "1.2.4",
"engines": {
"vscode": "^1.75.0"
},

View file

@ -2,32 +2,26 @@ const vscode = require("vscode");
const fs = require("fs");
const path = require("path");
let languageConfigDisposable = null;
let instructionData = null;
function activate(context) {
// Load instruction data from local JSON file
loadInstructionData();
// Set up initial language configuration
updateLanguageConfiguration();
// Listen for configuration changes
const configChangeListener = vscode.workspace.onDidChangeConfiguration(
(event) => {
if (event.affectsConfiguration("assembly.comments")) {
updateLanguageConfiguration();
}
}
);
// Hover provider for mnemonics
const hoverProvider = vscode.languages.registerHoverProvider("x86asm", {
provideHover(document, position, token) {
try {
const range = document.getWordRangeAtPosition(
position,
/\b[a-zA-Z0-9_]+\b/
);
// Add null check for range to prevent charAt error
if (!range) {
return undefined;
}
const word = document.getText(range);
if (word && instructionData) {
@ -39,6 +33,10 @@ function activate(context) {
}
return undefined;
} catch (error) {
console.error("Error in hover provider:", error);
return undefined;
}
},
});
@ -47,6 +45,7 @@ function activate(context) {
"x86asm",
{
async provideDefinition(document, position, token) {
try {
const range = document.getWordRangeAtPosition(position);
if (!range) {
return;
@ -54,15 +53,15 @@ function activate(context) {
const word = document.getText(range);
return await findDefinition(word);
} catch (error) {
console.error("Error in definition provider:", error);
return undefined;
}
},
}
);
context.subscriptions.push(
hoverProvider,
definitionProvider,
configChangeListener
);
context.subscriptions.push(hoverProvider, definitionProvider);
}
function loadInstructionData() {
@ -97,55 +96,8 @@ function loadInstructionData() {
}
}
function updateLanguageConfiguration() {
// Dispose previous language configuration if it exists
if (languageConfigDisposable) {
languageConfigDisposable.dispose();
}
// Get current configuration
const config = vscode.workspace.getConfiguration("assembly.comments");
const lineComment = config.get("lineComment", ";");
const blockComment = config.get("blockComment", ["/*", "*/"]);
// Register new language configuration
languageConfigDisposable = vscode.languages.setLanguageConfiguration(
"x86asm",
{
comments: {
lineComment: lineComment,
blockComment: blockComment,
},
brackets: [
["{", "}"],
["[", "]"],
["(", ")"],
],
autoClosingPairs: [
["{", "}"],
["[", "]"],
["(", ")"],
['"', '"'],
["'", "'"],
],
surroundingPairs: [
["{", "}"],
["[", "]"],
["(", ")"],
['"', '"'],
["'", "'"],
],
folding: {
markers: {
start: "\\.section",
end: "\\.endsection",
},
},
}
);
}
function getInstructionInfo(instruction) {
try {
if (!instructionData) {
return null;
}
@ -157,7 +109,7 @@ function getInstructionInfo(instruction) {
// Format the hover content using the syntaxes/x86_instructions.json format
// Keep title as is since it usually doesn't need paragraph breaks
let hoverContent = `**${info.title}**\n\n`;
let hoverContent = `**${info.title || "Unknown Instruction"}**\n\n`;
if (info.opcode) {
hoverContent += `**Opcode:** ${info.opcode}\n\n`;
@ -174,9 +126,14 @@ function getInstructionInfo(instruction) {
}
return hoverContent;
} catch (error) {
console.error("Error in getInstructionInfo:", error);
return null;
}
}
async function findDefinition(word) {
try {
// Get all x86 assembly files in the workspace
const files = await vscode.workspace.findFiles(
"**/*.s",
@ -197,9 +154,14 @@ async function findDefinition(word) {
// If no definition is found, return undefined
return undefined;
} catch (error) {
console.error("Error in findDefinition:", error);
return undefined;
}
}
function searchForDefinition(lines, word, document) {
try {
const externRegex = new RegExp(`^extern\\s+${word}\\s*`, "i");
const globalRegex = new RegExp(`^global\\s+${word}\\s*`, "i");
const labelRegex = new RegExp(`^${word}:`, "i");
@ -215,12 +177,14 @@ function searchForDefinition(lines, word, document) {
}
return undefined; // If no match is found
} catch (error) {
console.error("Error in searchForDefinition:", error);
return undefined;
}
}
function deactivate() {
if (languageConfigDisposable) {
languageConfigDisposable.dispose();
}
// No cleanup needed
}
module.exports = {