11import { PrismEditor } from "../../index.js"
22import { Token } from "../../prism/core.js"
3- import { TokenStream } from "../../prism/types.js"
3+ import { TokenName , TokenStream } from "../../prism/types.js"
44import { updateNode } from "../../utils/local.js"
55import { matchTemplate } from "../search/search.js"
66import { map } from "./tooltip.js"
7- import { Completion , CompletionContext , CompletionSource } from "./types.js"
7+ import { Completion , CompletionContext , CompletionSource } from "./types.js"
88
99const optionsFromKeys = ( obj : object , icon ?: string ) : Completion [ ] =>
1010 Object . keys ( obj ) . map ( tag => ( { label : tag , icon } ) )
@@ -54,7 +54,7 @@ const completeFromList = (options: Completion[]): CompletionSource<{ path: strin
5454 * @param filter Function used to filter tokens you want to search in. Is called with the
5555 * type of the token and its starting position. If the filter returns true, the token
5656 * will be searched.
57- * @param pattern Pattern used to search for words.
57+ * @param pattern Pattern used to search for words. Must have the `g` flag.
5858 * @param init Words that should be completed even if they're not found in the document.
5959 * @param tokensOnly If `true` only the text of tokens whose `content` is a string will
6060 * be searched. If `false`, any string inside the {@link TokenStream} can be searched.
@@ -63,7 +63,7 @@ const completeFromList = (options: Completion[]): CompletionSource<{ path: strin
6363const findWords = (
6464 context : CompletionContext ,
6565 editor : PrismEditor ,
66- filter : ( type : string , start : number ) => boolean ,
66+ filter : ( type : TokenName , start : number ) => boolean ,
6767 pattern : RegExp ,
6868 init ?: Iterable < string > ,
6969 tokensOnly ?: boolean ,
@@ -74,37 +74,28 @@ const findWords = (
7474 const search = ( tokens : TokenStream , pos : number , isCorrectLang : boolean ) => {
7575 let i = 0
7676 let token : string | Token
77- if ( isCorrectLang ) {
78- for ( ; ( token = tokens [ i ++ ] ) ; ) {
79- if ( typeof token == "string" ) {
80- if ( ! tokensOnly ) match ( token , pos )
81- } else {
82- const type = token . type
83- const content = token . content
84- if ( ( token . alias || type ) . slice ( 0 , 9 ) != "language-" && filter ( type , pos ) ) {
85- if ( Array . isArray ( content ) ) {
86- search ( content , pos , true )
87- } else match ( content , pos )
88- }
89- }
90- pos += token . length
91- }
92- } else {
93- for ( ; ( token = tokens [ i ++ ] ) ; ) {
94- if ( typeof token != "string" ) {
95- const type = token . type
96- const content = token . content
97- if ( Array . isArray ( content ) ) {
98- const aliasType = token . alias || type
77+
78+ for ( ; ( token = tokens [ i ++ ] ) ; ) {
79+ if ( typeof token == "string" ) {
80+ if ( ! tokensOnly && isCorrectLang ) match ( token , pos )
81+ } else {
82+ const type = token . type
83+ const content = token . content
84+ const aliasType = token . alias || type
85+
86+ if ( Array . isArray ( content ) ) {
87+ if ( ! isCorrectLang || filter ( type , pos ) ) {
9988 search (
10089 content ,
10190 pos ,
102- aliasType . slice ( 0 , 9 ) == "language-" ? definition == map [ aliasType . slice ( 9 ) ] : false ,
91+ aliasType . slice ( 0 , 9 ) == "language-"
92+ ? definition == map [ aliasType . slice ( 9 ) ]
93+ : isCorrectLang ,
10394 )
10495 }
105- }
106- pos += token . length
96+ } else if ( isCorrectLang && filter ( type , pos ) ) match ( content , pos )
10797 }
98+ pos += token . length
10899 }
109100 }
110101 const match = ( token : string , pos : number ) => {
0 commit comments