11import * as vscode from 'vscode' ;
2- import { MarkedString } from 'vscode-languageclient' ;
32import { LANGUAGE } from './LanguageDesc' ;
43
54class CashscriptHoverProvider implements vscode . HoverProvider {
@@ -13,20 +12,22 @@ class CashscriptHoverProvider implements vscode.HoverProvider{
1312 let range = document . getWordRangeAtPosition ( position , this . re ) ;
1413 let word = document . getText ( range ) ;
1514
16- //this.channel.appendLine("hoverword: "+word)
15+
16+ const varTypes = this . getVariableTypes ( document , word ) ; // fix this
17+ if ( varTypes ) return new vscode . Hover ( varTypes , range ) ;
18+
1719 const annotation = this . getHoverAnnotation ( word ) ;
1820 if ( annotation ) return new vscode . Hover ( annotation , range ) ;
1921
22+ const memberHovers = this . getMemberHovers ( document , word ) ;
23+ if ( memberHovers ) return new vscode . Hover ( memberHovers , range ) ;
24+
2025 const miscel = this . getMiscellaneousHovers ( document , position ) ;
2126 if ( miscel ) return new vscode . Hover ( miscel , range )
2227
23- // check special words
24-
2528 const dotHovers = this . getTxDotHovers ( document , position ) ;
2629 if ( dotHovers ) return new vscode . Hover ( dotHovers , range )
2730
28- const varTypes = this . getVariableTypes ( document , word ) ; // fix this
29- if ( varTypes ) return new vscode . Hover ( varTypes , range ) ;
3031
3132 return null ;
3233 }
@@ -60,16 +61,28 @@ class CashscriptHoverProvider implements vscode.HoverProvider{
6061 * Very bad way to get type annotations, better option: custom Tree Builder
6162 */
6263 getVariableTypes ( document :vscode . TextDocument , targetWord :string ) :vscode . MarkdownString [ ] {
63- const reg = / ( [ a - z A - Z 0 - 9 ] + ) \s + ( p k ) [ ^ a - z A - Z 0 - 9 ] / ;
64- const text = document . getText ( ) ;
65- const matches = text . match ( new RegExp ( `([a-zA-Z0-9]+)\\s+(${ targetWord } )[^a-zA-Z0-9]` ) ) ; //regex still incomplete
66- if ( ! matches ) return null ;
6764
65+ const type = this . getVariableType ( targetWord , document ) ;
66+ if ( ! type ) return null ;
6867 return [
69- new vscode . MarkdownString ( ) . appendCodeblock ( `${ matches [ 1 ] } ${ matches [ 2 ] } ` ) ,
68+ new vscode . MarkdownString ( ) . appendCodeblock ( `${ type } ${ targetWord } ` ) ,
7069 ]
7170 }
7271
72+ /**
73+ * Gets the data type of a variable
74+ *
75+ * @param variable the variable to be seached for
76+ * @param document the entire text document
77+ * @returns a string of the data type
78+ */
79+ getVariableType ( variable :string , document :vscode . TextDocument ) {
80+ const text = document . getText ( ) ;
81+ const matches = text . match ( new RegExp ( `\\b(int|bool|string|pubkey|sig|datasig|bytes\\d*)\\s+${ variable } \\b` ) ) ; //regex still incomplete
82+ if ( ! matches ) return null ;
83+ return matches [ 1 ] ;
84+ }
85+
7386 getTxDotHovers ( document :vscode . TextDocument , position :vscode . Position ) :vscode . MarkdownString [ ] {
7487 const reg = / t x .[ a - z A - Z 0 - 9 ] + / ;
7588 let range = document . getWordRangeAtPosition ( position , reg ) ;
@@ -116,9 +129,29 @@ class CashscriptHoverProvider implements vscode.HoverProvider{
116129 }
117130
118131
119- return [
120- new vscode . MarkdownString ( ) . appendCodeblock ( TX_HOVERS [ word ] . code )
121- ] ;
132+ if ( TX_HOVERS [ word ] . code ) {
133+ return [
134+ new vscode . MarkdownString ( ) . appendCodeblock ( TX_HOVERS [ word ] . code )
135+ ] ;
136+ }
137+
138+ return null
139+ }
140+
141+ getMemberHovers ( document :vscode . TextDocument , word :string ) {
142+ if ( word === "split" ) {
143+ return [
144+ new vscode . MarkdownString ( ) . appendCodeblock ( '[s1, s2] sequence.split(int i)' ) ,
145+ new vscode . MarkdownString ( 'Splits the sequence at the specified index and returns a tuple with the two resulting sequences.' )
146+ ]
147+ } else if ( word === "reverse" ) {
148+ return [
149+ new vscode . MarkdownString ( ) . appendCodeblock ( 'any sequence.reverse()' ) ,
150+ new vscode . MarkdownString ( 'Reverses the sequence.' )
151+ ]
152+ }
153+
154+ return null ;
122155 }
123156
124157}
0 commit comments