Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline hover #123

Merged
merged 4 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
// 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 @@ -396,6 +399,55 @@
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) {

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

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

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
// 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 @@ -216,6 +219,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
Loading