1- import { Hover } from 'vscode' ;
1+ import { Hover , SymbolKind } from 'vscode' ;
2+ import * as proxy from '../../providers/jediProxy' ;
3+ import { highlightCode } from '../../providers/jediHelpers' ;
4+ import { EOL } from 'os' ;
25export class HoverParser {
3- public static parse ( data : Object ) : Hover {
4- return null ;
6+ public static parse ( data : proxy . IHoverResult , currentWord : string ) : Hover {
7+ let results = [ ] ;
8+ let capturedInfo : string [ ] = [ ] ;
9+ data . items . forEach ( item => {
10+ let { signature } = item ;
11+ switch ( item . kind ) {
12+ case SymbolKind . Constructor :
13+ case SymbolKind . Function :
14+ case SymbolKind . Method : {
15+ signature = 'def ' + signature ;
16+ break ;
17+ }
18+ case SymbolKind . Class : {
19+ signature = 'class ' + signature ;
20+ break ;
21+ }
22+ default : {
23+ signature = typeof item . text === 'string' && item . text . length > 0 ? item . text : currentWord ;
24+ }
25+ }
26+ if ( item . docstring ) {
27+ let lines = item . docstring . split ( / \r ? \n / ) ;
28+ // If the docstring starts with the signature, then remove those lines from the docstring
29+ if ( lines . length > 0 && item . signature . indexOf ( lines [ 0 ] ) === 0 ) {
30+ lines . shift ( ) ;
31+ let endIndex = lines . findIndex ( line => item . signature . endsWith ( line ) ) ;
32+ if ( endIndex >= 0 ) {
33+ lines = lines . filter ( ( line , index ) => index > endIndex ) ;
34+ }
35+ }
36+ if ( lines . length > 0 && item . signature . startsWith ( currentWord ) && lines [ 0 ] . startsWith ( currentWord ) && lines [ 0 ] . endsWith ( ')' ) ) {
37+ lines . shift ( ) ;
38+ }
39+ let descriptionWithHighlightedCode = highlightCode ( lines . join ( EOL ) ) ;
40+ let hoverInfo = [ '```python' , signature , '```' , descriptionWithHighlightedCode ] . join ( EOL ) ;
41+ let key = signature + lines . join ( '' ) ;
42+ // Sometimes we have duplicate documentation, one with a period at the end
43+ if ( capturedInfo . indexOf ( key ) >= 0 || capturedInfo . indexOf ( key + '.' ) >= 0 ) {
44+ return ;
45+ }
46+ capturedInfo . push ( key ) ;
47+ capturedInfo . push ( key + '.' ) ;
48+ results . push ( hoverInfo ) ;
49+ return ;
50+ }
51+ if ( item . description ) {
52+ let descriptionWithHighlightedCode = highlightCode ( item . description ) ;
53+ let hoverInfo = '```python' + EOL + signature + EOL + '```' + EOL + descriptionWithHighlightedCode ;
54+ let lines = item . description . split ( EOL ) ;
55+ let key = signature + lines . join ( '' ) ;
56+ // Sometimes we have duplicate documentation, one with a period at the end
57+ if ( capturedInfo . indexOf ( key ) >= 0 || capturedInfo . indexOf ( key + '.' ) >= 0 ) {
58+ return ;
59+ }
60+ capturedInfo . push ( key ) ;
61+ capturedInfo . push ( key + '.' ) ;
62+ results . push ( hoverInfo ) ;
63+ }
64+ } ) ;
65+ return new Hover ( results ) ;
566 }
667}
0 commit comments