Skip to content

Commit

Permalink
feat: add more convenience around profile id parsing
Browse files Browse the repository at this point in the history
* profile id and version are not currently present in the profile document
* add convenience to nodejs_comlink package to parse them from file name in the parseProfile function
  • Loading branch information
TheEdward162 committed Jan 30, 2024
1 parent 95ac4fc commit e5ebe32
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
2 changes: 2 additions & 0 deletions core/comlink/src/typescript_parser/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub struct Profile {
pub scope: Option<String>,
/// Name part of `scope/name`.
pub name: String,
/// Version of the profile.
pub version: String,
/// Documentation of the profile.
pub documentation: Documentation,
/// List of use cases.
Expand Down
1 change: 1 addition & 0 deletions core/comlink/src/typescript_parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ impl<'a> ProfileParser<'a> {
}

fn parse_profile(&mut self) -> (Profile, ProfileSpans) {
// TODO: scope, name and version are never parsed because they don't appear in the document
let mut profile = Profile::default();
let mut spans = ProfileSpans::default();

Expand Down
5 changes: 1 addition & 4 deletions packages/comlink_language_server/src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,7 @@ export class ComlinkDocument implements TextDocument {
undefined,
false
);
const { profile, spans, diagnostics } = parser.parseProfile(source)
const profileId = ComlinkParser.parseProfileFileName(this.uri)
profile.scope = profileId.scope
profile.name = profileId.name
const { profile, spans, diagnostics } = parser.parseProfile(source, this.uri)
result = { kind: 'profile', profile, spans, diagnostics }
ctx.work?.workDoneProgress.done();

Expand Down
35 changes: 27 additions & 8 deletions packages/nodejs_comlink/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,27 @@ class TextCoderImpl implements TextCoder {
}
}

type ParseProfileOutput = { profile: Profile, spans: ProfileSpans, diagnostics: Diagnostic[] }
export class ComlinkParser extends AppContextSync {
public static parseProfileFileName(uri: string): { scope?: string, name: string } {
const baseName = path.basename(uri, '.profile.ts')
public static parseProfileFileName(uri: string): { scope?: string, name: string, version?: string } {
let baseName = path.basename(uri, '.profile.ts')
const result: { scope?: string, name: string, version?: string } = { name: baseName }

let versionSeparatorIndex = baseName.indexOf('@')
if (versionSeparatorIndex >= 0) {
result.version = baseName.slice(versionSeparatorIndex + 1)
baseName = baseName.slice(0, versionSeparatorIndex)
}

const parts = baseName.split('.')
if (parts.length === 1) {
return { name: parts[0] }
result.name = parts[0]
} else {
return { scope: parts[0], name: parts[1] }
result.scope = parts[0]
result.name = parts[1]
}

return result
}

public static async create(): Promise<ComlinkParser> {
Expand All @@ -55,7 +67,7 @@ export class ComlinkParser extends AppContextSync {
instance: WebAssembly.Instance,
parseTsProfile: () => void
}
private parserState: undefined | { profile: string } | { profile: Profile, spans: ProfileSpans, diagnostics: Diagnostic[] }
private parserState: undefined | { profile: string } | ParseProfileOutput

private constructor(module: WebAssembly.Module) {
super()
Expand Down Expand Up @@ -102,13 +114,20 @@ export class ComlinkParser extends AppContextSync {
writeStream(_handle: number, _data: Uint8Array): number { throw new Error('not implemented') }
closeStream(_handle: number): void { throw new Error('not implemented') }

public parseProfile(profile: string): { profile: Profile, spans: ProfileSpans, diagnostics: Diagnostic[] } {
public parseProfile(profile: string, filename?: string): ParseProfileOutput {
this.parserState = { profile }
this.instance!.parseTsProfile()

const result = this.parserState
const result = this.parserState as unknown as ParseProfileOutput
this.parserState = undefined

return result as any
if (filename !== undefined) {
const { scope, name, version } = ComlinkParser.parseProfileFileName(filename)
result.profile.scope = scope
result.profile.name = name
result.profile.version = version ?? '0.0.0'
}

return result
}
}
1 change: 1 addition & 0 deletions packages/nodejs_comlink/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type Documentation = {
export type Profile = {
scope?: string,
name: string,
version: string,
documentation: Documentation
usecases: UseCase[]
}
Expand Down

0 comments on commit e5ebe32

Please sign in to comment.