Skip to content

Commit

Permalink
refactor: fold shared logic back in to core package
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesdabbs committed Jun 4, 2024
1 parent 594be10 commit b2a4fb2
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 104 deletions.
2 changes: 1 addition & 1 deletion packages/compile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"glob": "^8.1.0",
"js-yaml": "^4.1.0",
"yaml-front-matter": "^4.1.1",
"zod": "^3.22.4"
"zod": "^3.23.8"
},
"devDependencies": {
"@types/cors": "^2.8.17",
Expand Down
8 changes: 8 additions & 0 deletions packages/core/bin/build
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
set -exo pipefail

tsc --module es2022 --outDir dist/esm/
echo '{"type": "module"}' > dist/esm/package.json

tsc --module commonjs --outDir dist/cjs/
echo '{"type": "commonjs"}' > dist/cjs/package.json
10 changes: 7 additions & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@
},
"license": "MIT",
"author": "James Dabbs <james.dabbs@gmail.com> (https://jdabbs.com)",
"main": "./dist/esm/index.js",
"types": "./dist/types/index.d.ts",
"exports": {
"types": "./dist/types/index.d.ts",
"require": "./dist/cjs/index.js",
"import": "./dist/esm/index.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/pi-base/web.git"
},
"scripts": {
"build:peg": "peggy --plugin ./node_modules/ts-pegjs/dist/tspegjs -o src/Formula/Grammar.ts --cache src/Formula/Grammar.pegjs",
"build": "pnpm build:peg && tsc",
"build": "pnpm build:peg && ./bin/build",
"dev": "pnpm build:peg && tsc --watch",
"test": "vitest run",
"test:cov": "vitest run --coverage",
Expand All @@ -40,7 +44,7 @@
"unified": "^10.1.2",
"unist-util-is": "^5.2.1",
"unist-util-visit": "^4.1.2",
"zod": "^3.22.4"
"zod": "^3.23.8"
},
"devDependencies": {
"@types/debug": "^4.1.12",
Expand Down
6 changes: 1 addition & 5 deletions packages/core/src/Bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import { Space, spaceSchema } from './Space.js'
import { Theorem, theoremSchema } from './Theorem.js'
import { Trait, traitSchema } from './Trait.js'

export const defaultHost = import.meta.env?.VITE_PUBLIC_DATA_URL
? import.meta.env.VITE_PUBLIC_DATA_URL
: import.meta.env?.DEV
? 'http://localhost:3141'
: 'https://pi-base-bundles.s3.us-east-2.amazonaws.com'
export const defaultHost = 'https://pi-base-bundles.s3.us-east-2.amazonaws.com'

export type Version = {
ref: string
Expand Down
37 changes: 37 additions & 0 deletions packages/core/src/Id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type SpaceId = { kind: 'space'; id: number }
export type PropertyId = { kind: 'property'; id: number }
export type TheoremId = { kind: 'theorem'; id: number }


const pattern = /^(?<prefix>[spti])0*(?<id>\d+)/i

export function traitId({ space, property }: TraitId): string {
Expand Down Expand Up @@ -54,3 +55,39 @@ export function toInt(id: string): number {

return tagged.id
}

// TODO: these were extracted from other parallel-but-divergent implementations
// and should be unified.

type Pad = '' | '0' | '00' | '000' | '0000' | '00000' | '00000'
type XId<Prefix extends string> = `${Prefix}${Pad}${number}`

export type SId = XId<'S'>
export type PId = XId<'P'>
export type TId = XId<'T'>
export type SPId = [SId, PId]
export type EntityId = SId | PId | TId | SPId

export function isSpaceId(token: string): token is SId {
return token.match(/^S\d{1,6}$/) !== null
}

export function isPropertyId(token: string): token is PId {
return token.match(/^P\d{1,6}$/) !== null
}

export function isTheoremId(token: string): token is SId {
return token.match(/^T\d{1,6}$/) !== null
}

export function isTraitId(pair: [string, string]): pair is SPId {
return isSpaceId(pair[0]) && isPropertyId(pair[1])
}

export const idExp = /[PST]\d{1,6}/g

export function normalizeId(id: SId): SId
export function normalizeId(id: PId): PId
export function normalizeId(id: string) {
return `${id[0]}${id.slice(1).padStart(6, '0')}`
}
1 change: 1 addition & 0 deletions packages/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"npm-run-all": "^4.1.5"
},
"dependencies": {
"@pi-base/core": "workspace:*",
"debug": "^4.3.4",
"js-yaml": "^4.1.0",
"zod": "^3.23.8"
Expand Down
31 changes: 11 additions & 20 deletions packages/vscode/src/models/EntityStore.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import * as vscode from 'vscode'
import * as yaml from 'js-yaml'
import { z } from 'zod'
import { propertySchema, spaceSchema } from './schemas'
import {
Property,
PropertyId,
Space,
SpaceId,
isPropertyId,
isSpaceId,
normalizeId,
} from './types'
import { Space, Property, Id, schemas } from '@pi-base/core'

type Location = { path: string; uri: vscode.Uri }

Expand All @@ -28,9 +19,9 @@ export class EntityStore {

lookup(token: string) {
try {
if (isSpaceId(token)) {
if (Id.isSpaceId(token)) {
return this.fetch(token)
} else if (isPropertyId(token)) {
} else if (Id.isPropertyId(token)) {
return this.fetch(token)
}
} catch (error) {
Expand All @@ -39,19 +30,19 @@ export class EntityStore {
}
}

private async fetch(id: SpaceId): Promise<(Space & Location) | null>
private async fetch(id: PropertyId): Promise<(Property & Location) | null>
private async fetch(id: Id.SId): Promise<(Space & Location) | null>
private async fetch(id: Id.PId): Promise<(Property & Location) | null>
private async fetch(id: string) {
switch (id[0]) {
case 'S':
return this.load(
`spaces/${normalizeId(id as SpaceId)}/README.md`,
spaceSchema,
return this.load<Space>(
`spaces/${Id.normalizeId(id as Id.SId)}/README.md`,
schemas.space,
)
case 'P':
return this.load(
`properties/${normalizeId(id as PropertyId)}.md`,
propertySchema,
return this.load<Property>(
`properties/${Id.normalizeId(id as Id.PId)}.md`,
schemas.property,
)
default:
return null
Expand Down
21 changes: 0 additions & 21 deletions packages/vscode/src/models/schemas.ts

This file was deleted.

38 changes: 0 additions & 38 deletions packages/vscode/src/models/types.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/vscode/src/providers/EntityIdLinkProvider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as vscode from 'vscode'
import { BaseEntityProvider, matchingRanges } from './BaseEntityProvider'
import { idExp } from '../models/types'
import { Id } from '@pi-base/core'
import { debug } from '../models/logging'

export class EntityIdLinkProvider
Expand All @@ -10,7 +10,7 @@ export class EntityIdLinkProvider
async provideDocumentLinks(document: vscode.TextDocument) {
debug('EntityIdLinkProvider#provideDocumentLinks')

const ranges = matchingRanges(document, idExp)
const ranges = matchingRanges(document, Id.idExp)

const links: vscode.DocumentLink[] = []
for (const [range, match] of ranges) {
Expand Down
4 changes: 2 additions & 2 deletions packages/vscode/src/providers/decorationProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vscode from 'vscode'
import { idExp } from '../models/types'
import { Id } from '@pi-base/core'
import { EntityStore } from '../models/EntityStore'
import { matchingRanges } from './BaseEntityProvider'

Expand Down Expand Up @@ -47,7 +47,7 @@ async function triggerUpdateDecorations(
editor: vscode.TextEditor,
entities: EntityStore,
) {
const ranges = matchingRanges(editor.document, idExp)
const ranges = matchingRanges(editor.document, Id.idExp)

const decorations: vscode.DecorationOptions[] = []
for (const [range, match] of ranges) {
Expand Down
8 changes: 5 additions & 3 deletions packages/vscode/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
{
"compilerOptions": {
"module": "Node16",
"module": "ES2022",
"target": "ES2022",
"lib": [
"ES2022"
"ES2022",
"DOM"
],
"sourceMap": true,
"rootDir": "src",
"strict": true /* enable all strict type-checking options */
"strict": true, /* enable all strict type-checking options */
"moduleResolution": "bundler"
/* Additional Checks */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
Expand Down
16 changes: 7 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b2a4fb2

Please sign in to comment.