diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-09-20 02:18:09 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-09-20 02:18:09 +0300 |
commit | 6ed4ec3e0b1340f96b7c043ef51d1b33bbe85fde (patch) | |
tree | dc4d20fe6064752c0bd323187252c77e0a89144b /workhorse/internal/lsif_transformer/parser/code_hover.go | |
parent | 9868dae7fc0655bd7ce4a6887d4e6d487690eeed (diff) |
Add latest changes from gitlab-org/gitlab@15-4-stable-eev15.4.0-rc42
Diffstat (limited to 'workhorse/internal/lsif_transformer/parser/code_hover.go')
-rw-r--r-- | workhorse/internal/lsif_transformer/parser/code_hover.go | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/workhorse/internal/lsif_transformer/parser/code_hover.go b/workhorse/internal/lsif_transformer/parser/code_hover.go index 25550cce29e..ab3ab291432 100644 --- a/workhorse/internal/lsif_transformer/parser/code_hover.go +++ b/workhorse/internal/lsif_transformer/parser/code_hover.go @@ -28,6 +28,16 @@ type truncatableString struct { Truncated bool } +// supportedLexerLanguages is used for a fast lookup to ensure the language +// is supported by the lexer library. +var supportedLexerLanguages = map[string]struct{}{} + +func init() { + for _, name := range lexers.Names(true) { + supportedLexerLanguages[name] = struct{}{} + } +} + func (ts *truncatableString) UnmarshalText(b []byte) error { s := 0 for i := 0; s < len(b); i++ { @@ -93,6 +103,24 @@ func newCodeHover(content json.RawMessage) (*codeHover, error) { } func (c *codeHover) setTokens() { + // fastpath: bail early if no language specified + if c.Language == "" { + return + } + + // fastpath: lexer.Get() will first match against indexed languages by + // name and alias, and then fallback to a very slow filepath match. We + // avoid this slow path by first checking against languages we know to + // be within the index, and bailing if not found. + // + // Not case-folding immediately is done intentionally. These two lookups + // mirror the behaviour of lexer.Get(). + if _, ok := supportedLexerLanguages[c.Language]; !ok { + if _, ok := supportedLexerLanguages[strings.ToLower(c.Language)]; !ok { + return + } + } + lexer := lexers.Get(c.Language) if lexer == nil { return |