Skip to content

Commit

Permalink
(feat) add documentation strings to hover response (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
fnune authored Jul 5, 2020
1 parent eb19ef1 commit 7fe198b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,19 @@ export class TypeScriptPlugin
if (!info) {
return null;
}
const contents = ts.displayPartsToString(info.displayParts);
const declaration = ts.displayPartsToString(info.displayParts);
const documentation = typeof info.documentation === 'string'
? info.documentation
: ts.displayPartsToString(info.documentation);

// https://microsoft.github.io/language-server-protocol/specification#textDocument_hover
const contents = ['```typescript', declaration, '```']
.concat(documentation ? ['---', documentation] : [])
.join('\n');

return mapHoverToParent(fragment, {
range: convertRange(fragment, info.textSpan),
contents: { language: 'ts', value: contents },
contents,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,22 +104,37 @@ describe('TypescriptPlugin', () => {
]);
});

it('provides hover info', async () => {
it('provides basic hover info when no docstring exists', async () => {
const { plugin, document } = setup('hoverinfo.svelte');

assert.deepStrictEqual(await plugin.doHover(document, Position.create(0, 14)), <Hover>{
contents: {
language: 'ts',
value: 'const a: true',
assert.deepStrictEqual(await plugin.doHover(document, Position.create(4, 10)), <Hover>{
contents: '```typescript\nconst withoutDocs: true\n```',
range: {
start: {
character: 10,
line: 4,
},
end: {
character: 21,
line: 4,
},
},
});
});

it('provides formatted hover info when a docstring exists', async () => {
const { plugin, document } = setup('hoverinfo.svelte');

assert.deepStrictEqual(await plugin.doHover(document, Position.create(2, 10)), <Hover>{
contents: '```typescript\nconst withDocs: true\n```\n---\nDocumentation string',
range: {
start: {
character: 14,
line: 0,
character: 10,
line: 2,
},
end: {
character: 15,
line: 0,
character: 18,
line: 2,
},
},
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
<script>const a = true</script>
<script>
/** Documentation string */
const withDocs = true
const withoutDocs = true
</script>

0 comments on commit 7fe198b

Please sign in to comment.