Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion server/src/capabilities/capabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,28 @@ export class ScopeItemCapability {
}

private findModuleByUri(uri: string): ScopeItemCapability | undefined {
const moduleName = uri.split('/').at(-1)?.split('.').slice(0, -1).join('.');
const normalisedUri = uri.toFilePath().toFileUri();

const allModules = [...(this.modules?.values() ?? [])].flat();
const uriMatches = allModules.filter(module => {
if (!module.locationUri) {
return false;
}

const moduleUri = module.locationUri.toFilePath().toFileUri();
return moduleUri.ciEquals(normalisedUri);
});

if (uriMatches.length === 1) {
return uriMatches[0];
}

if (uriMatches.length > 1) {
Services.logger.error(`Module URI ambiguity: ${uriMatches.length} found for ${normalisedUri}.`);
return;
Comment thread
DecimalTurn marked this conversation as resolved.
Outdated
}

const moduleName = normalisedUri.split('/').at(-1)?.split('.').slice(0, -1).join('.');
if (!moduleName) {
Services.logger.error(`Bad URI or name: ${moduleName} from ${uri}`);
Comment thread
DecimalTurn marked this conversation as resolved.
Outdated
return;
Expand Down
3 changes: 2 additions & 1 deletion server/src/project/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,8 @@ class WorkspaceEvents {
return;
}

const results = Services.projectScope.getDeclarationLocation(params.textDocument.uri, params.position);
const normalisedUri = params.textDocument.uri.toFilePath().toFileUri();
const results = Services.projectScope.getDeclarationLocation(normalisedUri, params.position);
Services.logger.debug(`Processed onDefinition: returning ${JSON.stringify(results)}`);

if (results === undefined) {
Expand Down
70 changes: 70 additions & 0 deletions server/src/test/capabilities.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import 'reflect-metadata';
import '../extensions/stringExtensions';

import { describe, it } from 'mocha';
import * as assert from 'assert';
import { Position, Range } from 'vscode-languageserver';

import { AssignmentType, ScopeItemCapability, ScopeType } from '../capabilities/capabilities';

function createElement(name: string, uri: string, range: Range) {
return {
context: {
range,
document: { uri }
},
identifierCapability: {
name,
range
}
} as any;
}

describe('ScopeItemCapability module lookup by URI', () => {
it('resolves declaration location when module name differs from filename', () => {
const projectScope = new ScopeItemCapability(undefined, ScopeType.PROJECT);

const moduleUri = 'file:///c:/sample/project/Somethings.bas';
const moduleScope = new ScopeItemCapability(
createElement('Something', moduleUri, Range.create(0, 0, 20, 0)),
ScopeType.MODULE,
AssignmentType.NONE,
projectScope
);
moduleScope.locationUri = moduleUri;

const declaration = new ScopeItemCapability(
createElement('Test', moduleUri, Range.create(2, 0, 4, 0)),
ScopeType.SUBROUTINE,
AssignmentType.NONE,
moduleScope
);
declaration.locationUri = moduleUri;

const reference = new ScopeItemCapability(
createElement('Test', moduleUri, Range.create(10, 18, 10, 22)),
ScopeType.REFERENCE,
AssignmentType.CALL,
moduleScope
);
reference.locationUri = moduleUri;
reference.link = declaration;

moduleScope.references = new Map([
['Test', [reference]]
]);

projectScope.modules = new Map([
['Something', [moduleScope]]
]);

const locationLinks = projectScope.getDeclarationLocation(
moduleUri,
Position.create(10, 19)
);

assert.ok(locationLinks, 'Expected declaration location results');
assert.strictEqual(locationLinks?.length, 1, 'Expected one declaration location');
assert.strictEqual(locationLinks?.[0].targetUri, moduleUri, 'Expected location to resolve to module URI');
});
});