import { Block , BlockFactory } from "@css-blocks/core" ; import { CompletionItem , CompletionItemKind , Position , TextDocument } from "vscode-languageserver-types" ; import { PathTransformer } from "../pathTransformers/PathTransformer" ; import { AttributeType , ClassAttribute , getItemAtCursor } from "./hbsUtils" ; import { transformPathsFromUri } from "./pathTransformer" ; export async function getHbsCompletions ( document , position , blockFactory , pathTransformer ) { let transformedPaths = transformPathsFromUri ( document . uri , pathTransformer , blockFactory . configuration ) ; let { blockFsPath } = transformedPaths ; if ( ! blockFsPath ) { return [ ] ; } try { let block = await blockFactory . getBlockFromPath ( blockFsPath ) ; let itemAtCursor = getItemAtCursor ( document . getText ( ) , position ) ; if ( ! ( itemAtCursor && block ) ) { return [ ] ; } let attributeAtCursor = itemAtCursor . attribute ; if ( attributeAtCursor . referencedBlock ) { block = block . getExportedBlock ( attributeAtCursor . referencedBlock ) ; } if ( ! block ) { return [ ] ; } if ( attributeAtCursor . attributeType === AttributeType . ambiguous ) { let completions = [ { label : "block:" , kind : CompletionItemKind . Property , } , ] ; block . eachBlockExport ( ( name ) => { completions . push ( { label : `${ name }:` , kind : CompletionItemKind . Property , } ) ; } ) ; return completions ; } if ( attributeAtCursor . attributeType === AttributeType . class ) { return block . classes . filter ( blockClass => ! blockClass . isRoot ) . map ( blockClass => { return { label : blockClass . name , kind : CompletionItemKind . Property , } ; } ) ; } if ( attributeAtCursor . attributeType === AttributeType . state ) { let completions = [ ] ; if ( "class" . startsWith ( attributeAtCursor . name ) ) { let siblingClass = itemAtCursor . siblingAttributes . find ( ( attr ) => attr . referencedBlock === attributeAtCursor . referencedBlock ) ; if ( ! siblingClass ) { completions . push ( { label : "class" , kind : CompletionItemKind . Property , } ) ; } } if ( "scope" . startsWith ( attributeAtCursor . name ) ) { if ( attributeAtCursor . referencedBlock ) { completions . push ( { label : "scope" , kind : CompletionItemKind . Property , } ) ; } } if ( itemAtCursor . siblingAttributes && itemAtCursor . siblingAttributes . length ) { itemAtCursor . siblingAttributes . forEach ( classAttribute => { if ( block && classAttribute . referencedBlock === attributeAtCursor . referencedBlock ) { if ( classAttribute . name ) { const blockClass = block . getClass ( classAttribute . name ) ; if ( blockClass ) { const attributes = blockClass . getAttributes ( ) ; completions = completions . concat ( attributes . map ( ( attr ) : CompletionItem => { return { label : attr . name , kind : CompletionItemKind . Property , } ; } ) ) ; } } } } ) ; } return completions ; } return [ ] ; } catch ( e ) { return [ ] ; } }