Skip to content

Commit d76eec9

Browse files
committed
add completion provider
1 parent 6bbbdb1 commit d76eec9

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

src/features/completion-provider.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
}

src/lib/helper.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export const intrinsics = [
1919
"SYSTEM_CLOCK", "TAN", "TANH", "THIS_IMAGE", "TIME", "TIME8", "TINY", "TRAILZ", "TRANSFER", "TRANSPOSE", "TRIM", "TTYNAM", "UBOUND", "UCOBOUND", "UMASK", "UNLINK", "UNPACK", "VERIFY", "XOR"];
2020

2121

22+
export const FORTRAN_KEYWORDS = ["FUNCTION", "MODULE", "SUBROUTINE", "CONTAINS", "USE","KIND", "DO", "IF", "ELIF","END", "IMPLICIT"];
23+
2224
export const isIntrinsic = (keyword) => {
2325
return intrinsics.findIndex(intrinsic => intrinsic === keyword.toUpperCase()) !== -1;
2426
}
@@ -80,3 +82,15 @@ export const getIncludeParams = (paths: string[]) => {
8082
}
8183

8284

85+
86+
export function isPositionInString(document: vscode.TextDocument, position: vscode.Position): boolean {
87+
let lineText = document.lineAt(position.line).text;
88+
let lineTillCurrentPosition = lineText.substr(0, position.character);
89+
90+
// Count the number of double quotes in the line till current position. Ignore escaped double quotes
91+
let doubleQuotesCnt = (lineTillCurrentPosition.match(/\"/g) || []).length;
92+
let escapedDoubleQuotesCnt = (lineTillCurrentPosition.match(/\\\"/g) || []).length;
93+
94+
doubleQuotesCnt -= escapedDoubleQuotesCnt;
95+
return doubleQuotesCnt % 2 === 1;
96+
}

0 commit comments

Comments
 (0)