最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - VS Code Hover Extension implement HoverProvider - Stack Overflow

programmeradmin2浏览0评论

Trying to add hovers to add hovers to my VS Code extension. I was able to syntax highlighting and mands to work, but stuck on adding this hover feature.

I think my blocker is how to properly implement the HoverProvider API. I'm doing a simple test below for a hover provider that activates when a series of tokens are recognized as the keyword HELLO. The hover I've implemented in my testing. I'm using vsce package to package and test my extension locally.

My mand for the extension works, but when I hover over the word "HELLO", my hover does not appear.

./client/extension.js

const vscode = require('vscode');

function activate(context) {

    console.log('Congratulations, your extension "star-rod" is now active!');

    let disposable = vscodemands.registerCommand('extension.mamar', () => {
        vscode.window.showInformationMessage("The Star Rod... is powerful beyond belief. It can grant any wish. For as long as we can remember, Bowser has been making wishes like, for instance... 'I'd like to trounce Mario' or 'I want Princess Peach to like me.' Of course, Stars ignore such selfish wishes. As a result, his wishes were never granted.");
    });

    context.subscriptions.push(disposable);

    vscode.languages.registerHoverProvider('javascript', {
        provideHover(document, position, token) {

            const range = document.getWordRangeAtPosition(position);
            const word = document.getText(range);

            if (word == "HELLO") {

                return new vscode.Hover({
                    language: "Hello language",
                    value: "Hello Value"
                });
            }
        }
    });
}

function deactivate() { }

module.exports = {
    activate,
    deactivate
}

./package.json

    {
 "name": "star-rod-script",
 "publisher": "sonicspiral",
 "displayName": "Star Rod Script",
 "description": "Syntax highlighting for Paper Mario 64 ROM patching tool",
 "version": "1.0.1",
 "repository": {
  "type": "git",
  "url": ".git"
 },
 "categories": [
  "Programming Languages"
 ],
 "activationEvents": [
    "onCommand:extension.mamar",
    "onLanguage:star-rod-script"
 ],
 "engines": {
  "vscode": "^1.31.0"
 },
 "main": "./client/extension.js",
 "contributes": {
  "capabilities": {
    "hoverProvider": "true"
  },
  "mands": [
    {
     "mand": "extension.mamar",
     "title": "Mamar"
    }
  ],
  "languages": [
   {
    "id": "star-rod-script",
    "extensions": [
     ".bpat",
     ".bscr",
     ".mpat",
     ".mscr"
    ],
    "aliases": [
     "Star Rod Script",
     "mscr"
    ],
    "configuration": "./language-configuration.json"
   }
  ],
  "grammars": [
   {
    "language": "star-rod-script",
    "scopeName": "source.mscr",
    "path": "./syntaxes/mscr.tmLanguage.json"
   }
  ]
 },
 "devDependencies": {
  "js-yaml": "^3.12.1",
  "vscode": "^1.1.29"
 }
}

Trying to add hovers to add hovers to my VS Code extension. I was able to syntax highlighting and mands to work, but stuck on adding this hover feature.

I think my blocker is how to properly implement the HoverProvider API. I'm doing a simple test below for a hover provider that activates when a series of tokens are recognized as the keyword HELLO. The hover I've implemented in my testing. I'm using vsce package to package and test my extension locally.

My mand for the extension works, but when I hover over the word "HELLO", my hover does not appear.

./client/extension.js

const vscode = require('vscode');

function activate(context) {

    console.log('Congratulations, your extension "star-rod" is now active!');

    let disposable = vscode.mands.registerCommand('extension.mamar', () => {
        vscode.window.showInformationMessage("The Star Rod... is powerful beyond belief. It can grant any wish. For as long as we can remember, Bowser has been making wishes like, for instance... 'I'd like to trounce Mario' or 'I want Princess Peach to like me.' Of course, Stars ignore such selfish wishes. As a result, his wishes were never granted.");
    });

    context.subscriptions.push(disposable);

    vscode.languages.registerHoverProvider('javascript', {
        provideHover(document, position, token) {

            const range = document.getWordRangeAtPosition(position);
            const word = document.getText(range);

            if (word == "HELLO") {

                return new vscode.Hover({
                    language: "Hello language",
                    value: "Hello Value"
                });
            }
        }
    });
}

function deactivate() { }

module.exports = {
    activate,
    deactivate
}

./package.json

    {
 "name": "star-rod-script",
 "publisher": "sonicspiral",
 "displayName": "Star Rod Script",
 "description": "Syntax highlighting for Paper Mario 64 ROM patching tool",
 "version": "1.0.1",
 "repository": {
  "type": "git",
  "url": "https://github./gregdegruy/star-rod.git"
 },
 "categories": [
  "Programming Languages"
 ],
 "activationEvents": [
    "onCommand:extension.mamar",
    "onLanguage:star-rod-script"
 ],
 "engines": {
  "vscode": "^1.31.0"
 },
 "main": "./client/extension.js",
 "contributes": {
  "capabilities": {
    "hoverProvider": "true"
  },
  "mands": [
    {
     "mand": "extension.mamar",
     "title": "Mamar"
    }
  ],
  "languages": [
   {
    "id": "star-rod-script",
    "extensions": [
     ".bpat",
     ".bscr",
     ".mpat",
     ".mscr"
    ],
    "aliases": [
     "Star Rod Script",
     "mscr"
    ],
    "configuration": "./language-configuration.json"
   }
  ],
  "grammars": [
   {
    "language": "star-rod-script",
    "scopeName": "source.mscr",
    "path": "./syntaxes/mscr.tmLanguage.json"
   }
  ]
 },
 "devDependencies": {
  "js-yaml": "^3.12.1",
  "vscode": "^1.1.29"
 }
}
Share Improve this question edited Feb 21, 2019 at 23:31 greg asked Feb 20, 2019 at 17:45 greggreg 1,2042 gold badges24 silver badges52 bronze badges 3
  • Does your extension get activated? Can you provide more information about what does not work and what you have tried? – Matt Bierner Commented Feb 21, 2019 at 4:15
  • Yes, but hover dialog do not appears when I hover over my test "HELLO" keyword. I just need an example how to create a hover with proper syntax, I don't fully understand the API around it. – greg Commented Feb 21, 2019 at 5:20
  • Is it normal that you never use the token that you passed as input at provideHover? – supe345 Commented Aug 25, 2021 at 15:32
Add a ment  | 

2 Answers 2

Reset to default 4

Your package.json looks a bit odd. I bet your extension is not activated. The "contributes/capabilites" value is something I haven't seen before. Remove that and instead change your activationEvents to:

"activationEvents": [
    "onLanguage:star-rod-script"
],

Your code allowed me to get Hovers working in my first extension. I think your mistake is having javascript as the selector: vscode.DocumentSelector. Is that there from code you copied? That should probably be set to star-rod-script for your extension.

I also don't have "capabilities": {"hoverProvider": "true"} in mine. I changed your code to:

disposable = vscode.languages.registerHoverProvider('star-rod-script', {  // or 'star rod script'
    //....
});

context.subscriptions.push(disposable);

I don't know the nuances of how you apply your extension to certain documents, but it doesn't look like you're trying to apply the hover to javascript docs. You need the selector to include the docs your extension works with. In my case that's covered by my extension name which is the language mode that shows up in the vscode status bar. More info on document-selectors.

Not sure if it's needed, but I also took the return and pushed it onto the subscriptions array. Works without that, but I think that's proper??

发布评论

评论列表(0)

  1. 暂无评论