Skip to content

Commit 46eccf3

Browse files
authored
Merge pull request #63 from dylon/dylon/drying-up-resolve
DRYs up the resolution logic a bit
2 parents 93de79b + 5b06e97 commit 46eccf3

File tree

2 files changed

+27
-33
lines changed

2 files changed

+27
-33
lines changed

server/src/lfortran-accessors.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,19 @@ import {
3333

3434
import shellescape from 'shell-escape';
3535

36+
const RE_FILE_URI: RegExp = /^file:(?:\/\/)?/;
37+
3638
/**
3739
* Accessor interface for interacting with LFortran. Possible implementations
3840
* include a CLI accessor and service accessor.
3941
*/
4042
export interface LFortranAccessor {
4143

44+
resolve(uri: string,
45+
filename: string,
46+
flags: string[],
47+
resolved?: Map<string, string>): string;
48+
4249
version(settings: LFortranSettings): Promise<string>;
4350

4451
/**
@@ -299,12 +306,21 @@ export class LFortranCLIAccessor implements LFortranAccessor {
299306
return output;
300307
}
301308

302-
resolve(filename: string, flags: string[], resolved?: Map<string, string>): string {
309+
resolve(uri: string,
310+
filename: string,
311+
flags: string[],
312+
resolved?: Map<string, string>): string {
303313
const fnid: string = "resolve";
304314
const start: number = performance.now();
305315

306316
let filePath: string = filename;
307317

318+
if (filePath === this.tmpFile.name) {
319+
filePath = uri;
320+
}
321+
322+
filePath = filePath.replace(RE_FILE_URI, "");
323+
308324
if (!fs.existsSync(filePath)) {
309325
let resolution: string | undefined = resolved?.get(filePath);
310326
if (resolution === undefined) {
@@ -331,8 +347,12 @@ export class LFortranCLIAccessor implements LFortranAccessor {
331347

332348
// if file name is `b.f90` then it will be replaced with `$(pwd)/b.f90`
333349
// if file name is `a/b.f90` then it will be replaced with `$(pwd)/a/b.f90`
334-
350+
// -----------------------------------------------------------------------
351+
// TODO: Collect an example that demonstrates the need for this resolution
352+
// that does not work with `fs.realpathSync`, above.
353+
// -----------------------------------------------------------------------
335354
const newFilePath: string = path.resolve(filePath);
355+
336356
if (this.logger.isBenchmarkOrTraceEnabled()) {
337357
this.logBenchmarkAndTrace(
338358
fnid, start,
@@ -374,7 +394,7 @@ export class LFortranCLIAccessor implements LFortranAccessor {
374394
for (let i = 0, k = symbols.length; i < k; i++) {
375395
const symbol: Record<string, any> = symbols[i];
376396
const symbolPath: string =
377-
this.resolve(symbol.filename, settings.compiler.flags, resolved);
397+
this.resolve(uri, symbol.filename, settings.compiler.flags, resolved);
378398

379399
const location: Location = symbol.location;
380400
// location.uri = uri;
@@ -428,8 +448,8 @@ export class LFortranCLIAccessor implements LFortranAccessor {
428448
const results: Record<string, any>[] = JSON.parse(stdout);
429449
for (let i = 0, k = results.length; i < k; i++) {
430450
const result: Record<string, any> = results[i];
431-
let symbolPath: string =
432-
this.resolve(result.filename, settings.compiler.flags);
451+
const symbolPath: string =
452+
this.resolve(uri, result.filename, settings.compiler.flags);
433453

434454
const location = result.location;
435455

@@ -443,9 +463,6 @@ export class LFortranCLIAccessor implements LFortranAccessor {
443463
end.line--;
444464
end.character--;
445465

446-
if (symbolPath.endsWith(".tmp")) {
447-
symbolPath = uri;
448-
}
449466
definitions.push({
450467
targetUri: symbolPath,
451468
targetRange: range,

server/src/lfortran-language-server.ts

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
import fs from 'fs';
77

8-
import path from 'path';
9-
108
import {
119
CompletionItem,
1210
CompletionItemKind,
@@ -260,29 +258,8 @@ export class LFortranLanguageServer {
260258
const document = this.documents.get(uri);
261259
let text = document?.getText();
262260
if (text === undefined) {
263-
let filePath: string = uri;
264-
if (filePath.startsWith("file://")) {
265-
filePath = filePath.substring(7);
266-
}
267-
if (!fs.existsSync(filePath)) {
268-
let resolution: string | undefined = resolved?.get(filePath);
269-
if (resolution === undefined) {
270-
for (const flag of this.settings.compiler.flags) {
271-
if (flag.startsWith("-I")) {
272-
const includeDir = flag.substring(2);
273-
resolution = path.join(includeDir, filePath);
274-
if (fs.existsSync(resolution)) {
275-
resolution = fs.realpathSync(resolution);
276-
resolved?.set(filename, resolution);
277-
filePath = resolution;
278-
break;
279-
}
280-
}
281-
}
282-
} else {
283-
filePath = resolution;
284-
}
285-
}
261+
const filePath: string =
262+
this.lfortran.resolve(uri, uri, this.settings.compiler.flags, resolved);
286263
if (fs.existsSync(filePath)) {
287264
let entry = this.fileCache.get(filePath);
288265
const stats = fs.statSync(filePath);

0 commit comments

Comments
 (0)