gitea/vendor/github.com/alecthomas/chroma/lexers/n/nix.go
mrsdizzie af7ffaa279
Server-side syntax highlighting for all code (#12047)
* Server-side syntax hilighting for all code

This PR does a few things:

* Remove all traces of highlight.js
* Use chroma library to provide fast syntax hilighting directly on the server
* Provide syntax hilighting for diffs
* Re-style both unified and split diffs views
* Add custom syntax hilighting styling for both regular and arc-green

Fixes #7729
Fixes #10157
Fixes #11825
Fixes #7728
Fixes #3872
Fixes #3682

And perhaps gets closer to #9553

* fix line marker

* fix repo search

* Fix single line select

* properly load settings

* npm uninstall highlight.js

* review suggestion

* code review

* forgot to call function

* fix test

* Apply suggestions from code review

suggestions from @silverwind thanks

Co-authored-by: silverwind <me@silverwind.io>

* code review

* copy/paste error

* Use const for highlight size limit

* Update web_src/less/_repository.less

Co-authored-by: Lauris BH <lauris@nix.lv>

* update size limit to 1MB and other styling tweaks

* fix highlighting for certain diff sections

* fix test

* add worker back as suggested

Co-authored-by: silverwind <me@silverwind.io>
Co-authored-by: Lauris BH <lauris@nix.lv>
2020-07-01 00:34:03 +03:00

122 lines
3.5 KiB
Go

package n
import (
"strings"
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// nixb matches right boundary of a nix word. Use it instead of \b.
const nixb = `(?![a-zA-Z0-9_'-])`
// Nix lexer.
var Nix = internal.Register(MustNewLexer(
&Config{
Name: "Nix",
Aliases: []string{"nixos", "nix"},
Filenames: []string{"*.nix"},
MimeTypes: []string{"text/x-nix"},
},
Rules{
"root": {
Include("keywords"),
Include("builtins"),
// "./path" and ".float" literals have to be above "." operator
Include("literals"),
Include("operators"),
{`#.*$`, CommentSingle, nil},
{`/\*`, CommentMultiline, Push("comment")},
{`\(`, Punctuation, Push("paren")},
{`\[`, Punctuation, Push("list")},
{`"`, StringDouble, Push("qstring")},
{`''`, StringSingle, Push("istring")},
{`{`, Punctuation, Push("scope")},
{`let` + nixb, Keyword, Push("scope")},
Include("id"),
Include("space"),
},
"keywords": {
{`import` + nixb, KeywordNamespace, nil},
{Words(``, nixb, strings.Fields("rec inherit with if then else assert")...), Keyword, nil},
},
"builtins": {
{`throw` + nixb, NameException, nil},
{Words(``, nixb, strings.Fields("abort baseNameOf builtins currentTime dependencyClosure derivation dirOf fetchTarball filterSource getAttr getEnv hasAttr isNull map removeAttrs toString toXML")...), NameBuiltin, nil},
},
"literals": {
{Words(``, nixb, strings.Fields("true false null")...), NameConstant, nil},
Include("uri"),
Include("path"),
Include("int"),
Include("float"),
},
"operators": {
{` [/-] `, Operator, nil},
{`(\.)(\${)`, ByGroups(Operator, StringInterpol), Push("interpol")},
{`(\?)(\s*)(\${)`, ByGroups(Operator, Text, StringInterpol), Push("interpol")},
{Words(``, ``, strings.Fields("@ . ? ++ + != ! // == && || -> <= < >= > *")...), Operator, nil},
{`[;:]`, Punctuation, nil},
},
"comment": {
{`\*/`, CommentMultiline, Pop(1)},
{`.|\n`, CommentMultiline, nil},
},
"paren": {
{`\)`, Punctuation, Pop(1)},
Include("root"),
},
"list": {
{`\]`, Punctuation, Pop(1)},
Include("root"),
},
"qstring": {
{`"`, StringDouble, Pop(1)},
{`\${`, StringInterpol, Push("interpol")},
{`\\.`, StringEscape, nil},
{`.|\n`, StringDouble, nil},
},
"istring": {
{`''\$`, StringEscape, nil}, // "$"
{`'''`, StringEscape, nil}, // "''"
{`''\\.`, StringEscape, nil}, // "\."
{`''`, StringSingle, Pop(1)},
{`\${`, StringInterpol, Push("interpol")},
// The next rule is important: "$" escapes any symbol except "{"!
{`\$.`, StringSingle, nil}, // "$."
{`.|\n`, StringSingle, nil},
},
"scope": {
{`}:`, Punctuation, Pop(1)},
{`}`, Punctuation, Pop(1)},
{`in` + nixb, Keyword, Pop(1)},
{`\${`, StringInterpol, Push("interpol")},
Include("root"), // "==" has to be above "="
{Words(``, ``, strings.Fields("= ? ,")...), Operator, nil},
},
"interpol": {
{`}`, StringInterpol, Pop(1)},
Include("root"),
},
"id": {
{`[a-zA-Z_][a-zA-Z0-9_'-]*`, Name, nil},
},
"uri": {
{`[a-zA-Z][a-zA-Z0-9+.-]*:[a-zA-Z0-9%/?:@&=+$,_.!~*'-]+`, StringDoc, nil},
},
"path": {
{`[a-zA-Z0-9._+-]*(/[a-zA-Z0-9._+-]+)+`, StringRegex, nil},
{`~(/[a-zA-Z0-9._+-]+)+/?`, StringRegex, nil},
{`<[a-zA-Z0-9._+-]+(/[a-zA-Z0-9._+-]+)*>`, StringRegex, nil},
},
"int": {
{`-?[0-9]+` + nixb, NumberInteger, nil},
},
"float": {
{`-?(([1-9][0-9]*\.[0-9]*)|(0?\.[0-9]+))([Ee][+-]?[0-9]+)?` + nixb, NumberFloat, nil},
},
"space": {
{`[ \t\r\n]+`, Text, nil},
},
},
))