Skip to content

Commit

Permalink
Merge pull request #63 from dylon/dylon/drying-up-resolve
Browse files Browse the repository at this point in the history
DRYs up the resolution logic a bit
  • Loading branch information
certik authored Dec 29, 2024
2 parents 93de79b + 5b06e97 commit 46eccf3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 33 deletions.
33 changes: 25 additions & 8 deletions server/src/lfortran-accessors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,19 @@ import {

import shellescape from 'shell-escape';

const RE_FILE_URI: RegExp = /^file:(?:\/\/)?/;

/**
* Accessor interface for interacting with LFortran. Possible implementations
* include a CLI accessor and service accessor.
*/
export interface LFortranAccessor {

resolve(uri: string,
filename: string,
flags: string[],
resolved?: Map<string, string>): string;

version(settings: LFortranSettings): Promise<string>;

/**
Expand Down Expand Up @@ -299,12 +306,21 @@ export class LFortranCLIAccessor implements LFortranAccessor {
return output;
}

resolve(filename: string, flags: string[], resolved?: Map<string, string>): string {
resolve(uri: string,
filename: string,
flags: string[],
resolved?: Map<string, string>): string {
const fnid: string = "resolve";
const start: number = performance.now();

let filePath: string = filename;

if (filePath === this.tmpFile.name) {
filePath = uri;
}

filePath = filePath.replace(RE_FILE_URI, "");

if (!fs.existsSync(filePath)) {
let resolution: string | undefined = resolved?.get(filePath);
if (resolution === undefined) {
Expand All @@ -331,8 +347,12 @@ export class LFortranCLIAccessor implements LFortranAccessor {

// if file name is `b.f90` then it will be replaced with `$(pwd)/b.f90`
// if file name is `a/b.f90` then it will be replaced with `$(pwd)/a/b.f90`

// -----------------------------------------------------------------------
// TODO: Collect an example that demonstrates the need for this resolution
// that does not work with `fs.realpathSync`, above.
// -----------------------------------------------------------------------
const newFilePath: string = path.resolve(filePath);

if (this.logger.isBenchmarkOrTraceEnabled()) {
this.logBenchmarkAndTrace(
fnid, start,
Expand Down Expand Up @@ -374,7 +394,7 @@ export class LFortranCLIAccessor implements LFortranAccessor {
for (let i = 0, k = symbols.length; i < k; i++) {
const symbol: Record<string, any> = symbols[i];
const symbolPath: string =
this.resolve(symbol.filename, settings.compiler.flags, resolved);
this.resolve(uri, symbol.filename, settings.compiler.flags, resolved);

const location: Location = symbol.location;
// location.uri = uri;
Expand Down Expand Up @@ -428,8 +448,8 @@ export class LFortranCLIAccessor implements LFortranAccessor {
const results: Record<string, any>[] = JSON.parse(stdout);
for (let i = 0, k = results.length; i < k; i++) {
const result: Record<string, any> = results[i];
let symbolPath: string =
this.resolve(result.filename, settings.compiler.flags);
const symbolPath: string =
this.resolve(uri, result.filename, settings.compiler.flags);

const location = result.location;

Expand All @@ -443,9 +463,6 @@ export class LFortranCLIAccessor implements LFortranAccessor {
end.line--;
end.character--;

if (symbolPath.endsWith(".tmp")) {
symbolPath = uri;
}
definitions.push({
targetUri: symbolPath,
targetRange: range,
Expand Down
27 changes: 2 additions & 25 deletions server/src/lfortran-language-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

import fs from 'fs';

import path from 'path';

import {
CompletionItem,
CompletionItemKind,
Expand Down Expand Up @@ -260,29 +258,8 @@ export class LFortranLanguageServer {
const document = this.documents.get(uri);
let text = document?.getText();
if (text === undefined) {
let filePath: string = uri;
if (filePath.startsWith("file://")) {
filePath = filePath.substring(7);
}
if (!fs.existsSync(filePath)) {
let resolution: string | undefined = resolved?.get(filePath);
if (resolution === undefined) {
for (const flag of this.settings.compiler.flags) {
if (flag.startsWith("-I")) {
const includeDir = flag.substring(2);
resolution = path.join(includeDir, filePath);
if (fs.existsSync(resolution)) {
resolution = fs.realpathSync(resolution);
resolved?.set(filename, resolution);
filePath = resolution;
break;
}
}
}
} else {
filePath = resolution;
}
}
const filePath: string =
this.lfortran.resolve(uri, uri, this.settings.compiler.flags, resolved);
if (fs.existsSync(filePath)) {
let entry = this.fileCache.get(filePath);
const stats = fs.statSync(filePath);
Expand Down

0 comments on commit 46eccf3

Please sign in to comment.