From 3adff68faf33c4e0acf301045eb7bd26429a9b6f Mon Sep 17 00:00:00 2001 From: Julius Riegel Date: Thu, 21 Mar 2024 12:03:03 +0100 Subject: [PATCH] Convert ide into an actual nu script --- content/config/broot/ide.toml.tmpl | 2 +- content/config/helix/languages.toml | 17 ++ .../helix/runtime/queries/nu/highlights.scm | 283 ++++++++++++++++++ content/config/nushell/aliases.nu.tmpl | 1 - content/config/nushell/config.nu.tmpl | 2 + content/config/nushell/scripts/dirs.nu.tmpl | 5 + .../nushell/scripts/ide.nu} | 51 +++- content/config/zellij/development.kdl.tmpl | 14 +- 8 files changed, 361 insertions(+), 14 deletions(-) create mode 100644 content/config/helix/runtime/queries/nu/highlights.scm create mode 100644 content/config/nushell/scripts/dirs.nu.tmpl rename content/{local-share/scripts/ide.nu.tmpl => config/nushell/scripts/ide.nu} (54%) diff --git a/content/config/broot/ide.toml.tmpl b/content/config/broot/ide.toml.tmpl index d9d2d9c..dcf1a56 100644 --- a/content/config/broot/ide.toml.tmpl +++ b/content/config/broot/ide.toml.tmpl @@ -14,7 +14,7 @@ icon_theme = "vscode" [[verbs]] key = "enter" shortcut = "e" -external = "nu {{dirs.data}}/scripts/ide.nu open-file '{file}'" +external = "nu -c \"use ide.nu; ide open-file '{file}'\"" apply_to = "file" leave_broot = false diff --git a/content/config/helix/languages.toml b/content/config/helix/languages.toml index 92b5e3a..d894371 100644 --- a/content/config/helix/languages.toml +++ b/content/config/helix/languages.toml @@ -14,6 +14,8 @@ args = [] command = "graphql-lsp" args = ["server", "-m", "stream"] +[language-server.typescript-language-server.config.preferences] +disableSuggestions = true # languages @@ -81,3 +83,18 @@ formatter = { command = "shfmt", args = ["-s", "-sr", "-"]} [[grammar]] name = "roc" source = { git = "https://github.com/faldor20/tree-sitter-roc", rev = "381743cd40ee19a9508c6445aacb9085d4bc0cf8" } + + +[[language]] +name = "nu" +scope = "source.nu" +injection-regex = "nu" +file-types = ["nu", "nuon"] +shebangs = ["nu"] +comment-token = "#" +indent = { tab-width = 2, unit = " " } +language-servers = [ "nu-lsp" ] + +[[grammar]] +name = "nu" +source = { git = "https://github.com/nushell/tree-sitter-nu", rev = "358c4f509eb97f0148bbd25ad36acc729819b9c1" } diff --git a/content/config/helix/runtime/queries/nu/highlights.scm b/content/config/helix/runtime/queries/nu/highlights.scm new file mode 100644 index 0000000..52b4882 --- /dev/null +++ b/content/config/helix/runtime/queries/nu/highlights.scm @@ -0,0 +1,283 @@ +;;; --- +;;; keywords +[ + "def" + "alias" + "export-env" + "export" + "extern" + "module" + + "let" + "let-env" + "mut" + "const" + + "hide-env" + + "source" + "source-env" + + "overlay" + "register" + + "loop" + "while" + "error" + + "do" + "if" + "else" + "try" + "catch" + "match" + + "break" + "continue" + "return" + +] @keyword + +(hide_mod "hide" @keyword) +(decl_use "use" @keyword) + +(ctrl_for + "for" @keyword + "in" @keyword +) +(overlay_list "list" @keyword.storage.modifier) +(overlay_hide "hide" @keyword.storage.modifier) +(overlay_new "new" @keyword.storage.modifier) +(overlay_use + "use" @keyword.storage.modifier + "as" @keyword +) +(ctrl_error "make" @keyword.storage.modifier) + +;;; --- +;;; literals +(val_number) @constant.numeric +(val_duration + unit: [ + "ns" "µs" "us" "ms" "sec" "min" "hr" "day" "wk" + ] @variable.parameter +) +(val_filesize + unit: [ + "b" "B" + + "kb" "kB" "Kb" "KB" + "mb" "mB" "Mb" "MB" + "gb" "gB" "Gb" "GB" + "tb" "tB" "Tb" "TB" + "pb" "pB" "Pb" "PB" + "eb" "eB" "Eb" "EB" + + "kib" "kiB" "kIB" "kIb" "Kib" "KIb" "KIB" + "mib" "miB" "mIB" "mIb" "Mib" "MIb" "MIB" + "gib" "giB" "gIB" "gIb" "Gib" "GIb" "GIB" + "tib" "tiB" "tIB" "tIb" "Tib" "TIb" "TIB" + "pib" "piB" "pIB" "pIb" "Pib" "PIb" "PIB" + "eib" "eiB" "eIB" "eIb" "Eib" "EIb" "EIB" + ] @variable.parameter +) +(val_binary + [ + "0b" + "0o" + "0x" + ] @constant.numeric + "[" @punctuation.bracket + digit: [ + "," @punctuation.delimiter + (hex_digit) @constant.number + ] + "]" @punctuation.bracket +) @constant.numeric +(val_bool) @constant.builtin +(val_nothing) @constant.builtin +(val_string) @string +(val_date) @constant.number +(inter_escape_sequence) @constant.character.escape +(escape_sequence) @constant.character.escape +(val_interpolated [ + "$\"" + "$\'" + "\"" + "\'" +] @string) +(unescaped_interpolated_content) @string +(escaped_interpolated_content) @string +(expr_interpolated ["(" ")"] @variable.parameter) + +;;; --- +;;; operators +(expr_binary [ + "+" + "-" + "*" + "/" + "mod" + "//" + "++" + "**" + "==" + "!=" + "<" + "<=" + ">" + ">=" + "=~" + "!~" + "and" + "or" + "xor" + "bit-or" + "bit-xor" + "bit-and" + "bit-shl" + "bit-shr" + "in" + "not-in" + "starts-with" + "ends-with" +] @operator ) + +(where_command [ + "+" + "-" + "*" + "/" + "mod" + "//" + "++" + "**" + "==" + "!=" + "<" + "<=" + ">" + ">=" + "=~" + "!~" + "and" + "or" + "xor" + "bit-or" + "bit-xor" + "bit-and" + "bit-shl" + "bit-shr" + "in" + "not-in" + "starts-with" + "ends-with" +] @operator) + +(assignment [ + "=" + "+=" + "-=" + "*=" + "/=" + "++=" +] @operator) + +(expr_unary ["not" "-"] @operator) + +(val_range [ + ".." + "..=" + "..<" +] @operator) + +["=>" "=" "|"] @operator + +[ + "o>" "out>" + "e>" "err>" + "e+o>" "err+out>" + "o+e>" "out+err>" +] @special + +;;; --- +;;; punctuation +[ + "," + ";" +] @punctuation.delimiter + +(param_short_flag "-" @punctuation.delimiter) +(param_long_flag ["--"] @punctuation.delimiter) +(long_flag ["--"] @punctuation.delimiter) +(param_rest "..." @punctuation.delimiter) +(param_type [":"] @punctuation.special) +(param_value ["="] @punctuation.special) +(param_cmd ["@"] @punctuation.special) +(param_opt ["?"] @punctuation.special) + +[ + "(" ")" + "{" "}" + "[" "]" +] @punctuation.bracket + +(val_record + (record_entry ":" @punctuation.delimiter)) +;;; --- +;;; identifiers +(param_rest + name: (_) @variable.parameter) +(param_opt + name: (_) @variable.parameter) +(parameter + param_name: (_) @variable.parameter) +(param_cmd + (cmd_identifier) @string) +(param_long_flag) @variable.parameter +(param_short_flag) @variable.parameter + +(short_flag) @variable.parameter +(long_flag) @variable.parameter + +(scope_pattern [(wild_card) @function]) + +(cmd_identifier) @function + +(command + "^" @punctuation.delimiter + head: (_) @function +) + +"where" @function + +(path + ["." "?"] @punctuation.delimiter +) @variable.parameter + +(val_variable + "$" @variable.parameter + [ + (identifier) @namespace + "in" + "nu" + "env" + ] @special +) +;;; --- +;;; types +(flat_type) @type.builtin +(list_type + "list" @type.enum + ["<" ">"] @punctuation.bracket +) +(collection_type + ["record" "table"] @type.enum + "<" @punctuation.bracket + key: (_) @variable.parameter + ["," ":"] @punctuation.delimiter + ">" @punctuation.bracket +) + +(shebang) @comment +(comment) @comment diff --git a/content/config/nushell/aliases.nu.tmpl b/content/config/nushell/aliases.nu.tmpl index 7c3037c..5ecaed3 100644 --- a/content/config/nushell/aliases.nu.tmpl +++ b/content/config/nushell/aliases.nu.tmpl @@ -1,4 +1,3 @@ -alias ide = {{dirs.data}}/scripts/ide.nu {{#if-installed helix}} alias hx = helix {{/if-installed}} diff --git a/content/config/nushell/config.nu.tmpl b/content/config/nushell/config.nu.tmpl index 31390fb..309cd09 100644 --- a/content/config/nushell/config.nu.tmpl +++ b/content/config/nushell/config.nu.tmpl @@ -468,3 +468,5 @@ source git.nu use ci.nu {{/if-installed}} {{/if-installed}} + +use ide.nu diff --git a/content/config/nushell/scripts/dirs.nu.tmpl b/content/config/nushell/scripts/dirs.nu.tmpl new file mode 100644 index 0000000..7b82da0 --- /dev/null +++ b/content/config/nushell/scripts/dirs.nu.tmpl @@ -0,0 +1,5 @@ +export const HOME = '{{dirs.home}}' + +export const CONFIG = '{{dirs.config}}' + +export const DATA = '{{dirs.data}}' diff --git a/content/local-share/scripts/ide.nu.tmpl b/content/config/nushell/scripts/ide.nu similarity index 54% rename from content/local-share/scripts/ide.nu.tmpl rename to content/config/nushell/scripts/ide.nu index 86c59f0..088f474 100755 --- a/content/local-share/scripts/ide.nu.tmpl +++ b/content/config/nushell/scripts/ide.nu @@ -1,26 +1,29 @@ #!/bin/env nu use task.nu +use dirs.nu -def --env main [] { +const ZELLIJ_LAYOUT = ($dirs.CONFIG | path join zellij/development.toml) + +export def --env main [] { if $env.ZELLIJ? == null { if $env.WEZTERM_PANE? != null { let name = ($env.PWD | path basename) wezterm cli set-tab-title $"IDE ($name)" } let tmpdir = init - zellij --layout {{dirs.config}}/zellij/development.toml --session (session-name) + zellij --layout $ZELLIJ_LAYOUT --session (session-name) rm -r $tmpdir if $env.WEZTERM_PANE? != null { wezterm cli set-tab-title "" } } else { - zellij action new-tab --layout {{dirs.config}}/zellij/development.toml --name (tab-name) + zellij action new-tab --layout $ZELLIJ_LAYOUT --name (tab-name) } } -export def `main open-file` [path: string] { +export def open-file [path: string] { if ($path | str starts-with "http") { /usr/bin/xdg-open $path return @@ -34,13 +37,22 @@ export def `main open-file` [path: string] { } } -export def `main floating-prompt` [] { +export def floating-prompt [] { task spawn -i { zellij action new-pane --floating -- nu -c 'nu -e clear; zellij action close-pane'; } zellij action close-pane } +export def last-notification [] { + let notifications = (open $env.NOTIFY_FILE | lines) + if ($notifications | is-empty) { + return + } + $notifications | skip 1 | save -f $env.NOTIFY_FILE + $notifications | first +} + def open-editor [path: string] { zellij ac move-focus right zellij ac write-chars $":open ($path)\r" @@ -72,10 +84,33 @@ def --env init [] { let tmpdir = mktemp -d --suffix "-ide" let tmpbin = ($tmpdir | path join "bin") mkdir $tmpbin - let new_open = ($tmpbin | path join "xdg-open") - "#!/bin/bash\nnu {{dirs.data}}/scripts/ide.nu open-file $*\n" | save -f $new_open - chmod +x $new_open $env.PATH = ($env.PATH | prepend $tmpbin) + create-xdg-open $tmpbin + create-notify-send $tmpdir $tmpbin $tmpdir } + +def create-xdg-open [tmpbin: string] { + let new_open = ($tmpbin | path join "xdg-open") + ("#!/bin/env nu + use ide.nu + def main [file: string] { + ide open-file $file + } + " | save -f $new_open) + chmod +x $new_open +} + +def --env create-notify-send [tmpdir: string, tmpbin: string] { + $env.NOTIFY_FILE = ($tmpdir | path join "notifications.txt") + touch $env.NOTIFY_FILE + + let new_ntfy = ($tmpbin | path join "notify-send") + ("#!/bin/env nu + def main [title: string, content: string] { + $\"(ansi {attr: bi})($title)(ansi reset) ($content)\\n\" | save -a $env.NOTIFY_FILE + } + " | save -f $new_ntfy) + chmod +x $new_ntfy +} diff --git a/content/config/zellij/development.kdl.tmpl b/content/config/zellij/development.kdl.tmpl index 7c64bfa..5cc422b 100644 --- a/content/config/zellij/development.kdl.tmpl +++ b/content/config/zellij/development.kdl.tmpl @@ -9,6 +9,7 @@ layout { plugin location="https://github.com/dj95/zjstatus/releases/latest/download/zjstatus.wasm" { format_left "{mode}#[fg=#181825,bg=#4b0082]{command_pwd}#[fg=#4b0082,bg=blue]{command_git_branch}#[fg=blue,bg=#181825]{tabs}" format_right "{command_cpuusage}#[fg=cyan,bg=green]{command_memusage}#[fg=green,bg=#b1bbfa]{datetime}" + format_center "{command_notify}" format_space "#[bg=#181825]" hide_frame_for_single_pane "true" @@ -28,17 +29,22 @@ layout { command_pwd_command "nu -c '$env.PWD | path relative-to $env.HOME'" command_pwd_format "#[fg=#ffffff,bg=#4b0082] {stdout} " command_pwd_interval "1" - command_pwd_interval "static" + command_pwd_rendermode "static" command_cpuusage_command "nu -c 'sys | get cpu | get cpu_usage | math avg | math round -p 2 | $\"($in) %\"'" command_cpuusage_format "#[fg=#181825,bg=cyan] {stdout} " command_cpuusage_interval "10" - command_cpuusage_interval "static" + command_cpuusage_rendermode "static" command_memusage_command "nu -c 'sys | get mem.used'" command_memusage_format "#[fg=#181825,bg=green] {stdout} " command_memusage_interval "10" - command_memusage_interval "static" + command_memusage_rendermode "static" + + command_notify_command "nu -c \"use ide.nu; ide last-notification\"" + command_notify_format "{stdout}" + command_notify_interval "5" + command_notify_rendermode "static" datetime "#[fg=#181825,bg=#b1bbfa] {format} " datetime_format "%A, %d %b %Y %H:%M" @@ -73,6 +79,6 @@ layout { keybinds { normal { bind "Ctrl f" { ToggleFocusFullscreen; } - bind "Alt p" { Run "nu" "{{dirs.data}}/scripts/ide.nu" "floating-prompt"; } + bind "Alt p" { Run "nu" "-c" "use ide.nu; ide floating-prompt"; } } }