aboutsummaryrefslogtreecommitdiff
path: root/plugins/language_nix.lua
blob: 149c2fa05021596cf8283d8908837da8b4d3a926 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
-- mod-version:3
-- https://nixos.wiki/wiki/Overview_of_the_Nix_Language
local syntax = require "core.syntax"

local function merge_tables(a, b)
  for _, v in pairs(b) do
    table.insert(a, v)
  end
end

local default_symbols = {
  ["import"]   = "keyword2",
  ["with"]     = "keyword2",
  ["builtins"] = "keyword2",
  ["inherit"]  = "keyword2",
  ["assert"]   = "keyword2",
  ["let"]      = "keyword2",
  ["in"]       = "keyword2",
  ["rec"]      = "keyword2",
  ["if"]       = "keyword",
  ["else"]     = "keyword",
  ["then"]     = "keyword",
  ["true"]     = "literal",
  ["false"]    = "literal",
  ["null"]     = "literal",
}

local default_patterns = {}

local string_interpolation = {
  { pattern = {"%${", "}"}, type = "keyword2", syntax = {
    patterns = default_patterns,
    symbols = default_symbols,
  }},
  { pattern = "[%S][%w]*", type = "string" },
}

merge_tables(default_patterns, {
  { pattern = "#.*",          type = "comment" },
  { pattern = {"/%*", "%*/"}, type = "comment" },
  { pattern = "-?%.?%d+",     type = "number"  },

  -- interpolation
  { pattern = {"%${", "}"}, type = "keyword2", syntax = {
    patterns = default_patterns,
    symbols = default_symbols,
  }},
  { pattern = {'"', '"', '\\'}, type = "string", syntax = {
    patterns = string_interpolation,
    symbols = {},
  }},
  { pattern = {"''", "''"}, type = "string", syntax = {
    patterns = string_interpolation,
    symbols = {},
  }},

  -- operators
  { pattern = "[%+%-%?!>%*]", type = "operator" },
  { pattern = "/ ",           type = "operator" },
  { pattern = "< ",           type = "operator" },
  { pattern = "//",           type = "operator" },
  { pattern = "&&",           type = "operator" },
  { pattern = "%->",          type = "operator" },
  { pattern = "||",           type = "operator" },
  { pattern = "==",           type = "operator" },
  { pattern = "!=",           type = "operator" },
  { pattern = ">=",           type = "operator" },
  { pattern = "<=",           type = "operator" },

  -- paths (function because its not used otherwise)
  { pattern = "%.?%.?/[^%s%[%]%(%){};,:]+", type = "function" },
  { pattern = "~/[^%s%[%]%(%){};,:]+",      type = "function" },
  { pattern = {"<", ">"},                   type = "function" },

  -- every other symbol
  { pattern = "[%a%-%_][%w%-%_]*", type = "symbol" },
  { pattern = ";%.,:",             type = "normal" },
})

syntax.add {
  name = "Nix",
  files = {"%.nix$"},
  comment = "#",
  block_comment = {"/*", "*/"},
  patterns = default_patterns,
  symbols = default_symbols,
}