|
1 |
| - |
2 |
| - |
3 | 1 | import { CancellationToken, TextDocument, Position, Hover } from "vscode";
|
4 |
| -import * as fs from 'fs'; |
5 |
| -import * as vscode from 'vscode'; |
6 |
| -import { isPositionInString, intrinsics, FORTRAN_KEYWORDS } from "../lib/helper"; |
| 2 | +import * as fs from "fs"; |
| 3 | +import * as vscode from "vscode"; |
| 4 | +import { |
| 5 | + isPositionInString, |
| 6 | + intrinsics, |
| 7 | + FORTRAN_KEYWORDS |
| 8 | +} from "../lib/helper"; |
7 | 9 | import { getDeclaredFunctions } from "../lib/functions";
|
8 | 10 |
|
| 11 | +export class FortranCompletionProvider |
| 12 | + implements vscode.CompletionItemProvider { |
| 13 | + public provideCompletionItems( |
| 14 | + document: vscode.TextDocument, |
| 15 | + position: vscode.Position, |
| 16 | + token: vscode.CancellationToken |
| 17 | + ): Thenable<vscode.CompletionItem[]> { |
| 18 | + return this.provideCompletionItemsInternal( |
| 19 | + document, |
| 20 | + position, |
| 21 | + token, |
| 22 | + vscode.workspace.getConfiguration("go") |
| 23 | + ); |
| 24 | + } |
| 25 | + public provideCompletionItemsInternal( |
| 26 | + document: vscode.TextDocument, |
| 27 | + position: vscode.Position, |
| 28 | + token: vscode.CancellationToken, |
| 29 | + config: vscode.WorkspaceConfiguration |
| 30 | + ): Thenable<vscode.CompletionItem[]> { |
| 31 | + return new Promise<vscode.CompletionItem[]>((resolve, reject) => { |
| 32 | + let filename = document.fileName; |
| 33 | + let lineText = document.lineAt(position.line).text; |
| 34 | + let lineTillCurrentPosition = lineText.substr(0, position.character); |
| 35 | + // nothing to complete |
| 36 | + if (lineText.match(/^\s*\/\//)) { |
| 37 | + return resolve([]); |
| 38 | + } |
9 | 39 |
|
10 |
| -export class FortranCompletionProvider implements vscode.CompletionItemProvider { |
11 |
| - |
12 |
| - public provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): Thenable<vscode.CompletionItem[]> { |
13 |
| - return this.provideCompletionItemsInternal(document, position, token, vscode.workspace.getConfiguration('go')); |
14 |
| - } |
15 |
| - public provideCompletionItemsInternal(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken, config: vscode.WorkspaceConfiguration): Thenable<vscode.CompletionItem[]> { |
16 |
| - return new Promise<vscode.CompletionItem[]>((resolve, reject) => { |
17 |
| - let filename = document.fileName; |
18 |
| - let lineText = document.lineAt(position.line).text; |
19 |
| - let lineTillCurrentPosition = lineText.substr(0, position.character); |
20 |
| - // nothing to complete |
21 |
| - if (lineText.match(/^\s*\/\//)) { |
22 |
| - return resolve([]); |
23 |
| - } |
24 |
| - |
25 |
| - let inString = isPositionInString(document, position); |
26 |
| - if (!inString && lineTillCurrentPosition.endsWith('\"')) { // completing a string |
27 |
| - return resolve([]); |
28 |
| - } |
29 |
| - |
30 |
| - // get current word |
31 |
| - let wordAtPosition = document.getWordRangeAtPosition(position); |
32 |
| - let currentWord = ''; |
33 |
| - if (wordAtPosition && wordAtPosition.start.character < position.character) { |
34 |
| - let word = document.getText(wordAtPosition); |
35 |
| - currentWord = word.substr(0, position.character - wordAtPosition.start.character); |
36 |
| - } |
37 |
| - |
38 |
| - if (currentWord.match(/^\d+$/)) { // starts with a number |
39 |
| - return resolve([]); |
40 |
| - } |
| 40 | + let inString = isPositionInString(document, position); |
| 41 | + if (!inString && lineTillCurrentPosition.endsWith('"')) { |
| 42 | + // completing a string |
| 43 | + return resolve([]); |
| 44 | + } |
41 | 45 |
|
42 |
| - let suggestions = []; |
| 46 | + // get current word |
| 47 | + let wordAtPosition = document.getWordRangeAtPosition(position); |
| 48 | + let currentWord = ""; |
| 49 | + if ( |
| 50 | + wordAtPosition && |
| 51 | + wordAtPosition.start.character < position.character |
| 52 | + ) { |
| 53 | + let word = document.getText(wordAtPosition); |
| 54 | + currentWord = word.substr( |
| 55 | + 0, |
| 56 | + position.character - wordAtPosition.start.character |
| 57 | + ); |
| 58 | + } |
43 | 59 |
|
44 |
| - if (currentWord.length > 0) { |
45 |
| - intrinsics.forEach(intrinsic => { |
46 |
| - if (intrinsic.startsWith(currentWord.toUpperCase())) { |
47 |
| - suggestions.push(new vscode.CompletionItem(intrinsic, vscode.CompletionItemKind.Method)); |
48 |
| - } |
49 |
| - }); |
| 60 | + if (currentWord.match(/^\d+$/)) { |
| 61 | + // starts with a number |
| 62 | + return resolve([]); |
| 63 | + } |
50 | 64 |
|
51 |
| - // add keyword suggestions |
52 |
| - FORTRAN_KEYWORDS.forEach(keyword => { |
53 |
| - if (keyword.startsWith(currentWord.toUpperCase())) { |
54 |
| - suggestions.push(new vscode.CompletionItem(keyword.toLowerCase(), vscode.CompletionItemKind.Keyword)); |
55 |
| - } |
56 |
| - }); |
57 |
| - } |
58 |
| - const functions = getDeclaredFunctions(document); |
59 |
| - // check for available functions |
60 |
| - functions.filter(fun => fun.name.startsWith(currentWord)) |
61 |
| - .forEach(fun =>{ |
62 |
| - suggestions.push(new vscode.CompletionItem(fun.name, vscode.CompletionItemKind.Function)); |
63 |
| - }); |
64 |
| - |
| 65 | + let suggestions = []; |
65 | 66 |
|
66 |
| - return resolve(suggestions); |
| 67 | + if (currentWord.length > 0) { |
| 68 | + intrinsics.forEach(intrinsic => { |
| 69 | + if (intrinsic.startsWith(currentWord.toUpperCase())) { |
| 70 | + suggestions.push( |
| 71 | + new vscode.CompletionItem( |
| 72 | + intrinsic, |
| 73 | + vscode.CompletionItemKind.Method |
| 74 | + ) |
| 75 | + ); |
| 76 | + } |
| 77 | + }); |
67 | 78 |
|
68 |
| - }) |
| 79 | + // add keyword suggestions |
| 80 | + FORTRAN_KEYWORDS.forEach(keyword => { |
| 81 | + if (keyword.startsWith(currentWord.toUpperCase())) { |
| 82 | + suggestions.push( |
| 83 | + new vscode.CompletionItem( |
| 84 | + keyword.toLowerCase(), |
| 85 | + vscode.CompletionItemKind.Keyword |
| 86 | + ) |
| 87 | + ); |
| 88 | + } |
| 89 | + }); |
| 90 | + } |
| 91 | + const functions = getDeclaredFunctions(document); |
| 92 | + // check for available functions |
| 93 | + functions.filter(fun => fun.name.startsWith(currentWord)).forEach(fun => { |
| 94 | + suggestions.push( |
| 95 | + new vscode.CompletionItem( |
| 96 | + fun.name, |
| 97 | + vscode.CompletionItemKind.Function |
| 98 | + ) |
| 99 | + ); |
| 100 | + }); |
69 | 101 |
|
70 |
| - } |
| 102 | + return resolve(suggestions); |
| 103 | + }); |
| 104 | + } |
71 | 105 | }
|
0 commit comments