File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ import { type BdlConfig } from "@disjukr/bdl/io/config";
66import { type BdlStandard } from "@disjukr/bdl/io/standard" ;
77import parseBdl from "@disjukr/bdl/parser" ;
88import builtinStandards from "@disjukr/bdl/builtin/standards" ;
9+ import { resolveModulePath } from "./module-path.ts" ;
910
1011export class BdlShortTermContext {
1112 #workspaceFolder: vscode . WorkspaceFolder | undefined ;
@@ -134,10 +135,12 @@ export class BdlShortTermDocumentContext {
134135 const pathEntries = Object . entries ( bdlConfig . paths ) ;
135136 for ( const [ packageName , packagePath ] of pathEntries ) {
136137 const absPkgPath = vscode . Uri . joinPath ( cfgDir , packagePath ) . path ;
137- if ( documentPath . startsWith ( absPkgPath ) ) {
138- const subPath = documentPath . slice ( absPkgPath . length ) ;
139- const names = subPath . split ( "/" ) . filter ( Boolean ) ;
140- const modulePath = `${ packageName } .${ names . join ( "." ) } ` ;
138+ const modulePath = resolveModulePath (
139+ packageName ,
140+ absPkgPath ,
141+ documentPath ,
142+ ) ;
143+ if ( modulePath ) {
141144 this . #moduleInfo = { success : true , packageName, modulePath } ;
142145 break get;
143146 }
Original file line number Diff line number Diff line change 1+ import { assertEquals } from "jsr:@std/assert@1" ;
2+ import { resolveModulePath } from "./module-path.ts" ;
3+
4+ Deno . test ( "resolveModulePath strips the .bdl extension" , ( ) => {
5+ assertEquals (
6+ resolveModulePath (
7+ "pkg" ,
8+ "/workspace/schemas/pkg" ,
9+ "/workspace/schemas/pkg/foo/bar.bdl" ,
10+ ) ,
11+ "pkg.foo.bar" ,
12+ ) ;
13+ } ) ;
14+
15+ Deno . test ( "resolveModulePath rejects sibling package prefixes" , ( ) => {
16+ assertEquals (
17+ resolveModulePath (
18+ "pkg" ,
19+ "/workspace/schemas/pkg" ,
20+ "/workspace/schemas/pkg-extra/foo.bdl" ,
21+ ) ,
22+ undefined ,
23+ ) ;
24+ } ) ;
25+
26+ Deno . test ( "resolveModulePath ignores non-bdl documents" , ( ) => {
27+ assertEquals (
28+ resolveModulePath (
29+ "pkg" ,
30+ "/workspace/schemas/pkg" ,
31+ "/workspace/schemas/pkg/foo/bar.json" ,
32+ ) ,
33+ undefined ,
34+ ) ;
35+ } ) ;
Original file line number Diff line number Diff line change 1+ export function resolveModulePath (
2+ packageName : string ,
3+ packageRootPath : string ,
4+ documentPath : string ,
5+ ) : string | undefined {
6+ const normalizedPackageRoot = packageRootPath . endsWith ( "/" )
7+ ? packageRootPath . slice ( 0 , - 1 )
8+ : packageRootPath ;
9+ const packagePrefix = `${ normalizedPackageRoot } /` ;
10+ if ( ! documentPath . startsWith ( packagePrefix ) ) return undefined ;
11+
12+ const relativeDocumentPath = documentPath . slice ( packagePrefix . length ) ;
13+ if ( ! relativeDocumentPath . endsWith ( ".bdl" ) ) return undefined ;
14+
15+ const relativeModulePath = relativeDocumentPath
16+ . slice ( 0 , - ".bdl" . length )
17+ . split ( "/" )
18+ . filter ( Boolean )
19+ . join ( "." ) ;
20+ if ( ! relativeModulePath ) return undefined ;
21+
22+ return `${ packageName } .${ relativeModulePath } ` ;
23+ }
You can’t perform that action at this time.
0 commit comments