Skip to content

Commit

Permalink
Merge pull request #123 from splunk/hover
Browse files Browse the repository at this point in the history
Inline hover
  • Loading branch information
JasonConger authored May 21, 2024
2 parents 1db02ec + 0da8757 commit 2371de9
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
6 changes: 4 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,22 @@
"preLaunchTask": "${defaultBuildTask}"
},
{
"name": "Mocha Tests",
"type": "node",
"args": [
"--timeout",
"999999",
"--colors",
"${workspaceFolder}/test"
],
"internalConsoleOptions": "openOnSessionStart",
"name": "Mocha Tests",

"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"request": "launch",
"skipFiles": [
"<node_internals>/**"
],
"type": "node"
"preLaunchTask": "npm: install"
},
{
"name": "Extension Functional Tests",
Expand Down
52 changes: 52 additions & 0 deletions out/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,9 @@ function handleSplunkDocument(context) {
// Register Setting completion items for this spec
let trimWhitespace = vscode.workspace.getConfiguration().get('splunk.spec.trimEqualSignWhitespace')
context.subscriptions.push(provideSettingCompletionItems(specConfig, trimWhitespace));

// Register Hovers
context.subscriptions.push(provideHovers(specConfig));
}

// Set up diagnostics (linting)
Expand Down Expand Up @@ -403,6 +406,55 @@ function checkSpecFilePath(specFilePath) {
return specFilePath;
}

function provideHovers(specConfig) {

let enableHover = vscode.workspace.getConfiguration().get('splunk.showDocumentationOnHover');
if(!enableHover) {
return;
}

// Get the currently open document
let currentDocument = path.basename(vscode.window.activeTextEditor.document.uri.fsPath);

vscode.languages.registerHoverProvider({ language: 'splunk', pattern: `**/${currentDocument}`}, {

provideHover(document, position, token) {

Check warning on line 421 in out/extension.js

View workflow job for this annotation

GitHub Actions / unit-test (macos-latest)

'token' is defined but never used

Check warning on line 421 in out/extension.js

View workflow job for this annotation

GitHub Actions / unit-test (ubuntu-latest)

'token' is defined but never used

Check warning on line 421 in out/extension.js

View workflow job for this annotation

GitHub Actions / unit-test (windows-latest)

'token' is defined but never used

const range = document.getWordRangeAtPosition(position, /\w[-\w\.]*/g);
const word = document.getText(range);

if((document.lineAt(position.line).text.startsWith('['))) {
// This is a stanza

// Get stanzas for this .spec file
// Find the hovered word
// Add a hover for the value
let stanza = specConfig["stanzas"].find(item => item.stanzaName === word)
if(stanza) {
let hoverContent = new vscode.MarkdownString(stanza["docString"])
hoverContent.isTrusted = true;
return new vscode.Hover(hoverContent);
}
} else {
// This might be a setting
let parentStanza = getParentStanza(document, position.line);
if(parentStanza) {
let stanzaSettings = splunkSpec.getStanzaSettings(specConfig, parentStanza)
let setting = stanzaSettings.find(item => item.name === word)
if(setting) {
let hoverContent = new vscode.MarkdownString(setting["docString"])
hoverContent.isTrusted = true;
return new vscode.Hover(hoverContent);
}
}
}
}

})

return null
}

function provideStanzaCompletionItems(specConfig) {

// Get the currently open document
Expand Down
11 changes: 11 additions & 0 deletions out/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,15 @@ function getStanzaType(stanza) {
return stanzaType
}

function addDefaultsAndGlobals(settings, defaults) {
// Add [default] or [global] settings to a stanza if they do not already exist
for(let i=0; i < defaults.length; i++) {
if(!settings.find(o => o.name === defaults[i].name && o.value === defaults[i].value)) {
settings.push(defaults[i])
}
}
}

function getStanzaSettings(specConfig, stanzaName) {
// Given a stanzaName, return its settings
// Stanzas could follow one of these syntaxes:
Expand All @@ -439,6 +448,7 @@ function getStanzaSettings(specConfig, stanzaName) {
}

let defaultSettings = getStanzaSettingsByStanzaName(specConfig, "[default]")
let globalSettings = getStanzaSettingsByStanzaName(specConfig, "[global]")
let stanzaType = getStanzaType(stanzaName)
if((stanzaName == "[default]") && (!specConfig.allowsFreeformStanzas)) { return defaultSettings }

Expand All @@ -465,6 +475,7 @@ function getStanzaSettings(specConfig, stanzaName) {

if(settings) {
settings.push(...defaultSettings)
addDefaultsAndGlobals(settings, globalSettings)
return settings
}
break
Expand Down
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@
]
}
],
"capabilities": {
"hoverProvider": "true"
},
"grammars": [
{
"language": "splunk",
Expand Down Expand Up @@ -218,6 +221,13 @@
"default": true,
"order": 14,
"description": "[SPL2] Automatically update to the latest version of the SPL2 language server."
},
"splunk.showDocumentationOnHover": {
"type": "boolean",
"scope": "machine",
"default": true,
"order": 15,
"description": "Show .conf file documentation when hovering over settings."
}
}
},
Expand Down

0 comments on commit 2371de9

Please sign in to comment.