vim9script import autoload "kg8m/configure/filetypes/javascript.vim" as jsConfig import autoload "kg8m/events.vim" import autoload "kg8m/util/matchpairs.vim" var queue_on_post_source: list export def OnSource(): void g:lexima_enable_endwise_rules = false enddef export def OnPostSource(): void queue_on_post_source = [ # Delay for performance. () => AddCommonRules(), () => AddRulesForEruby(), () => AddRulesForHtml(), () => AddRulesForJs(), () => AddRulesForTs(), () => AddRulesForMarkdown(), () => AddRulesForShell(), () => AddRulesForVim(), # Overwrite lexima.vim's default mapping. () => events.NotifyInsertModePluginLoaded(), ] DequeueOnPostSource() enddef def AddCommonRules(): void for pair in matchpairs.JapanesePairs() # `「` when # # | # # then # # 「|」 lexima#add_rule({ char: pair[0], input_after: pair[1] }) # `」` when # # |」 # # then # # 」| lexima#add_rule({ char: pair[1], at: $'\%#{pair[1]}', leave: 1 }) # `` when # # 「|」 # # then # # | # lexima#add_rule({ char: "", at: join(pair, '\%#'), delete: 1 }) endfor # `"` when # # "foo| # # or # # 'foo| # # then # # "foo"| # # or # # 'foo'| lexima#add_rule({ char: '"', except: '\%#"', syntax: "String" }) lexima#add_rule({ char: "'", except: "\\%#'", syntax: "String" }) enddef def AddRulesForEruby(): void # `%` when # # <| # # then # # <%| %> lexima#add_rule({ char: "%", at: '<\%#', input_after: "%>", filetype: "eruby" }) enddef def AddRulesForHtml(): void # JavaScript and TypeScript for html`...` const filetypes = [ "eruby", "html", "markdown", "vue", ] + jsConfig.JS_FILETYPES + jsConfig.TS_FILETYPES # `` when # # | # # then # # # | # lexima#add_rule({ char: "", at: '>\%#", filetype: filetypes }) # `-` when # # lexima#add_rule({ char: "-", at: '", input_after: "-->", filetype: filetypes }) enddef def AddRulesForJs(): void const filetypes = jsConfig.JS_FILETYPES + jsConfig.TS_FILETYPES # `` when # # `|` # # then # # ` # | # ` lexima#add_rule({ char: "", at: '`\%#`', input_after: "", filetype: filetypes }) enddef def AddRulesForTs(): void const filetypes = ["typescript"] # `<` when # # Foo| # # then # # Foo<|> lexima#add_rule({ char: "<", at: '\w\%#', input_after: ">", filetype: filetypes }) # `<` when # # Foo|< # # then # # Foo<| # # NOTE: Use `input: ""` because `leave: 1` doesn't work. lexima#add_rule({ char: "<", at: '\w\%#<', input: "U", filetype: filetypes }) # `<` when # # |( # # then # # <|>( lexima#add_rule({ char: "<", at: '\%#(', input_after: ">", filetype: filetypes }) # `>` when # # # # then # # | # # NOTE: Use `input: ""` because `leave: 1` doesn't work. lexima#add_rule({ char: ">", at: '\%#>', input: "U", filetype: filetypes }) # `` when # # <|> # # then # # | # lexima#add_rule({ char: "", at: '<\%#>', delete: 1, filetype: filetypes }) enddef def AddRulesForMarkdown(): void const filetypes = ["gitcommit", "markdown"] # `` when # # - [|] # * [|] # 1. [|] # # then # # - [ ] | # * [ ] | # 1. [ ] | lexima#add_rule({ char: "", at: '\v^\s*%([-*]|\d+\.)\s+\[%#\]', input: "U", filetype: filetypes }) for pattern in ['\*', '-', '\d+\.'] # `` when # # * | # - | # 1. | # 2. | # * [ ] | # - [ ] | # # then # # | lexima#add_rule({ char: "", at: $'\v^\s*{pattern}\s(\[[x ]\]\s)?%#$', input: $"0d$", filetype: filetypes }) endfor # `` when # # ```|``` # # then # # ``` # | # ``` lexima#add_rule({ char: "", at: '```\%#```', input_after: "", filetype: filetypes }) # `` when # # ```foo|``` # # then # # ```foo # | # ``` lexima#add_rule({ char: "", at: '```[a-z]\+\%#```', input_after: "", filetype: filetypes }) # ``` when # # `foo| # # then # # `foo`| lexima#add_rule({ char: "`", except: '\%#`\|``\%#', syntax: ["mkdCode", "mkdInlineCodeDelimiter"] }) enddef def AddRulesForShell(): void const filetypes = ["sh", "zsh"] # `` when # # (|) # # then # # ( # | # ) lexima#add_rule({ char: "", at: '(\%#)', input: '', filetype: filetypes }) # `` when # # $(|) # # then # # $( # | # ) lexima#add_rule({ char: "", at: '\$(\%#)', input: '', filetype: filetypes }) enddef def AddRulesForVim(): void const filetypes = ["vim"] # `"` when # # ...| # # or # # ...|" # # then # # ..."|" # # or # # ..."| # # NOTE: Always write comments at the beginning of line (indentation is allowed). lexima#add_rule({ char: '"', at: '\S.*\%#\%([^"]\|$\)', except: '\%#"', input_after: '"', filetype: filetypes }) lexima#add_rule({ char: '"', at: '\%#"', leave: 1, filetype: filetypes }) # `<` when # # foo| # # then # # foo<|> lexima#add_rule({ char: "<", at: '\w\%#', input_after: ">", filetype: filetypes }) # `<` when # # foo|< # # then # # foo<| # # NOTE: Use `input: ""` because `leave: 1` doesn't work. lexima#add_rule({ char: "<", at: '\w\%#<', input: "U", filetype: filetypes }) # `>` when # # # # then # # | # # NOTE: Use `input: ""` because `leave: 1` doesn't work. lexima#add_rule({ char: ">", at: '\%#>', input: "U", filetype: filetypes }) # `` when # # <|> # # then # # | # lexima#add_rule({ char: "", at: '<\%#>', delete: 1, filetype: filetypes }) for pair in [['(', ')'], ['[', ']'], ['{', '}']] # `` when # # (|) # [|] # {|} # # or # # vim9script # # (|) # [|] # {|} # # then # # ( # \ | # \ ) # # [ # \ | # \ ] # # { # \ | # \ } # # or # # vim9script # # ( # | # ) # # [ # | # ] # # { # | # } lexima#add_rule({ char: "", at: $'\V{pair[0]}\%#{pair[1]}', input: '=kg8m#plugin#lexima#ExprOnCrInEmptyParenthesesForVimScript()', filetype: filetypes }) endfor enddef export def ExprOnCrInEmptyParenthesesForVimScript(): string if search('^vim9s\%[cript]\>', "bnW") return "\\\" else const pseudo_indentation = matchstr(getline("."), '\v^\s*\\ \zs +\ze') return $"\\\ {pseudo_indentation}\\\\ {pseudo_indentation} " endif enddef def DequeueOnPostSource(): void if !empty(queue_on_post_source) const Callback = remove(queue_on_post_source, 0) timer_start(50, (_) => CallbackProxyOnPostSource(Callback)) endif enddef def CallbackProxyOnPostSource(Callback: func): void Callback() DequeueOnPostSource() enddef