Skip to content

Commit

Permalink
Remove redundant SourceRange definition
Browse files Browse the repository at this point in the history
  • Loading branch information
jtran committed Jan 16, 2025
1 parent 779644a commit 12b5f2c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 26 deletions.
5 changes: 3 additions & 2 deletions src/lang/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
ArtifactCommand,
ArtifactGraph,
defaultArtifactGraph,
isTopLevelModule,
SourceRange,
} from 'lang/wasm'
import { Operation } from 'wasm-lib/kcl/bindings/Operation'
Expand Down Expand Up @@ -255,7 +256,7 @@ export function kclErrorsToDiagnostics(
errors: KCLError[]
): CodeMirrorDiagnostic[] {
return errors
?.filter((err) => err.sourceRange[2] === 0)
?.filter((err) => isTopLevelModule(err.sourceRange))
.map((err) => {
return {
from: err.sourceRange[0],
Expand All @@ -270,7 +271,7 @@ export function complilationErrorsToDiagnostics(
errors: CompilationError[]
): CodeMirrorDiagnostic[] {
return errors
?.filter((err) => err.sourceRange[2] === 0)
?.filter((err) => isTopLevelModule(err.sourceRange))
.map((err) => {
let severity: any = 'error'
if (err.severity === 'Warning') {
Expand Down
11 changes: 3 additions & 8 deletions src/lang/std/artifactGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import {
PathToNode,
Program,
SourceRange,
sourceRangeFromRust,
RustSourceRange,
PathArtifact,
PlaneArtifact,
WallArtifact,
Expand All @@ -28,7 +26,7 @@ interface BaseArtifact {
}

export interface CodeRef {
range: RustSourceRange
range: SourceRange
pathToNode: PathToNode
}

Expand Down Expand Up @@ -404,13 +402,10 @@ export function getCodeRefsByArtifactId(
}
}

export function codeRefFromRange(
range: RustSourceRange,
ast: Program
): CodeRef {
export function codeRefFromRange(range: SourceRange, ast: Program): CodeRef {
return {
range,
pathToNode: getNodePathFromSourceRange(ast, sourceRangeFromRust(range)),
pathToNode: getNodePathFromSourceRange(ast, range),
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/lang/std/engineConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
ArtifactGraph,
defaultRustSourceRange,
ExecState,
RustSourceRange,
SourceRange,
} from 'lang/wasm'
import { VITE_KC_API_WS_MODELING_URL, VITE_KC_DEV_TOKEN } from 'env'
Expand Down Expand Up @@ -1302,8 +1301,8 @@ export enum EngineCommandManagerEvents {

interface PendingMessage {
command: EngineCommand
range: RustSourceRange
idToRangeMap: { [key: string]: RustSourceRange }
range: SourceRange
idToRangeMap: { [key: string]: SourceRange }
resolve: (data: [Models['WebSocketResponse_type']]) => void
reject: (reason: string) => void
promise: Promise<[Models['WebSocketResponse_type']]>
Expand Down Expand Up @@ -2018,9 +2017,9 @@ export class EngineCommandManager extends EventTarget {
return Promise.reject(new Error('rangeStr is undefined'))
if (commandStr === undefined)
return Promise.reject(new Error('commandStr is undefined'))
const range: RustSourceRange = JSON.parse(rangeStr)
const range: SourceRange = JSON.parse(rangeStr)
const command: EngineCommand = JSON.parse(commandStr)
const idToRangeMap: { [key: string]: RustSourceRange } =
const idToRangeMap: { [key: string]: SourceRange } =
JSON.parse(idToRangeStr)

// Current executeAst is stale, going to interrupt, a new executeAst will trigger
Expand Down
19 changes: 8 additions & 11 deletions src/lang/wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import { EnvironmentRef } from '../wasm-lib/kcl/bindings/EnvironmentRef'
import { Environment } from '../wasm-lib/kcl/bindings/Environment'
import { Node } from 'wasm-lib/kcl/bindings/Node'
import { CompilationError } from 'wasm-lib/kcl/bindings/CompilationError'
import { SourceRange as RustSourceRange } from 'wasm-lib/kcl/bindings/SourceRange'
import { SourceRange } from 'wasm-lib/kcl/bindings/SourceRange'
import { getAllCurrentSettings } from 'lib/settings/settingsUtils'
import { Operation } from 'wasm-lib/kcl/bindings/Operation'
import { KclErrorWithOutputs } from 'wasm-lib/kcl/bindings/KclErrorWithOutputs'
Expand Down Expand Up @@ -89,7 +89,7 @@ export type { BinaryPart } from '../wasm-lib/kcl/bindings/BinaryPart'
export type { Literal } from '../wasm-lib/kcl/bindings/Literal'
export type { LiteralValue } from '../wasm-lib/kcl/bindings/LiteralValue'
export type { ArrayExpression } from '../wasm-lib/kcl/bindings/ArrayExpression'
export type { SourceRange as RustSourceRange } from 'wasm-lib/kcl/bindings/SourceRange'
export type { SourceRange } from 'wasm-lib/kcl/bindings/SourceRange'

export type SyntaxType =
| 'Program'
Expand Down Expand Up @@ -118,20 +118,13 @@ export type { Solid } from '../wasm-lib/kcl/bindings/Solid'
export type { KclValue } from '../wasm-lib/kcl/bindings/KclValue'
export type { ExtrudeSurface } from '../wasm-lib/kcl/bindings/ExtrudeSurface'

/**
* The first two items are the start and end points (byte offsets from the start of the file).
* The third item is whether the source range belongs to the 'main' file, i.e., the file currently
* being rendered/displayed in the editor (TODO we need to handle modules better in the frontend).
*/
export type SourceRange = [number, number, number]

/**
* Convert a SourceRange as used inside the KCL interpreter into the above one for use in the
* frontend (essentially we're eagerly checking whether the frontend should care about the SourceRange
* so as not to expose details of the interpreter's current representation of module ids throughout
* the frontend).
*/
export function sourceRangeFromRust(s: RustSourceRange): SourceRange {
export function sourceRangeFromRust(s: SourceRange): SourceRange {
return [s[0], s[1], s[2]]
}

Expand All @@ -145,10 +138,14 @@ export function defaultSourceRange(): SourceRange {
/**
* Create a default RustSourceRange for testing or as a placeholder.
*/
export function defaultRustSourceRange(): RustSourceRange {
export function defaultRustSourceRange(): SourceRange {
return [0, 0, 0]
}

export function isTopLevelModule(range: SourceRange): boolean {
return range[2] === 0
}

export const wasmUrl = () => {
// For when we're in electron (file based) or web server (network based)
// For some reason relative paths don't work as expected. Otherwise we would
Expand Down
5 changes: 5 additions & 0 deletions src/wasm-lib/kcl/src/source_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ impl ModuleId {
}
}

/// The first two items are the start and end points (byte offsets from the start of the file).
/// The third item is whether the source range belongs to the 'main' file, i.e., the file currently
/// being rendered/displayed in the editor (TODO we need to handle modules better in the frontend).
///
/// @see {isTopLevelModule}
#[derive(Debug, Default, Deserialize, Serialize, PartialEq, Copy, Clone, ts_rs::TS, JsonSchema, Hash, Eq)]
#[ts(export, type = "[number, number, number]")]
pub struct SourceRange([usize; 3]);
Expand Down

0 comments on commit 12b5f2c

Please sign in to comment.