1
- import { CancellationToken , TextDocument , Position , Hover } from 'vscode' ;
2
- import * as fs from 'fs' ;
3
- import * as vscode from 'vscode' ;
4
- import { isPositionInString , intrinsics , FORTRAN_KEYWORDS } from '../lib/helper' ;
5
- import { getDeclaredFunctions } from '../lib/functions' ;
1
+ import { CancellationToken , TextDocument , Position , Hover } from 'vscode'
2
+ import * as fs from 'fs'
3
+ import * as vscode from 'vscode'
4
+ import { isPositionInString , intrinsics , FORTRAN_KEYWORDS } from '../lib/helper'
5
+ import { getDeclaredFunctions } from '../lib/functions'
6
6
7
- import { EXTENSION_ID } from '../lib/helper' ;
7
+ import { EXTENSION_ID } from '../lib/helper'
8
+ import { LoggingService } from '../services/logging-service'
8
9
9
10
class CaseCoverter {
10
- preferredCase : string ;
11
- static LOWER = 'lowercase' ;
12
- static UPPER = 'uppercase' ;
11
+ preferredCase : string
12
+ static LOWER = 'lowercase'
13
+ static UPPER = 'uppercase'
13
14
14
15
constructor ( preferredCase : string = CaseCoverter . LOWER ) {
15
- this . preferredCase = preferredCase ;
16
+ this . preferredCase = preferredCase
16
17
}
17
18
18
19
convert ( word : string ) : string {
19
20
if ( this . preferredCase === CaseCoverter . LOWER ) {
20
- return this . toLower ( word ) ;
21
+ return this . toLower ( word )
21
22
} else if ( this . preferredCase === CaseCoverter . UPPER ) {
22
- return this . toUpper ( word ) ;
23
+ return this . toUpper ( word )
23
24
}
24
25
25
- throw new Error ( `the provided case ${ this . preferredCase } is not supported` ) ;
26
+ throw new Error ( `the provided case ${ this . preferredCase } is not supported` )
26
27
}
27
28
28
29
toLower ( word : string ) {
29
- return word . toLowerCase ( ) ;
30
+ return word . toLowerCase ( )
30
31
}
31
32
toUpper ( word : string ) {
32
- return word . toUpperCase ( ) ;
33
+ return word . toUpperCase ( )
33
34
}
34
35
}
35
36
36
37
export class FortranCompletionProvider
37
- implements vscode . CompletionItemProvider {
38
+ implements vscode . CompletionItemProvider
39
+ {
40
+ constructor ( private loggingService : LoggingService ) { }
38
41
public provideCompletionItems (
39
42
document : vscode . TextDocument ,
40
43
position : vscode . Position ,
@@ -45,7 +48,7 @@ export class FortranCompletionProvider
45
48
position ,
46
49
token ,
47
50
vscode . workspace . getConfiguration ( EXTENSION_ID )
48
- ) ;
51
+ )
49
52
}
50
53
51
54
public provideCompletionItemsInternal (
@@ -55,105 +58,104 @@ export class FortranCompletionProvider
55
58
config : vscode . WorkspaceConfiguration
56
59
) : Thenable < vscode . CompletionItem [ ] > {
57
60
return new Promise < vscode . CompletionItem [ ] > ( ( resolve , reject ) => {
58
- let lineText = document . lineAt ( position . line ) . text ;
59
- let lineTillCurrentPosition = lineText . substr ( 0 , position . character ) ;
61
+ let lineText = document . lineAt ( position . line ) . text
62
+ let lineTillCurrentPosition = lineText . substr ( 0 , position . character )
60
63
// nothing to complete
61
64
if ( lineText . match ( / ^ \s * \/ \/ / ) ) {
62
- return resolve ( [ ] ) ;
65
+ return resolve ( [ ] )
63
66
}
64
67
65
- let inString = isPositionInString ( document , position ) ;
68
+ let inString = isPositionInString ( document , position )
66
69
if ( ! inString && lineTillCurrentPosition . endsWith ( '"' ) ) {
67
70
// completing a string
68
- return resolve ( [ ] ) ;
71
+ return resolve ( [ ] )
69
72
}
70
73
71
- let currentWord = this . getCurrentWord ( document , position ) ;
74
+ let currentWord = this . getCurrentWord ( document , position )
72
75
73
76
if ( currentWord . match ( / ^ \d + $ / ) ) {
74
77
// starts with a number
75
- return resolve ( [ ] ) ;
78
+ return resolve ( [ ] )
76
79
}
77
80
78
- const caseConverter = new CaseCoverter ( config . get ( 'preferredCase' ) ) ;
81
+ const caseConverter = new CaseCoverter ( config . get ( 'preferredCase' ) )
79
82
80
83
if ( currentWord . length === 0 ) {
81
- resolve ( [ ] ) ;
84
+ resolve ( [ ] )
82
85
}
83
86
84
87
const intrinsicSuggestions = this . getIntrinsicSuggestions (
85
88
currentWord ,
86
89
caseConverter
87
- ) ;
90
+ )
88
91
89
92
// add keyword suggestions
90
- const keywordSuggestions = this . getKeywordSuggestions ( currentWord ) ;
93
+ const keywordSuggestions = this . getKeywordSuggestions ( currentWord )
91
94
92
95
const functionSuggestions = this . getFunctionSuggestions (
93
96
document ,
94
97
currentWord
95
- ) ;
98
+ )
96
99
97
100
return resolve ( [
98
101
...intrinsicSuggestions ,
99
102
...keywordSuggestions ,
100
103
...functionSuggestions ,
101
- ] ) ;
102
- } ) ;
104
+ ] )
105
+ } )
103
106
}
104
107
105
108
private getIntrinsicSuggestions (
106
109
currentWord : string ,
107
110
caseConverter : CaseCoverter
108
111
) : vscode . CompletionItem [ ] {
109
112
return intrinsics
110
- . filter ( i => i . startsWith ( currentWord . toUpperCase ( ) ) )
113
+ . filter ( ( i ) => i . startsWith ( currentWord . toUpperCase ( ) ) )
111
114
. map ( ( intrinsic : string ) => {
112
115
return new vscode . CompletionItem (
113
116
caseConverter . convert ( intrinsic ) ,
114
117
vscode . CompletionItemKind . Method
115
- ) ;
116
- } ) ;
118
+ )
119
+ } )
117
120
}
118
121
119
122
private getKeywordSuggestions ( currentWord : string ) : vscode . CompletionItem [ ] {
120
- return FORTRAN_KEYWORDS . filter ( keyword =>
123
+ return FORTRAN_KEYWORDS . filter ( ( keyword ) =>
121
124
keyword . startsWith ( currentWord . toUpperCase ( ) )
122
- ) . map ( keyword => {
125
+ ) . map ( ( keyword ) => {
123
126
return new vscode . CompletionItem (
124
127
keyword . toLowerCase ( ) ,
125
128
vscode . CompletionItemKind . Keyword
126
- ) ;
127
- } ) ;
129
+ )
130
+ } )
128
131
}
129
132
130
133
private getFunctionSuggestions (
131
134
document : TextDocument ,
132
135
currentWord : string
133
136
) : vscode . CompletionItem [ ] {
134
- const functions = getDeclaredFunctions ( document ) ;
137
+ const functions = getDeclaredFunctions ( document )
135
138
// check for available functions
136
139
return functions
137
- . filter ( fun => fun . name . startsWith ( currentWord ) )
138
- . map ( fun => {
140
+ . filter ( ( fun ) => fun . name . startsWith ( currentWord ) )
141
+ . map ( ( fun ) => {
139
142
return new vscode . CompletionItem (
140
143
`${ fun . name } (` ,
141
144
vscode . CompletionItemKind . Function
142
- ) ;
143
- } ) ;
145
+ )
146
+ } )
144
147
}
145
148
146
149
private getCurrentWord ( document : TextDocument , position : Position ) : string {
147
- let wordAtPosition = document . getWordRangeAtPosition ( position ) ;
148
- let currentWord = '' ;
150
+ let wordAtPosition = document . getWordRangeAtPosition ( position )
151
+ let currentWord = ''
149
152
if ( wordAtPosition && wordAtPosition . start . character < position . character ) {
150
- let word = document . getText ( wordAtPosition ) ;
153
+ let word = document . getText ( wordAtPosition )
151
154
currentWord = word . substr (
152
155
0 ,
153
156
position . character - wordAtPosition . start . character
154
- ) ;
157
+ )
155
158
}
156
- return currentWord ;
157
-
159
+ return currentWord
158
160
}
159
161
}
0 commit comments