I am trying to develop a feature in my VS Code extension that allows me to retrieve the range of a custom word when I hover over it, provided that the line of text matches a specific pattern. Here is the code snippet I have implemented so far:
vscode.languages.registerHoverProvider('.mylanguage', {
provideHover(document, position, token) {
// define `hoverRange` somewhere here
const hoverLineText = document.lineAt(position.line).text;
const pattern = new RegExp("\\w+\\s{0,}\\(.{0,}\\s{0,}\\)");
if(pattern.test(hoverLineText)){
hoverRange = document.getWordRangeAtPosition(position, pattern);
}
console.log(hoverRange);
//etc. ...
The expected behavior is that upon hovering on any part (even whitespace) of a string like myFunction ( )
, the console should output the value of hoverRange
, which should include the closing parenthesis )
.
However, currently, nothing is displayed in the console when hovering over whitespace. To obtain the complete range of the string, I must specifically hover over myFunction
.
How can I modify my VS Code extension to consider myFunction ( )
as a single unit?