Skip to content

Commit

Permalink
Format source files with prettify
Browse files Browse the repository at this point in the history
  • Loading branch information
Silvia Chen committed Jan 29, 2025
1 parent 2cccea2 commit 5618223
Show file tree
Hide file tree
Showing 33 changed files with 8,993 additions and 9,762 deletions.
68 changes: 32 additions & 36 deletions src/completion/completeAsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,47 @@
* SPDX-License-Identifier: MIT
*/

import {
CompletionList,
JSONDocument,
Position,
TextDocument
} from 'vscode-json-languageservice'

import {
ASLOptions,
ASTTree,
findNodeAtLocation,
} from '../utils/astUtilityFunctions'
import { CompletionList, JSONDocument, Position, TextDocument } from 'vscode-json-languageservice'

import { ASLOptions, ASTTree, findNodeAtLocation } from '../utils/astUtilityFunctions'

import completeSnippets from './completeSnippets'
import completeStateNames from './completeStateNames'

export default function completeAsl(document: TextDocument, position: Position, doc: JSONDocument, jsonCompletions: CompletionList | null, aslOptions?: ASLOptions): CompletionList {

const offset = document.offsetAt(position)
const rootNode = (doc as ASTTree).root

if (!rootNode) {
return {
isIncomplete: false,
items: []
}
export default function completeAsl(
document: TextDocument,
position: Position,
doc: JSONDocument,
jsonCompletions: CompletionList | null,
aslOptions?: ASLOptions,
): CompletionList {
const offset = document.offsetAt(position)
const rootNode = (doc as ASTTree).root

if (!rootNode) {
return {
isIncomplete: false,
items: [],
}
}

const node = findNodeAtLocation(rootNode, offset)
const node = findNodeAtLocation(rootNode, offset)

const snippetsList = completeSnippets(node, offset, aslOptions)
let completionList = completeStateNames(node, offset, document, aslOptions) ?? jsonCompletions
const snippetsList = completeSnippets(node, offset, aslOptions)
let completionList = completeStateNames(node, offset, document, aslOptions) ?? jsonCompletions

if (completionList?.items) {
completionList.items = completionList.items.concat(snippetsList)
} else {
completionList = {
isIncomplete: false,
items: snippetsList
}
if (completionList?.items) {
completionList.items = completionList.items.concat(snippetsList)
} else {
completionList = {
isIncomplete: false,
items: snippetsList,
}
}

// Assign sort order for the completion items so we maintain order
// and snippets are shown near the end of the completion list
completionList.items.map((item,index) => ({ ...item, sortText: index.toString()}))
// Assign sort order for the completion items so we maintain order
// and snippets are shown near the end of the completion list
completionList.items.map((item, index) => ({ ...item, sortText: index.toString() }))

return completionList
return completionList
}
118 changes: 59 additions & 59 deletions src/completion/completeSnippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,94 +4,94 @@
*/

import {
ASTNode,
CompletionItem,
CompletionItemKind,
InsertTextFormat,
PropertyASTNode
ASTNode,
CompletionItem,
CompletionItemKind,
InsertTextFormat,
PropertyASTNode,
} from 'vscode-json-languageservice'

import {
findPropChildByName,
insideStateNode,
isChildOfStates,
isObjectNode,
} from '../utils/astUtilityFunctions'
import { findPropChildByName, insideStateNode, isChildOfStates, isObjectNode } from '../utils/astUtilityFunctions'

import errorHandlingSnippetsRaw from '../snippets/error_handling.json'
import stateSnippetsRaw from '../snippets/states.json'

interface Snippet {
name: string,
body: string[],
description: string
name: string
body: string[]
description: string
}

const ERROR_HANDLING_STATES = ['Task', 'Parallel', 'Map']
export const stateSnippets = parseSnippetsFromJson(stateSnippetsRaw)
export const errorHandlingSnippets = parseSnippetsFromJson(errorHandlingSnippetsRaw)

function parseSnippetsFromJson(json: Snippet[]): CompletionItem[] {
return json.map(snippet => {
const item = CompletionItem.create(snippet.name)
item.kind = CompletionItemKind.Snippet
item.insertTextFormat = InsertTextFormat.Snippet
item.insertText = snippet.body.join('\n')
item.documentation = snippet.description

return item
})
return json.map((snippet) => {
const item = CompletionItem.create(snippet.name)
item.kind = CompletionItemKind.Snippet
item.insertTextFormat = InsertTextFormat.Snippet
item.insertText = snippet.body.join('\n')
item.documentation = snippet.description

return item
})
}

function doesStateSupportErrorHandling(node: ASTNode): boolean {
let typeNode: PropertyASTNode | undefined
let typeNode: PropertyASTNode | undefined

if(isObjectNode(node)) {
typeNode = findPropChildByName(node, 'Type')
}
if (isObjectNode(node)) {
typeNode = findPropChildByName(node, 'Type')
}

return ERROR_HANDLING_STATES.includes(typeNode?.valueNode?.value as string)
return ERROR_HANDLING_STATES.includes(typeNode?.valueNode?.value as string)
}
interface CompleteSnippetsOptions {
shouldShowStateSnippets?: boolean,
shouldShowErrorSnippets?: {
retry: boolean,
catch: boolean
}
shouldShowStateSnippets?: boolean
shouldShowErrorSnippets?: {
retry: boolean
catch: boolean
}
}

export default function completeSnippets(node: ASTNode | undefined, offset: number, options?: CompleteSnippetsOptions): CompletionItem[] {
if (node) {
const errorSnippetOptionsNotDefined = options?.shouldShowErrorSnippets === undefined
// If the value of shouldShowStateSnippets is false prevent the snippets from being displayed
const showStateSnippets = options?.shouldShowStateSnippets || (options?.shouldShowStateSnippets === undefined && isChildOfStates(node))

if (showStateSnippets) {
return stateSnippets
}
export default function completeSnippets(
node: ASTNode | undefined,
offset: number,
options?: CompleteSnippetsOptions,
): CompletionItem[] {
if (node) {
const errorSnippetOptionsNotDefined = options?.shouldShowErrorSnippets === undefined
// If the value of shouldShowStateSnippets is false prevent the snippets from being displayed
const showStateSnippets =
options?.shouldShowStateSnippets || (options?.shouldShowStateSnippets === undefined && isChildOfStates(node))

if (showStateSnippets) {
return stateSnippets
}

if (errorSnippetOptionsNotDefined) {
if (insideStateNode(node) && doesStateSupportErrorHandling(node)) {
return errorHandlingSnippets
}
if (errorSnippetOptionsNotDefined) {
if (insideStateNode(node) && doesStateSupportErrorHandling(node)) {
return errorHandlingSnippets
}

return []
}
return []
}

const errorSnippetsToShow: string[] = []
const errorSnippetsToShow: string[] = []

if (options?.shouldShowErrorSnippets?.catch) {
errorSnippetsToShow.push('Catch')
}
if (options?.shouldShowErrorSnippets?.catch) {
errorSnippetsToShow.push('Catch')
}

if (options?.shouldShowErrorSnippets?.retry) {
errorSnippetsToShow.push('Retry')
}
if (options?.shouldShowErrorSnippets?.retry) {
errorSnippetsToShow.push('Retry')
}

if (errorSnippetsToShow.length) {
return errorHandlingSnippets.filter(snippet => errorSnippetsToShow.includes(snippet.label))
}
if (errorSnippetsToShow.length) {
return errorHandlingSnippets.filter((snippet) => errorSnippetsToShow.includes(snippet.label))
}
}

return []
return []
}
Loading

0 comments on commit 5618223

Please sign in to comment.