Skip to content

Commit

Permalink
Hook up the linter with library sequences.
Browse files Browse the repository at this point in the history
* The linter will typechecking the library sequences parameters/arguments
  • Loading branch information
goetzrrGit committed Nov 20, 2024
1 parent 1aa2f22 commit f86c45d
Show file tree
Hide file tree
Showing 5 changed files with 378 additions and 77 deletions.
43 changes: 43 additions & 0 deletions src/utilities/codemirror/codemirror-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
FswCommandArgumentUnsigned,
FswCommandArgumentVarString,
} from '@nasa-jpl/aerie-ampcs';
import type { VariableDeclaration } from '@nasa-jpl/seq-json-schema/types';
import type { EditorView } from 'codemirror';
import { fswCommandArgDefault } from '../sequence-editor/command-dictionary';
import type { CommandInfoMapper } from './commandInfoMapper';
Expand Down Expand Up @@ -107,6 +108,48 @@ export function getMissingArgDefs(argInfoArray: ArgTextDef[]): FswCommandArgumen
.map(argInfo => argInfo.argDef);
}

export function getDefaultVariableArgs(parameters: VariableDeclaration[]): string[] {
return parameters.map(parameter => {
switch (parameter.type) {
case 'STRING':
return `"${parameter.name}"`;
case 'FLOAT':
return parameter.allowable_ranges && parameter.allowable_ranges.length > 0
? parameter.allowable_ranges[0].min
: '0';
case 'INT':
case 'UINT':
return parameter.allowable_ranges && parameter.allowable_ranges.length > 0
? parameter.allowable_ranges[0].min
: '0';
case 'ENUM':
return parameter.allowable_values && parameter.allowable_values.length > 0
? `"${parameter.allowable_values[0]}"`
: parameter.enum_name
? `${parameter.enum_name}`
: 'UNKNOWN';
default:
throw Error(`unknown argument type ${parameter.type}`);
}
}) as string[];
}

export function addDefaultVariableArgs(
parameters: VariableDeclaration[],
view: EditorView,
commandNode: SyntaxNode,
commandInfoMapper: CommandInfoMapper,
) {
const insertPosition = commandInfoMapper.getArgumentAppendPosition(commandNode);
if (insertPosition !== undefined) {
const str = commandInfoMapper.formatArgumentArray(getDefaultVariableArgs(parameters), commandNode);
const transaction = view.state.update({
changes: { from: insertPosition, insert: str },
});
view.dispatch(transaction);
}
}

export function isQuoted(s: string): boolean {
return s.startsWith('"') && s.endsWith('"');
}
Expand Down
6 changes: 5 additions & 1 deletion src/utilities/codemirror/seq-n-tree-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ export class SeqNCommandInfoMapper implements CommandInfoMapper {
}

getArgumentAppendPosition(commandOrRepeatArgNode: SyntaxNode | null): number | undefined {
if (commandOrRepeatArgNode?.name === RULE_COMMAND) {
if (
commandOrRepeatArgNode?.name === RULE_COMMAND ||
commandOrRepeatArgNode?.name === TOKEN_ACTIVATE ||
commandOrRepeatArgNode?.name === TOKEN_LOAD
) {
const argsNode = commandOrRepeatArgNode.getChild('Args');
const stemNode = commandOrRepeatArgNode.getChild('Stem');
return getFromAndTo([stemNode, argsNode]).to;
Expand Down
5 changes: 3 additions & 2 deletions src/utilities/sequence-editor/extension-points.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '@nasa-jpl/aerie-ampcs';
import { get } from 'svelte/store';
import { inputFormat, sequenceAdaptation } from '../../stores/sequence-adaptation';
import type { IOutputFormat } from '../../types/sequencing';
import type { IOutputFormat, LibrarySequence } from '../../types/sequencing';
import { seqJsonLinter } from './seq-json-linter';
import { sequenceLinter } from './sequence-linter';

Expand Down Expand Up @@ -81,14 +81,15 @@ export function inputLinter(
channelDictionary: ChannelDictionary | null = null,
commandDictionary: CommandDictionary | null = null,
parameterDictionaries: ParameterDictionary[] = [],
librarySequences: LibrarySequence[] = [],
): Extension {
return linter(view => {
const inputLinter = get(sequenceAdaptation).inputFormat.linter;
const tree = syntaxTree(view.state);
const treeNode = tree.topNode;
let diagnostics: Diagnostic[];

diagnostics = sequenceLinter(view, channelDictionary, commandDictionary, parameterDictionaries);
diagnostics = sequenceLinter(view, channelDictionary, commandDictionary, parameterDictionaries, librarySequences);

if (inputLinter !== undefined && commandDictionary !== null) {
diagnostics = inputLinter(diagnostics, commandDictionary, view, treeNode);
Expand Down
Loading

0 comments on commit f86c45d

Please sign in to comment.