Skip to content

Commit 389a6e2

Browse files
committed
Updates install tools.
We now prompt the user to install fortran-language-server and Fortran IntelliSense if the packages are missing. There is a slight issue with the calls being asynchronous, so if they are installed in the wrong order Fortran IntelliSense first and then fortls an error is being throw.
1 parent 652b7eb commit 389a6e2

File tree

3 files changed

+79
-30
lines changed

3 files changed

+79
-30
lines changed

src/extension.ts

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

10-
import { FORTRAN_FREE_FORM_ID, EXTENSION_ID } from './lib/helper'
11-
import { FortranLangServer, checkForLangServer } from './lang-server'
10+
import { FortranLangServer } from './lang-server'
11+
import { FORTRAN_FREE_FORM_ID, EXTENSION_ID, promptForMissingTool } from './lib/helper'
1212
import { LoggingService } from './services/logging-service'
1313
import * as pkg from '../package.json'
14+
import { LANG_SERVER_TOOL_ID } from './lib/tools'
1415

1516
export function activate(context: vscode.ExtensionContext) {
1617
const loggingService = new LoggingService()
@@ -54,6 +55,25 @@ export function activate(context: vscode.ExtensionContext) {
5455
loggingService.logInfo('Symbol Provider is not enabled')
5556
}
5657

58+
// Check if the language server is installed and if not prompt to install it
59+
if (!which.sync('fortls', { nothrow: true })) {
60+
let msg = `It is highly recommended to use the fortran-language-server to
61+
enable hover, peeking, gotos and many more.
62+
For a full list of features the language server adds see:
63+
https://github.com/hansec/fortran-language-server`;
64+
promptForMissingTool(LANG_SERVER_TOOL_ID, msg, 'Python', loggingService);
65+
}
66+
67+
// Check that Fortran Intellisense is installed and if not prompt to install
68+
if (!vscode.extensions.getExtension('hansec.fortran-ls')) {
69+
let msg = `It is highly recommended to install the Fortran IntelliSense
70+
extension. The extension is used to interface with the
71+
fortran-language-server.
72+
For a full list of features provided by the extension see:
73+
https://github.com/hansec/vscode-fortran-ls`;
74+
promptForMissingTool('hansec.fortran-ls', msg, 'VSExt', loggingService);
75+
}
76+
5777
// Our interface with `fortls` has been disabled in favour of the @hansec's
5878
// VS Code extension Fortran IntelliSense
5979
const useInternalFLInterface = false;

src/lib/helper.ts

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as fs from 'fs';
22
import * as vscode from 'vscode';
3+
import { installPythonTool } from './tools';
34
import intrinsics from './fortran-intrinsics';
4-
import { installTool, LANG_SERVER_TOOL_ID } from './tools';
5+
import { LoggingService } from '../services/logging-service';
56

67
// IMPORTANT: this should match the value
78
// on the package.json otherwise the extension won't
@@ -109,24 +110,50 @@ export function isPositionInString(
109110
let saveKeywordToJson = keyword => {
110111
let doc = _loadDocString(keyword);
111112
let docObject = JSON.stringify({ keyword: keyword, docstr: doc });
112-
fs.appendFile('src/docs/' + keyword + '.json', docObject, function(err) {
113+
fs.appendFile('src/docs/' + keyword + '.json', docObject, function (err) {
113114
if (err) throw err;
114115
console.log('Saved!');
115116
});
116117
};
117118

118119

119-
export function promptForMissingTool(tool: string) {
120+
/**
121+
* Install a package either a Python pip package or a VS Marketplace Extension.
122+
*
123+
* For the Python install supply the name of the package in PyPi
124+
* e.g. fortran-language-server
125+
*
126+
* For the VS Extension to be installed supply the id of the extension
127+
* e.g 'hansec.fortran-ls'
128+
*
129+
* @param tool name of the tool e.g. fortran-language-server
130+
* @param msg optional message for installing said package
131+
* @param toolType type of tool, supports `Python` (through pip) and 'VSExt'
132+
*/
133+
export function promptForMissingTool(tool: string, msg: string, toolType: string, logger?: LoggingService) {
120134
const items = ['Install'];
121-
let message = '';
122-
if (tool === 'fortran-langserver') {
123-
message =
124-
'You choose to use the fortranLanguageServer functionality but it is not installed. Please press the Install button to install it';
125-
}
126-
vscode.window.showInformationMessage(message, ...items).then(selected => {
127-
if (selected === 'Install') {
128-
installTool(tool);
129-
}
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('workbench.extensions.installExtension', tool);
147+
logger.logInfo(`Extension ${tool} successfully installed`);
148+
break;
149+
150+
default:
151+
logger.logError(`Failed to install tool: ${tool}`);
152+
break;
153+
}
154+
}
155+
})
156+
);
130157
});
131158

132159
}

src/lib/tools.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
export const LANG_SERVER_TOOL_ID = 'fortran-langserver';
1+
export const LANG_SERVER_TOOL_ID = 'fortran-language-server';
2+
export const FORMATTERS = ['Disabled', 'findent', 'fprettify'];
3+
24
import * as cp from 'child_process';
5+
import { LoggingService } from '../services/logging-service';
36

7+
export function installPythonTool(pyPackage: string, logger?: LoggingService) {
48

5-
export function installTool(toolname) {
6-
if (toolname === LANG_SERVER_TOOL_ID) {
7-
const installProcess = cp.spawn(
8-
'pip',
9-
'install --user --upgrade fortran-language-server'.split(' ')
10-
);
11-
installProcess.on('exit', (code, signal) => {
12-
if (code !== 0) {
13-
// extension failed to install
14-
}
15-
});
16-
installProcess.on('error', err => {
17-
// failed to install
18-
});
19-
}
9+
const installProcess = cp.spawn(
10+
'pip',
11+
'install --user --upgrade '.concat(pyPackage).split(' ')
12+
);
13+
installProcess.stdout.on('data', (data) => { logger.logInfo(`pip install: ${data}`) });
14+
installProcess.on('exit', (code, signal) => {
15+
if (code !== 0) {
16+
logger.logError(`Python package ${pyPackage} failed to install with code: ${code}, signal: ${signal}`);
17+
}
18+
});
19+
installProcess.on('error', err => {
20+
logger.logError(`${err}`);
21+
});
2022
}

0 commit comments

Comments
 (0)