Skip to content

Commit d6a3fd5

Browse files
committed
Always return markdown content
This fixes a small bug in vscode where documentation that pops out to the right was not formatted using markdown.
1 parent 6434dc7 commit d6a3fd5

File tree

2 files changed

+77
-27
lines changed

2 files changed

+77
-27
lines changed

server/src/__tests__/server.test.ts

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,21 @@ describe('server', () => {
109109
)
110110

111111
expect(result).toBeDefined()
112-
expect(result).toEqual({
113-
contents:
114-
'### Function: **hello_world** - *defined on line 8*\n\n```txt\nthis is a comment\ndescribing the function\nhello_world\nthis function takes two arguments\n```',
115-
})
112+
expect(result).toMatchInlineSnapshot(`
113+
Object {
114+
"contents": Object {
115+
"kind": "markdown",
116+
"value": "Function: **hello_world** - *defined on line 8*
117+
118+
\`\`\`txt
119+
this is a comment
120+
describing the function
121+
hello_world
122+
this function takes two arguments
123+
\`\`\`",
124+
},
125+
}
126+
`)
116127
})
117128

118129
it('responds to onDefinition', async () => {
@@ -491,7 +502,10 @@ describe('server', () => {
491502
"name": "BLUE",
492503
"type": 3,
493504
},
494-
"documentation": "### Variable: **BLUE** - *defined in extension.inc*",
505+
"documentation": Object {
506+
"kind": "markdown",
507+
"value": "Variable: **BLUE** - *defined in extension.inc*",
508+
},
495509
"kind": 6,
496510
"label": "BLUE",
497511
},
@@ -520,11 +534,14 @@ describe('server', () => {
520534
"name": "add_a_user",
521535
"type": 3,
522536
},
523-
"documentation": "### Function: **add_a_user** - *defined in issue101.sh*
537+
"documentation": Object {
538+
"kind": "markdown",
539+
"value": "Function: **add_a_user** - *defined in issue101.sh*
524540
525541
\`\`\`txt
526542
Helper function to add a user
527543
\`\`\`",
544+
},
528545
"kind": 3,
529546
"label": "add_a_user",
530547
},
@@ -607,7 +624,10 @@ describe('server', () => {
607624
"name": "RED",
608625
"type": 3,
609626
},
610-
"documentation": "### Variable: **RED** - *defined in extension.inc*",
627+
"documentation": Object {
628+
"kind": "markdown",
629+
"value": "Variable: **RED** - *defined in extension.inc*",
630+
},
611631
"kind": 6,
612632
"label": "RED",
613633
},
@@ -616,7 +636,10 @@ describe('server', () => {
616636
"name": "GREEN",
617637
"type": 3,
618638
},
619-
"documentation": "### Variable: **GREEN** - *defined in extension.inc*",
639+
"documentation": Object {
640+
"kind": "markdown",
641+
"value": "Variable: **GREEN** - *defined in extension.inc*",
642+
},
620643
"kind": 6,
621644
"label": "GREEN",
622645
},
@@ -625,7 +648,10 @@ describe('server', () => {
625648
"name": "BLUE",
626649
"type": 3,
627650
},
628-
"documentation": "### Variable: **BLUE** - *defined in extension.inc*",
651+
"documentation": Object {
652+
"kind": "markdown",
653+
"value": "Variable: **BLUE** - *defined in extension.inc*",
654+
},
629655
"kind": 6,
630656
"label": "BLUE",
631657
},
@@ -634,7 +660,10 @@ describe('server', () => {
634660
"name": "RESET",
635661
"type": 3,
636662
},
637-
"documentation": "### Variable: **RESET** - *defined in extension.inc*",
663+
"documentation": Object {
664+
"kind": "markdown",
665+
"value": "Variable: **RESET** - *defined in extension.inc*",
666+
},
638667
"kind": 6,
639668
"label": "RESET",
640669
},
@@ -643,7 +672,10 @@ describe('server', () => {
643672
"name": "USER",
644673
"type": 3,
645674
},
646-
"documentation": "### Variable: **USER** - *defined in issue101.sh*",
675+
"documentation": Object {
676+
"kind": "markdown",
677+
"value": "Variable: **USER** - *defined in issue101.sh*",
678+
},
647679
"kind": 6,
648680
"label": "USER",
649681
},
@@ -652,7 +684,10 @@ describe('server', () => {
652684
"name": "PASSWORD",
653685
"type": 3,
654686
},
655-
"documentation": "### Variable: **PASSWORD** - *defined in issue101.sh*",
687+
"documentation": Object {
688+
"kind": "markdown",
689+
"value": "Variable: **PASSWORD** - *defined in issue101.sh*",
690+
},
656691
"kind": 6,
657692
"label": "PASSWORD",
658693
},
@@ -661,11 +696,14 @@ describe('server', () => {
661696
"name": "COMMENTS",
662697
"type": 3,
663698
},
664-
"documentation": "### Variable: **COMMENTS** - *defined in issue101.sh*
699+
"documentation": Object {
700+
"kind": "markdown",
701+
"value": "Variable: **COMMENTS** - *defined in issue101.sh*
665702
666703
\`\`\`txt
667704
Having shifted twice, the rest is now comments ...
668705
\`\`\`",
706+
},
669707
"kind": 6,
670708
"label": "COMMENTS",
671709
},
@@ -674,7 +712,10 @@ describe('server', () => {
674712
"name": "tag",
675713
"type": 3,
676714
},
677-
"documentation": "### Variable: **tag** - *defined in ../../scripts/tag-release.inc*",
715+
"documentation": Object {
716+
"kind": "markdown",
717+
"value": "Variable: **tag** - *defined in ../../scripts/tag-release.inc*",
718+
},
678719
"kind": 6,
679720
"label": "tag",
680721
},

server/src/server.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -343,19 +343,24 @@ export default class BashServer {
343343
}: {
344344
symbol: LSP.SymbolInformation
345345
currentUri: string
346-
}): string {
346+
}): LSP.MarkupContent {
347347
const symbolUri = symbol.location.uri
348-
const symbolStarLine = symbol.location.range.start.line
348+
const symbolStartLine = symbol.location.range.start.line
349349

350-
const commentAboveSymbol = this.analyzer.commentsAbove(symbolUri, symbolStarLine)
350+
const commentAboveSymbol = this.analyzer.commentsAbove(symbolUri, symbolStartLine)
351351
const symbolDocumentation = commentAboveSymbol ? `\n\n${commentAboveSymbol}` : ''
352-
const hoverHeader = `### ${symbolKindToDescription(symbol.kind)}: **${symbol.name}**`
352+
const hoverHeader = `${symbolKindToDescription(symbol.kind)}: **${symbol.name}**`
353353
const symbolLocation =
354354
symbolUri !== currentUri
355355
? `in ${path.relative(path.dirname(currentUri), symbolUri)}`
356-
: `on line ${symbolStarLine + 1}`
356+
: `on line ${symbolStartLine + 1}`
357357

358-
return `${hoverHeader} - *defined ${symbolLocation}*${symbolDocumentation}`
358+
// TODO: An improvement could be to add show the symbol definition in the hover instead
359+
// of the defined location – similar to how VSCode works for languages like TypeScript.
360+
361+
return getMarkdownContent(
362+
`${hoverHeader} - *defined ${symbolLocation}*${symbolDocumentation}`,
363+
)
359364
}
360365

361366
private getCompletionItemsForSymbols({
@@ -427,7 +432,7 @@ export default class BashServer {
427432
) {
428433
const shellDocumentation = await getShellDocumentation({ word })
429434
if (shellDocumentation) {
430-
return { contents: getMarkdownContent(shellDocumentation) }
435+
return { contents: getMarkdownContent(shellDocumentation, 'man') }
431436
}
432437
} else {
433438
const symbolDocumentation = deduplicateSymbols({
@@ -688,7 +693,7 @@ export default class BashServer {
688693
return documentation
689694
? {
690695
...item,
691-
documentation: getMarkdownContent(documentation),
696+
documentation: getMarkdownContent(documentation, 'man'),
692697
}
693698
: item
694699
} catch (error) {
@@ -791,11 +796,15 @@ function symbolKindToDescription(s: LSP.SymbolKind): string {
791796
}
792797
}
793798

794-
const getMarkdownContent = (documentation: string): LSP.MarkupContent => ({
795-
value: ['``` man', documentation, '```'].join('\n'),
796-
// Passed as markdown for syntax highlighting
797-
kind: 'markdown' as const,
798-
})
799+
function getMarkdownContent(documentation: string, language?: string): LSP.MarkupContent {
800+
return {
801+
value: language
802+
? // eslint-disable-next-line prefer-template
803+
['``` ' + language, documentation, '```'].join('\n')
804+
: documentation,
805+
kind: LSP.MarkupKind.Markdown,
806+
}
807+
}
799808

800809
function getCommandOptions(name: string, word: string): string[] {
801810
// TODO: The options could be cached.

0 commit comments

Comments
 (0)