File tree Expand file tree Collapse file tree 3 files changed +12
-5
lines changed
redisinsight/ui/src/utils/monaco Expand file tree Collapse file tree 3 files changed +12
-5
lines changed Original file line number Diff line number Diff line change @@ -5,15 +5,17 @@ import { IRedisCommand } from 'uiSrc/constants'
55
66const STRING_DOUBLE = 'string.double'
77
8+ const escapeRegex = ( str : string ) => str . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' )
9+
810export const getRedisMonarchTokensProvider = (
911 commands : IRedisCommand [ ] ,
1012) : monacoEditor . languages . IMonarchLanguage => {
1113 const commandRedisCommands = [ ...commands ]
1214 const searchCommands = remove ( commandRedisCommands , ( { token } ) =>
1315 token ?. startsWith ( ModuleCommandPrefix . RediSearch ) ,
1416 )
15- const COMMON_COMMANDS_REGEX = `^\\s*(\\d+\\s+)?(${ commandRedisCommands . map ( ( { token } ) => token ) . join ( '|' ) } )\\b`
16- const SEARCH_COMMANDS_REGEX = `^\\s*(\\d+\\s+)?(${ searchCommands . map ( ( { token } ) => token ) . join ( '|' ) } )\\b`
17+ const COMMON_COMMANDS_REGEX = `^\\s*(\\d+\\s+)?(${ commandRedisCommands . map ( ( { token } ) => escapeRegex ( token || '' ) ) . join ( '|' ) } )\\b`
18+ const SEARCH_COMMANDS_REGEX = `^\\s*(\\d+\\s+)?(${ searchCommands . map ( ( { token } ) => escapeRegex ( token || '' ) ) . join ( '|' ) } )\\b`
1719
1820 return {
1921 defaultToken : '' ,
Original file line number Diff line number Diff line change @@ -3,6 +3,9 @@ import { curryRight } from 'lodash'
33import { Maybe } from 'uiSrc/utils'
44import { IRedisCommand } from 'uiSrc/constants'
55
6+ // Escape special regex characters in tokens
7+ const escapeRegex = ( str : string ) => str . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' )
8+
69const appendToken = ( token : string , name : Maybe < string > ) =>
710 name ? `${ token } .${ name } ` : token
811export const generateQuery = (
@@ -17,7 +20,7 @@ export const generateQuery = (
1720 ) : languages . IMonarchLanguageRule =>
1821 args ?. length
1922 ? [
20- `(${ args ?. map ( ( { token } ) => token ) . join ( '|' ) } )\\b` ,
23+ `(${ args ?. map ( ( { token } ) => escapeRegex ( token || '' ) ) . join ( '|' ) } )\\b` ,
2124 { token : 'function' , next : appendTokenName ( tokenName ) } ,
2225 ]
2326 : [ / _ / , '' ]
Original file line number Diff line number Diff line change @@ -5,6 +5,8 @@ import { generateQuery } from 'uiSrc/utils/monaco/monarchTokens/redisearchTokens
55import { ICommandTokenType , IRedisCommand } from 'uiSrc/constants'
66import { DefinedArgumentName } from 'uiSrc/pages/workbench/constants'
77
8+ const escapeRegex = ( str : string ) => str . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' )
9+
810export const generateKeywords = ( commands : IRedisCommand [ ] ) =>
911 commands . map ( ( { name } ) => name )
1012export const generateTokens = (
@@ -153,7 +155,7 @@ export const getBlockTokens = (
153155
154156 if ( tokensWithNextExpression . length ) {
155157 result . push ( [
156- `(${ tokensWithNextExpression . map ( ( { token } ) => token ) . join ( '|' ) } )\\b` ,
158+ `(${ tokensWithNextExpression . map ( ( { token } ) => escapeRegex ( token || '' ) ) . join ( '|' ) } )\\b` ,
157159 {
158160 token : `argument.block.${ lvl } .${ name } ` ,
159161 next : '@query' ,
@@ -163,7 +165,7 @@ export const getBlockTokens = (
163165
164166 if ( restTokens . length ) {
165167 result . push ( [
166- `(${ restTokens . map ( ( { token } ) => token ) . join ( '|' ) } )\\b` ,
168+ `(${ restTokens . map ( ( { token } ) => escapeRegex ( token || '' ) ) . join ( '|' ) } )\\b` ,
167169 { token : `argument.block.${ lvl } .${ name } ` , next : '@root' } ,
168170 ] )
169171 }
You can’t perform that action at this time.
0 commit comments