Skip to content

Commit 4a1664c

Browse files
committed
Rearranges routines from helper to tools
The helper.ts file was cluttered, filled with routines and variables that belonged to the tools file. This have now been moved. In addition, the old interface with the language server has been removed in preparation for the new interface that will be added.
1 parent 4ed05e2 commit 4a1664c

File tree

7 files changed

+99
-139
lines changed

7 files changed

+99
-139
lines changed

src/extension.ts

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import FortranHoverProvider from './features/hover-provider';
77
import { FortranCompletionProvider } from './features/completion-provider';
88
import { FortranDocumentSymbolProvider } from './features/document-symbol-provider';
99

10-
import { FortranLangServer } from './lang-server';
11-
import { FORTRAN_DOCUMENT_SELECTOR, EXTENSION_ID, promptForMissingTool } from './lib/helper';
1210
import { LoggingService } from './services/logging-service';
1311
import * as pkg from '../package.json';
1412
import { LANG_SERVER_TOOL_ID } from './lib/tools';
1513
import { FortranFormattingProvider } from './features/formatting-provider';
14+
import FortranLanguageServer from './fortls-interface';
15+
import { EXTENSION_ID, FortranDocumentSelector, promptForMissingTool } from './lib/tools';
1616

1717
// Make it global to catch errors when activation fails
1818
const loggingService = new LoggingService();
@@ -26,15 +26,15 @@ export function activate(context: vscode.ExtensionContext) {
2626
if (extensionConfig.get('linterEnabled', true)) {
2727
const linter = new FortranLintingProvider(loggingService);
2828
linter.activate(context.subscriptions);
29-
vscode.languages.registerCodeActionsProvider(FORTRAN_DOCUMENT_SELECTOR, linter);
29+
vscode.languages.registerCodeActionsProvider(FortranDocumentSelector(), linter);
3030
loggingService.logInfo('Linter is enabled');
3131
} else {
3232
loggingService.logInfo('Linter is not enabled');
3333
}
3434

3535
if (extensionConfig.get('formatter') !== 'Disabled') {
3636
const disposable: vscode.Disposable = vscode.languages.registerDocumentFormattingEditProvider(
37-
FORTRAN_DOCUMENT_SELECTOR,
37+
FortranDocumentSelector(),
3838
new FortranFormattingProvider(loggingService)
3939
);
4040
context.subscriptions.push(disposable);
@@ -45,22 +45,22 @@ export function activate(context: vscode.ExtensionContext) {
4545

4646
if (extensionConfig.get('provideCompletion', true)) {
4747
const completionProvider = new FortranCompletionProvider(loggingService);
48-
vscode.languages.registerCompletionItemProvider(FORTRAN_DOCUMENT_SELECTOR, completionProvider);
48+
vscode.languages.registerCompletionItemProvider(FortranDocumentSelector(), completionProvider);
4949
} else {
5050
loggingService.logInfo('Completion Provider is not enabled');
5151
}
5252

5353
if (extensionConfig.get('provideHover', true)) {
5454
const hoverProvider = new FortranHoverProvider(loggingService);
55-
vscode.languages.registerHoverProvider(FORTRAN_DOCUMENT_SELECTOR, hoverProvider);
55+
vscode.languages.registerHoverProvider(FortranDocumentSelector(), hoverProvider);
5656
loggingService.logInfo('Hover Provider is enabled');
5757
} else {
5858
loggingService.logInfo('Hover Provider is not enabled');
5959
}
6060

6161
if (extensionConfig.get('provideSymbols', true)) {
6262
const symbolProvider = new FortranDocumentSymbolProvider();
63-
vscode.languages.registerDocumentSymbolProvider(FORTRAN_DOCUMENT_SELECTOR, symbolProvider);
63+
vscode.languages.registerDocumentSymbolProvider(FortranDocumentSelector(), symbolProvider);
6464
loggingService.logInfo('Symbol Provider is enabled');
6565
} else {
6666
loggingService.logInfo('Symbol Provider is not enabled');
@@ -74,30 +74,4 @@ export function activate(context: vscode.ExtensionContext) {
7474
https://github.com/hansec/fortran-language-server`;
7575
promptForMissingTool(LANG_SERVER_TOOL_ID, msg, 'Python', loggingService);
7676
}
77-
78-
// Check that Fortran Intellisense is installed and if not prompt to install
79-
if (!vscode.extensions.getExtension('hansec.fortran-ls')) {
80-
const msg = `It is highly recommended to install the Fortran IntelliSense
81-
extension. The extension is used to interface with the
82-
fortran-language-server.
83-
For a full list of features provided by the extension see:
84-
https://github.com/hansec/vscode-fortran-ls`;
85-
promptForMissingTool('hansec.fortran-ls', msg, 'VSExt', loggingService);
86-
}
87-
88-
// Our interface with `fortls` has been disabled in favour of the @hansec's
89-
// VS Code extension Fortran IntelliSense
90-
const useInternalFLInterface = false;
91-
if (useInternalFLInterface) {
92-
const langServer = new FortranLangServer(context, extensionConfig);
93-
langServer.start();
94-
langServer.onReady().then(() => {
95-
const capabilities = langServer.getCapabilities();
96-
if (!capabilities) {
97-
return vscode.window.showErrorMessage(
98-
'The language server is not able to serve any features at the moment.'
99-
);
100-
}
101-
});
102-
}
10377
}

src/features/completion-provider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as vscode from 'vscode';
44
import { isPositionInString, intrinsics, FORTRAN_KEYWORDS } from '../lib/helper';
55
import { getDeclaredFunctions } from '../lib/functions';
66

7-
import { EXTENSION_ID } from '../lib/helper';
7+
import { EXTENSION_ID } from '../lib/tools';
88
import { LoggingService } from '../services/logging-service';
99

1010
class CaseCoverter {

src/features/formatting-provider.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ import * as which from 'which';
66
import * as vscode from 'vscode';
77
import * as cp from 'child_process';
88

9-
import { FORMATTERS } from '../lib/tools';
109
import { LoggingService } from '../services/logging-service';
11-
import { EXTENSION_ID, promptForMissingTool } from '../lib/helper';
10+
import { FORMATTERS, EXTENSION_ID, promptForMissingTool } from '../lib/tools';
1211

1312
export class FortranFormattingProvider implements vscode.DocumentFormattingEditProvider {
1413
constructor(private logger: LoggingService) {}

src/features/linter-provider.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
import * as path from 'path';
44
import * as cp from 'child_process';
5-
import { FORTRAN_DOCUMENT_SELECTOR, getIncludeParams } from '../lib/helper';
65

76
import * as vscode from 'vscode';
87
import { LoggingService } from '../services/logging-service';
9-
import { resolveVariables } from '../lib/tools';
8+
import { FortranDocumentSelector, resolveVariables } from '../lib/tools';
109
import * as fg from 'fast-glob';
1110
import { glob } from 'glob';
1211

@@ -21,8 +20,8 @@ export default class FortranLintingProvider {
2120

2221
// Only lint Fortran (free, fixed) format files
2322
if (
24-
!FORTRAN_DOCUMENT_SELECTOR.some(e => e.scheme === textDocument.uri.scheme) ||
25-
!FORTRAN_DOCUMENT_SELECTOR.some(e => e.language === textDocument.languageId)
23+
!FortranDocumentSelector().some(e => e.scheme === textDocument.uri.scheme) ||
24+
!FortranDocumentSelector().some(e => e.language === textDocument.languageId)
2625
) {
2726
return;
2827
}
@@ -111,7 +110,7 @@ export default class FortranLintingProvider {
111110
const fileNameWithoutExtension = textDocument.fileName.substring(0, extensionIndex);
112111
const argList = [
113112
...args,
114-
...getIncludeParams(includePaths), // include paths
113+
...this.getIncludeParams(includePaths), // include paths
115114
textDocument.fileName,
116115
`-o ${fileNameWithoutExtension}.mod`,
117116
];
@@ -222,4 +221,8 @@ export default class FortranLintingProvider {
222221
this.loggingService.logInfo(`Linter.arguments:\n${args.join('\r\n')}`);
223222
return args.map(e => resolveVariables(e));
224223
}
224+
225+
private getIncludeParams = (paths: string[]) => {
226+
return paths.map(path => `-I${path}`);
227+
};
225228
}

src/lang-server.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/lib/helper.ts

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,7 @@ import { installPythonTool } from './tools';
44
import intrinsics from './fortran-intrinsics';
55
import { LoggingService } from '../services/logging-service';
66

7-
// IMPORTANT: this should match the value
8-
// on the package.json otherwise the extension won't
9-
// work at all
10-
export const FORTRAN_DOCUMENT_SELECTOR = [
11-
{ scheme: 'file', language: 'FortranFreeForm' },
12-
{ scheme: 'file', language: 'FortranFixedForm' },
13-
];
147
export { intrinsics };
15-
export const EXTENSION_ID = 'fortran';
168

179
export const FORTRAN_KEYWORDS = [
1810
'FUNCTION',
@@ -84,10 +76,6 @@ export const _loadDocString = (keyword: string) => {
8476
return docText;
8577
};
8678

87-
export const getIncludeParams = (paths: string[]) => {
88-
return paths.map(path => `-I${path}`);
89-
};
90-
9179
export function isPositionInString(
9280
document: vscode.TextDocument,
9381
position: vscode.Position
@@ -112,52 +100,6 @@ const saveKeywordToJson = keyword => {
112100
});
113101
};
114102

115-
/**
116-
* Install a package either a Python pip package or a VS Marketplace Extension.
117-
*
118-
* For the Python install supply the name of the package in PyPi
119-
* e.g. fortran-language-server
120-
*
121-
* For the VS Extension to be installed supply the id of the extension
122-
* e.g 'hansec.fortran-ls'
123-
*
124-
* @param tool name of the tool e.g. fortran-language-server
125-
* @param msg optional message for installing said package
126-
* @param toolType type of tool, supports `Python` (through pip) and 'VSExt'
127-
*/
128-
export function promptForMissingTool(
129-
tool: string,
130-
msg: string,
131-
toolType: string,
132-
logger?: LoggingService
133-
) {
134-
const items = ['Install'];
135-
return new Promise((resolve, reject) => {
136-
resolve(
137-
vscode.window.showInformationMessage(msg, ...items).then(selected => {
138-
if (selected === 'Install') {
139-
switch (toolType) {
140-
case 'Python':
141-
installPythonTool(tool, logger);
142-
break;
143-
144-
case 'VSExt':
145-
logger.logInfo(`Installing VS Marketplace Extension with id: ${tool}`);
146-
vscode.commands.executeCommand('extension.open', tool);
147-
vscode.commands.executeCommand('workbench.extensions.installExtension', tool);
148-
logger.logInfo(`Extension ${tool} successfully installed`);
149-
break;
150-
151-
default:
152-
logger.logError(`Failed to install tool: ${tool}`);
153-
break;
154-
}
155-
}
156-
})
157-
);
158-
});
159-
}
160-
161103
export function isUri(input: any): input is vscode.Uri {
162104
return input && input instanceof vscode.Uri;
163105
}

src/lib/tools.ts

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,95 @@
1-
export const LANG_SERVER_TOOL_ID = 'fortran-language-server';
2-
export const FORMATTERS = ['Disabled', 'findent', 'fprettify'];
3-
41
import * as os from 'os';
52
import * as vscode from 'vscode';
63
import * as assert from 'assert';
74
import * as cp from 'child_process';
85
import { LoggingService } from '../services/logging-service';
96
import { isString, isArrayOfString } from './helper';
107

8+
export const EXTENSION_ID = 'fortran';
9+
export const LANG_SERVER_TOOL_ID = 'fortran-language-server';
10+
export const FORMATTERS = ['Disabled', 'findent', 'fprettify'];
11+
1112
// Platform-specific environment variable delimiter
1213
export const envDelimiter: string = process.platform === 'win32' ? ';' : ':';
1314

15+
/**
16+
* Generates a document selector for the supported file and languages
17+
* if the folder is present then a glob pattern for all the files in the workspace
18+
* is included
19+
*
20+
* @warning this should match the value on the package.json otherwise the extension
21+
* won't work at all
22+
*
23+
* @param folder `optional` WorkspaceFolder to search
24+
* @returns tuple of DocumentSelector
25+
*/
26+
export function FortranDocumentSelector(folder?: vscode.WorkspaceFolder) {
27+
if (folder) {
28+
return [
29+
{ scheme: 'file', language: 'FortranFreeForm', pattern: `${folder.uri.fsPath}/**/*` },
30+
{ scheme: 'file', language: 'FortranFixedForm', pattern: `${folder.uri.fsPath}/**/*` },
31+
];
32+
} else {
33+
return [
34+
{ scheme: 'file', language: 'FortranFreeForm' },
35+
{ scheme: 'file', language: 'FortranFixedForm' },
36+
];
37+
}
38+
}
39+
40+
/**
41+
* Install a package either a Python pip package or a VS Marketplace Extension.
42+
*
43+
* For the Python install supply the name of the package in PyPi
44+
* e.g. fortran-language-server
45+
*
46+
* For the VS Extension to be installed supply the id of the extension
47+
* e.g 'hansec.fortran-ls'
48+
*
49+
* @param tool name of the tool e.g. fortran-language-server
50+
* @param msg optional message for installing said package
51+
* @param toolType type of tool, supports `Python` (through pip) and 'VSExt'
52+
*/
53+
export function promptForMissingTool(
54+
tool: string,
55+
msg: string,
56+
toolType: string,
57+
logger?: LoggingService
58+
) {
59+
const items = ['Install'];
60+
return new Promise((resolve, reject) => {
61+
resolve(
62+
vscode.window.showInformationMessage(msg, ...items).then(selected => {
63+
if (selected === 'Install') {
64+
switch (toolType) {
65+
case 'Python':
66+
installPythonTool(tool, logger);
67+
break;
68+
69+
case 'VSExt':
70+
logger.logInfo(`Installing VS Marketplace Extension with id: ${tool}`);
71+
vscode.commands.executeCommand('extension.open', tool);
72+
vscode.commands.executeCommand('workbench.extensions.installExtension', tool);
73+
logger.logInfo(`Extension ${tool} successfully installed`);
74+
break;
75+
76+
default:
77+
logger.logError(`Failed to install tool: ${tool}`);
78+
break;
79+
}
80+
}
81+
})
82+
);
83+
});
84+
}
85+
86+
/**
87+
* A wrapper around a call to `pip` for installing external tools.
88+
* Does not explicitly check if `pip` is installed.
89+
*
90+
* @param pyPackage name of python package in PyPi
91+
* @param logger `optional` logging channel for output
92+
*/
1493
export function installPythonTool(pyPackage: string, logger?: LoggingService) {
1594
const installProcess = cp.spawn('pip', 'install --user --upgrade '.concat(pyPackage).split(' '));
1695
installProcess.stdout.on('data', data => {

0 commit comments

Comments
 (0)