Skip to content

Commit 2371de9

Browse files
authored
Merge pull request #123 from splunk/hover
Inline hover
2 parents 1db02ec + 0da8757 commit 2371de9

File tree

4 files changed

+77
-2
lines changed

4 files changed

+77
-2
lines changed

.vscode/launch.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,22 @@
3131
"preLaunchTask": "${defaultBuildTask}"
3232
},
3333
{
34+
"name": "Mocha Tests",
35+
"type": "node",
3436
"args": [
3537
"--timeout",
3638
"999999",
3739
"--colors",
3840
"${workspaceFolder}/test"
3941
],
4042
"internalConsoleOptions": "openOnSessionStart",
41-
"name": "Mocha Tests",
43+
4244
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
4345
"request": "launch",
4446
"skipFiles": [
4547
"<node_internals>/**"
4648
],
47-
"type": "node"
49+
"preLaunchTask": "npm: install"
4850
},
4951
{
5052
"name": "Extension Functional Tests",

out/extension.js

+52
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,9 @@ function handleSplunkDocument(context) {
316316
// Register Setting completion items for this spec
317317
let trimWhitespace = vscode.workspace.getConfiguration().get('splunk.spec.trimEqualSignWhitespace')
318318
context.subscriptions.push(provideSettingCompletionItems(specConfig, trimWhitespace));
319+
320+
// Register Hovers
321+
context.subscriptions.push(provideHovers(specConfig));
319322
}
320323

321324
// Set up diagnostics (linting)
@@ -403,6 +406,55 @@ function checkSpecFilePath(specFilePath) {
403406
return specFilePath;
404407
}
405408

409+
function provideHovers(specConfig) {
410+
411+
let enableHover = vscode.workspace.getConfiguration().get('splunk.showDocumentationOnHover');
412+
if(!enableHover) {
413+
return;
414+
}
415+
416+
// Get the currently open document
417+
let currentDocument = path.basename(vscode.window.activeTextEditor.document.uri.fsPath);
418+
419+
vscode.languages.registerHoverProvider({ language: 'splunk', pattern: `**/${currentDocument}`}, {
420+
421+
provideHover(document, position, token) {
422+
423+
const range = document.getWordRangeAtPosition(position, /\w[-\w\.]*/g);
424+
const word = document.getText(range);
425+
426+
if((document.lineAt(position.line).text.startsWith('['))) {
427+
// This is a stanza
428+
429+
// Get stanzas for this .spec file
430+
// Find the hovered word
431+
// Add a hover for the value
432+
let stanza = specConfig["stanzas"].find(item => item.stanzaName === word)
433+
if(stanza) {
434+
let hoverContent = new vscode.MarkdownString(stanza["docString"])
435+
hoverContent.isTrusted = true;
436+
return new vscode.Hover(hoverContent);
437+
}
438+
} else {
439+
// This might be a setting
440+
let parentStanza = getParentStanza(document, position.line);
441+
if(parentStanza) {
442+
let stanzaSettings = splunkSpec.getStanzaSettings(specConfig, parentStanza)
443+
let setting = stanzaSettings.find(item => item.name === word)
444+
if(setting) {
445+
let hoverContent = new vscode.MarkdownString(setting["docString"])
446+
hoverContent.isTrusted = true;
447+
return new vscode.Hover(hoverContent);
448+
}
449+
}
450+
}
451+
}
452+
453+
})
454+
455+
return null
456+
}
457+
406458
function provideStanzaCompletionItems(specConfig) {
407459

408460
// Get the currently open document

out/spec.js

+11
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,15 @@ function getStanzaType(stanza) {
419419
return stanzaType
420420
}
421421

422+
function addDefaultsAndGlobals(settings, defaults) {
423+
// Add [default] or [global] settings to a stanza if they do not already exist
424+
for(let i=0; i < defaults.length; i++) {
425+
if(!settings.find(o => o.name === defaults[i].name && o.value === defaults[i].value)) {
426+
settings.push(defaults[i])
427+
}
428+
}
429+
}
430+
422431
function getStanzaSettings(specConfig, stanzaName) {
423432
// Given a stanzaName, return its settings
424433
// Stanzas could follow one of these syntaxes:
@@ -439,6 +448,7 @@ function getStanzaSettings(specConfig, stanzaName) {
439448
}
440449

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

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

466476
if(settings) {
467477
settings.push(...defaultSettings)
478+
addDefaultsAndGlobals(settings, globalSettings)
468479
return settings
469480
}
470481
break

package.json

+10
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@
7575
]
7676
}
7777
],
78+
"capabilities": {
79+
"hoverProvider": "true"
80+
},
7881
"grammars": [
7982
{
8083
"language": "splunk",
@@ -218,6 +221,13 @@
218221
"default": true,
219222
"order": 14,
220223
"description": "[SPL2] Automatically update to the latest version of the SPL2 language server."
224+
},
225+
"splunk.showDocumentationOnHover": {
226+
"type": "boolean",
227+
"scope": "machine",
228+
"default": true,
229+
"order": 15,
230+
"description": "Show .conf file documentation when hovering over settings."
221231
}
222232
}
223233
},

0 commit comments

Comments
 (0)