|
| 1 | + |
| 2 | + |
| 3 | +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"; |
| 7 | + |
| 8 | + |
| 9 | +export class FortranCompletionProvider implements vscode.CompletionItemProvider { |
| 10 | + |
| 11 | + public provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): Thenable<vscode.CompletionItem[]> { |
| 12 | + return this.provideCompletionItemsInternal(document, position, token, vscode.workspace.getConfiguration('go')); |
| 13 | + } |
| 14 | + public provideCompletionItemsInternal(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken, config: vscode.WorkspaceConfiguration): Thenable<vscode.CompletionItem[]> { |
| 15 | + return new Promise<vscode.CompletionItem[]>((resolve, reject) => { |
| 16 | + let filename = document.fileName; |
| 17 | + let lineText = document.lineAt(position.line).text; |
| 18 | + let lineTillCurrentPosition = lineText.substr(0, position.character); |
| 19 | + // nothing to complete |
| 20 | + if (lineText.match(/^\s*\/\//)) { |
| 21 | + return resolve([]); |
| 22 | + } |
| 23 | + |
| 24 | + let inString = isPositionInString(document, position); |
| 25 | + if (!inString && lineTillCurrentPosition.endsWith('\"')) { // completing a string |
| 26 | + return resolve([]); |
| 27 | + } |
| 28 | + |
| 29 | + // get current word |
| 30 | + let wordAtPosition = document.getWordRangeAtPosition(position); |
| 31 | + let currentWord = ''; |
| 32 | + if (wordAtPosition && wordAtPosition.start.character < position.character) { |
| 33 | + let word = document.getText(wordAtPosition); |
| 34 | + currentWord = word.substr(0, position.character - wordAtPosition.start.character); |
| 35 | + } |
| 36 | + |
| 37 | + if (currentWord.match(/^\d+$/)) { // starts with a number |
| 38 | + return resolve([]); |
| 39 | + } |
| 40 | + |
| 41 | + let suggestions = []; |
| 42 | + |
| 43 | + if (currentWord.length > 0) { |
| 44 | + intrinsics.forEach(intrinsic => { |
| 45 | + if (intrinsic.startsWith(currentWord.toUpperCase())) { |
| 46 | + suggestions.push(new vscode.CompletionItem(intrinsic, vscode.CompletionItemKind.Method)); |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + // add keyword suggestions |
| 51 | + FORTRAN_KEYWORDS.forEach(keyword => { |
| 52 | + if (keyword.startsWith(currentWord.toUpperCase())) { |
| 53 | + suggestions.push(new vscode.CompletionItem(keyword.toLowerCase(), vscode.CompletionItemKind.Keyword)); |
| 54 | + } |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + return resolve(suggestions); |
| 59 | + |
| 60 | + }) |
| 61 | + |
| 62 | + } |
| 63 | +} |
0 commit comments