Skip to content

Commit

Permalink
reuse isDefined function
Browse files Browse the repository at this point in the history
  • Loading branch information
joswig committed Nov 5, 2024
1 parent f9cc9d9 commit 818aa69
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
7 changes: 6 additions & 1 deletion src/components/sequencing/form/ArgEditor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@
{:else}
<ArgTitle {argDef} />
{#if isVariable && isFswCommandArgumentEnum(argDef)}
<div class="st-typography-small-caps" title="Dictionary values must be quoted in editor">Global/Parameter</div>
<div
class="st-typography-small-caps"
title="Literals are quoted in editor, Constants/Parameters/Variables are unquoted"
>
Reference
</div>
<EnumEditor
{argDef}
initVal={argInfo.text ?? ''}
Expand Down
7 changes: 4 additions & 3 deletions src/utilities/codemirror/vml/vmlBlockLibrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
HwCommand,
NumericRange,
} from '@nasa-jpl/aerie-ampcs';
import { isDefined } from '../../sequence-editor/tree-utils';
import { VmlLanguage } from './vml';
import {
RULE_BLOCK,
Expand Down Expand Up @@ -49,7 +50,7 @@ export function vmlBlockLibraryToCommandDictionary(vml: string, id?: string, pat
...(parsed.topNode.getChild(RULE_FUNCTIONS)?.getChildren(RULE_FUNCTION) ?? []).map(blockNode =>
blockToCommandDef(blockNode, vml),
),
].filter((maybeCommandDef): maybeCommandDef is FswCommand => !!maybeCommandDef);
].filter(isDefined);

const mission_name = '';
const spacecraft_ids = [0];
Expand Down Expand Up @@ -84,7 +85,7 @@ function blockToCommandDef(functionNode: SyntaxNode, vml: string): FswCommand |
const parameterNodes = commonFunctionNode?.getChild(RULE_PARAMETERS)?.getChildren(RULE_PARAMETER) ?? [];
const fswArguments: FswCommandArgument[] = parameterNodes
?.map(parameterNode => inputToArgument(parameterNode, vml))
.filter((maybeArg): maybeArg is FswCommandArgument => !!maybeArg);
.filter(isDefined);

if (stem) {
return {
Expand Down Expand Up @@ -205,7 +206,7 @@ function parseRange(parameterNode: SyntaxNode | null, vml: string): null | strin
}
return null;
})
.filter((maybeRangeValue): maybeRangeValue is number | string | NumericRange => !!maybeRangeValue);
.filter(isDefined);

// mixed arrays aren't resolved due to undefined meaning
if (rangeValues.every(rangeValue => typeof rangeValue === 'number')) {
Expand Down
10 changes: 4 additions & 6 deletions src/utilities/codemirror/vml/vmlFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { syntaxTree } from '@codemirror/language';
import type { ChangeSpec, EditorState } from '@codemirror/state';
import type { SyntaxNode } from '@lezer/common';
import { EditorView } from 'codemirror';
import { isDefined } from '../../sequence-editor/tree-utils';
import {
RULE_ASSIGNMENT,
RULE_CALL_PARAMETERS,
Expand Down Expand Up @@ -142,15 +143,15 @@ export function vmlFormat(view: EditorView): void {
const docText = state.toText(state.sliceDoc());

const maybeChanges = linesToFormat.flatMap((line: LineOfNodes) => {
const firstNode = line.find(maybeNode => !!maybeNode);
const firstNode = line.find(isDefined);
if (firstNode === undefined) {
// unexpected case of no nodes on line
return [];
}

const commandLine = docText.lineAt(firstNode.from);

const filteredArray: SyntaxNode[] = line.filter((maybeNode): maybeNode is SyntaxNode => !!maybeNode);
const filteredArray: SyntaxNode[] = line.filter(isDefined);
const deletions: ChangeSpec[] = [];

// remove indentation at start of line
Expand Down Expand Up @@ -209,10 +210,7 @@ export function vmlFormat(view: EditorView): void {
return [...deletions, ...insertions];
});

const changes = [
...commandIndentChangeMap.values(),
...maybeChanges.filter((maybeChange): maybeChange is ChangeSpec => !!maybeChange),
];
const changes = [...commandIndentChangeMap.values(), ...maybeChanges.filter(isDefined)];

// Consider delete end of line whitespace
// Consider alignment of comments
Expand Down
11 changes: 6 additions & 5 deletions src/utilities/codemirror/vml/vmlTreeUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import type { SyntaxNode, Tree } from '@lezer/common';
import { filterNodesToArray, getChildrenNode, getNearestAncestorNodeOfType } from '../../sequence-editor/tree-utils';
import {
filterNodesToArray,
getChildrenNode,
getNearestAncestorNodeOfType,
isDefined,
} from '../../sequence-editor/tree-utils';
import type { CommandInfoMapper } from '../commandInfoMapper';
import {
RULE_CALL_PARAMETER,
Expand Down Expand Up @@ -123,10 +128,6 @@ export class VmlCommandInfoMapper implements CommandInfoMapper {
}
}

function isDefined<Type>(maybeValue: Type | null | undefined): maybeValue is Type {
return maybeValue !== null && maybeValue !== undefined;
}

export function getArgumentPosition(argNode: SyntaxNode): number {
return (
getNearestAncestorNodeOfType(argNode, [RULE_STATEMENT])
Expand Down
4 changes: 4 additions & 0 deletions src/utilities/sequence-editor/tree-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,7 @@ export function filterNodesToArray(cursor: TreeCursor, filter?: (node: SyntaxNod
export function nodeContents(input: string, node: SyntaxNode): string {
return input.substring(node.from, node.to);
}

export function isDefined<Type>(maybeValue: Type | null | undefined): maybeValue is Type {
return maybeValue !== null && maybeValue !== undefined;
}

0 comments on commit 818aa69

Please sign in to comment.