From 813fa01bfefc24f6206c817303a2f348a9ce87ba Mon Sep 17 00:00:00 2001 From: "Douglas M." Date: Fri, 8 Mar 2024 12:29:13 +0100 Subject: [PATCH] refactor: rewrite in typescript --- .gitignore | 1 + Makefile | 34 +- devbox.json | 20 +- fnl/forem-nvim/api.fnl | 98 - fnl/forem-nvim/article.fnl | 30 - fnl/forem-nvim/buffer.fnl | 111 - fnl/forem-nvim/init.fnl | 90 - fnl/forem-nvim/notify.fnl | 12 - fnl/forem-nvim/picker.fnl | 50 - fnl/forem-nvim/util.fnl | 56 - lua/forem-nvim/api.lua | 191 +- lua/forem-nvim/article.lua | 26 +- lua/forem-nvim/buffer.lua | 189 +- lua/forem-nvim/feed.lua | 137 + lua/forem-nvim/init.lua | 192 +- lua/forem-nvim/notify.lua | 19 +- lua/forem-nvim/picker.lua | 88 +- lua/forem-nvim/util.lua | 150 +- lua/lualib_bundle.lua | 2629 ++++++++++++++ package-lock.json | 198 + package.json | 22 + plugin/forem-nvim.fnl | 12 - plugin/forem-nvim.lua | 62 +- plugin/forem-nvim.ts | 34 + plugin/lualib_bundle.lua | 2629 ++++++++++++++ plugin/tsconfig.json | 9 + scripts/fennel | 6000 ------------------------------- src/forem-nvim/api.ts | 142 + src/forem-nvim/article.ts | 36 + src/forem-nvim/buffer.ts | 59 + src/forem-nvim/feed.ts | 163 + src/forem-nvim/init.ts | 124 + src/forem-nvim/notify.ts | 9 + src/forem-nvim/picker.ts | 66 + src/forem-nvim/util.ts | 67 + src/types/_G.d.ts | 3 + src/types/api.d.ts | 2010 +++++++++++ src/types/fn.d.ts | 19 + src/types/forem-nvim.d.ts | 15 + src/types/global.d.ts | 694 ++++ src/types/highlight.d.ts | 15 + src/types/keymap.d.ts | 24 + src/types/lsp.d.ts | 1230 +++++++ src/types/luassert.d.ts | 46 + src/types/plenary.d.ts | 19 + src/types/telescope.d.ts | 39 + tests/forem-nvim/setup_spec.fnl | 38 - tests/forem-nvim/setup_spec.lua | 115 +- tests/forem-nvim/setup_spec.ts | 86 + tests/minimal_init.fnl | 20 - tests/minimal_init.lua | 35 +- tests/minimal_init.ts | 32 + tests/tsconfig.json | 20 + tsconfig.json | 20 + 54 files changed, 11137 insertions(+), 7098 deletions(-) delete mode 100644 fnl/forem-nvim/api.fnl delete mode 100644 fnl/forem-nvim/article.fnl delete mode 100644 fnl/forem-nvim/buffer.fnl delete mode 100644 fnl/forem-nvim/init.fnl delete mode 100644 fnl/forem-nvim/notify.fnl delete mode 100644 fnl/forem-nvim/picker.fnl delete mode 100644 fnl/forem-nvim/util.fnl create mode 100644 lua/forem-nvim/feed.lua create mode 100644 lua/lualib_bundle.lua create mode 100644 package-lock.json create mode 100644 package.json delete mode 100644 plugin/forem-nvim.fnl create mode 100644 plugin/forem-nvim.ts create mode 100644 plugin/lualib_bundle.lua create mode 100644 plugin/tsconfig.json delete mode 100755 scripts/fennel create mode 100644 src/forem-nvim/api.ts create mode 100644 src/forem-nvim/article.ts create mode 100644 src/forem-nvim/buffer.ts create mode 100644 src/forem-nvim/feed.ts create mode 100644 src/forem-nvim/init.ts create mode 100644 src/forem-nvim/notify.ts create mode 100644 src/forem-nvim/picker.ts create mode 100644 src/forem-nvim/util.ts create mode 100644 src/types/_G.d.ts create mode 100644 src/types/api.d.ts create mode 100644 src/types/fn.d.ts create mode 100644 src/types/forem-nvim.d.ts create mode 100644 src/types/global.d.ts create mode 100644 src/types/highlight.d.ts create mode 100644 src/types/keymap.d.ts create mode 100644 src/types/lsp.d.ts create mode 100644 src/types/luassert.d.ts create mode 100644 src/types/plenary.d.ts create mode 100644 src/types/telescope.d.ts delete mode 100644 tests/forem-nvim/setup_spec.fnl create mode 100644 tests/forem-nvim/setup_spec.ts delete mode 100644 tests/minimal_init.fnl create mode 100644 tests/minimal_init.ts create mode 100644 tests/tsconfig.json create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore index 8550a25..7258c4b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ vendor/plenary.nvim .DS_Store /.envrc +node_modules diff --git a/Makefile b/Makefile index bc2d68d..8b113e6 100644 --- a/Makefile +++ b/Makefile @@ -2,30 +2,20 @@ FENNEL_SCRIPT=scripts/fennel TESTS_INIT=tests/minimal_init.lua TESTS_DIR=tests/ -src_files = $(wildcard fnl/**/*.fnl) -out_files = $(src_files:fnl/%.fnl=lua/%.lua) -plugin_files = $(wildcard plugin/*.fnl) -plugin_out_files = $(plugin_files:plugin/%.fnl=plugin/%.lua) -test_init= $(wildcard tests/*.fnl) -test_files =$(wildcard tests/**/*.fnl) -test_out_files = $(test_files:tests/%.fnl=tests/%.lua) -test_out_init = $(test_init:tests/%.fnl=tests/%.lua) - -compile: $(out_files) $(plugin_out_files) - -lua/%.lua: fnl/%.fnl lua/ - $(FENNEL_SCRIPT) --compile $< > $@ - -plugin/%.lua: plugin/%.fnl plugin/ - $(FENNEL_SCRIPT) --compile $< > $@ - -lua/: +plugin_files = $(wildcard plugin/*.ts) +plugin_out_files = $(plugin_files:plugin/%.ts=plugin/%.lua) +test_init= $(wildcard tests/*.ts) +test_files =$(wildcard tests/**/*.ts) +test_out_files = $(test_files:tests/%.ts=tests/%.lua) +test_out_init = $(test_init:tests/%.ts=tests/%.lua) + +compile: mkdir -p lua/forem-nvim + npx tstl + npx tstl -p plugin/tsconfig.json + npx tstl -p tests/tsconfig.json -tests/%.lua: tests/%.fnl tests/ - $(FENNEL_SCRIPT) --compile $< > $@ - -test: $(test_out_files) $(test_out_init) +test: @nvim \ --headless \ --noplugin \ diff --git a/devbox.json b/devbox.json index e2ccc97..aa60e3e 100644 --- a/devbox.json +++ b/devbox.json @@ -1,12 +1,12 @@ { - "packages": [ - "github:NixOS/nixpkgs/nixpkgs-unstable#fnlfmt", - "github:NixOS/nixpkgs/nixpkgs-unstable#fennel-ls", - "github:NixOS/nixpkgs/nixpkgs-unstable#lua-language-server", - "github:NixOS/nixpkgs/nixpkgs-unstable#stylua" - ], - "shell": { - "init_hook": [], - "scripts": {} - } + "packages": [ + "github:NixOS/nixpkgs/nixpkgs-unstable#nodejs_21", + "github:NixOS/nixpkgs/nixpkgs-unstable#nodePackages.typescript-language-server", + "github:NixOS/nixpkgs/nixpkgs-unstable#lua-language-server", + "github:NixOS/nixpkgs/nixpkgs-unstable#stylua" + ], + "shell": { + "init_hook": [], + "scripts": {} + } } diff --git a/fnl/forem-nvim/api.fnl b/fnl/forem-nvim/api.fnl deleted file mode 100644 index 1ee6f8d..0000000 --- a/fnl/forem-nvim/api.fnl +++ /dev/null @@ -1,98 +0,0 @@ -(local curl (require :plenary.curl)) -(local notify (require :forem-nvim.notify)) -(local job (require :plenary.job)) -(local M {}) -(local base-url "https://dev.to/api") - -(λ key [] - vim.env.FOREM_API_KEY) - -(λ handle-async-error [response] - (notify.error (.. "Error: " response.body.error))) - -(λ get-article-template [title] - (string.format "--- -title: %s -published: false -description: -tags: -# cover_image: https://direct_url_to_image.jpg -# Use a ratio of 100:42 for best results. ---- - -" title)) - -(λ request [request-fun endpoint options] - (let [parameters (vim.tbl_extend :force - {:url (.. base-url endpoint) - :headers {:api-key (key) - :content_type :application/json - :accept :application/vnd.forem.api-v1+json}} - options) - response (request-fun parameters)] - (if response.body - (vim.tbl_extend :force response - {:body (vim.fn.json_decode response.body)}) - response))) - -(λ request-async [method endpoint options on-success ?on-error] - (: (job:new {:command :curl - :args [:-X - method - :-H - "Content-Type: application/json" - :-H - "Accept: application/vnd.forem.api-v1+json" - :-H - (.. "api-key: " (key)) - :-d - (vim.fn.json_encode options) - (.. base-url endpoint)] - :on_exit (fn [this code] - (vim.schedule #(let [results (this:result) - result (vim.fn.join results "\n") - response (vim.fn.json_decode result)] - (if (= code 0) - (on-success response) - (do - (handle-async-error response) - (when ?on-error - (?on-error response)))))))}) - :start)) - -(λ get [endpoint on-sucess ?on-error] - (request-async :GET endpoint {} on-sucess ?on-error)) - -(λ put [endpoint body] - (request curl.put endpoint {: body})) - -(λ post [endpoint body] - (request curl.post endpoint {: body})) - -(λ M.my-articles [on-success ?on-error] - (get :/articles/me/all on-success ?on-error)) - -(λ M.save-article [id content] - (put (.. :/articles/ id) - (vim.fn.json_encode {:article {:body_markdown content}}))) - -(λ M.new-article [title] - (post :/articles - (vim.fn.json_encode {:article {:body_markdown (get-article-template title)}}))) - -(λ M.feed [on-success ?on-error] - (get :/articles on-success ?on-error)) - -(λ M.get-article [id on-success ?on-error] - (get (.. :/articles/ id) on-success ?on-error)) - -(λ M.get-article-by-path [path on-success ?on-error] - (get (.. :/articles/ path) on-success ?on-error)) - -(λ M.handle-api-error [response on-success] - (let [start-status (-> response.status (tostring) (string.sub 1 2))] - (if (not= start-status :20) - (notify.error (.. "Error: " response.body.error)) - (on-success response)))) - -M diff --git a/fnl/forem-nvim/article.fnl b/fnl/forem-nvim/article.fnl deleted file mode 100644 index 2c7d293..0000000 --- a/fnl/forem-nvim/article.fnl +++ /dev/null @@ -1,30 +0,0 @@ -(local M {}) -(local {: get-plural} (require :forem-nvim.util)) - -(local new-line "\n") - -(λ M.get-body-lines [article] - (vim.split article.body_markdown new-line)) - -(λ tags-to-str [tags] - (-> (vim.tbl_map (fn [tag] - (.. "#" tag)) tags) - (vim.fn.join ", "))) - -(λ M.format-to-feed [article max-columns] - [(.. "🭽" (string.rep "▔" max-columns) "🭾") - (.. " ## " article.title) - (.. " " article.description) - (.. " 👤" article.user.name " (" article.user.username ")") - "▏" - (.. " 🕒 " article.reading_time_minutes " " - (get-plural article.reading_time_minutes :minute) " of reading time") - (.. " Tags: " (tags-to-str article.tag_list)) - (.. " 💕" (tostring article.positive_reactions_count) " 💬" - (tostring article.comments_count)) - (.. " 📆" article.readable_publish_date) - "▏" - (.. "🭼" (string.rep "▁" max-columns) "🭿") - ""]) - -M diff --git a/fnl/forem-nvim/buffer.fnl b/fnl/forem-nvim/buffer.fnl deleted file mode 100644 index a3205a4..0000000 --- a/fnl/forem-nvim/buffer.fnl +++ /dev/null @@ -1,111 +0,0 @@ -(local api (require :forem-nvim.api)) -(local Article (require :forem-nvim.article)) -(local notify (require :forem-nvim.notify)) -(local util (require :forem-nvim.util)) -(local {: fold : set-locals : set-local} (require :forem-nvim.util)) -(local M {}) - -(λ set-basic-options [] - (set-locals [[:filetype :markdown] [:modified false]])) - -(λ set-basic-options-and [opt-vals] - (set-basic-options) - (set-locals opt-vals)) - -(λ set-feed-basic-options [] - (set-basic-options-and [[:modifiable false] - [:spell false] - [:buftype :nowrite] - [:swapfile false]])) - -(λ M.write [bufnr text ?init] - (let [modifiable (vim.opt_local.modifiable:get)] - (set-local :modifiable true) - (vim.api.nvim_buf_set_lines bufnr (or ?init 0) -1 false text) - (set-local :modifiable modifiable))) - -(λ M.get-content [] - (let [bufnr (vim.api.nvim_get_current_buf)] - (values (-> (vim.api.nvim_buf_get_lines bufnr 0 -1 1) - (vim.fn.join "\n")) bufnr))) - -(λ M.open-my-article [article] - (vim.cmd (string.format ":edit forem://my-article/%s" article.id)) - (let [bufnr (vim.api.nvim_get_current_buf) - article-body (Article.get-body-lines article)] - (M.write bufnr article-body)) - (set-basic-options-and [[:buftype :acwrite] [:swapfile false]])) - -(λ seek-feed-title [line-number get-next-line-number count] - (let [line-content (vim.fn.getline line-number) - ?title (string.match line-content " ## (.+)" 1)] - (if ?title - ?title - (> count 1000) - (notify.error "Could not find title") - (seek-feed-title (get-next-line-number line-number) - get-next-line-number (+ count 1))))) - -(λ get-card-title [line-number] - (let [line-content (vim.fn.getline line-number) - is-card-upper-border? (string.match line-content "🭽" 1) - is-out-of-card? (not (string.match line-content "^[ |🭽|▏|🭼]" 1))] - (if is-out-of-card? - nil - (seek-feed-title line-number - (if is-card-upper-border? - (fn [n] - (+ n 1)) - (fn [n] - (- n 1))) 0)))) - -(λ open-feed-article [location] - (let [title (get-card-title (vim.fn.line ".")) - ?article-data (. _G.forem-feed-articles title)] - (when title - (if ?article-data - (if (= location :browser) - (util.open-browser-url ?article-data.url) - (api.get-article ?article-data.id M.open-article)) - (notify.error "Could not find article data. Please reopen the feed."))))) - -(λ M.load-feed [] - (set-feed-basic-options) - (local bufnr (vim.api.nvim_get_current_buf)) - (M.write bufnr ["Loading feed..."]) - (api.feed (fn [articles] - (vim.keymap.set :n : #(open-feed-article :buffer) - {:buffer true :silent true}) - (vim.keymap.set :n : #(open-feed-article :browser) - {:buffer true :silent true}) - (let [max-columns (fold (fn [article total] - (vim.fn.max [(length article.title) - (length article.description) - total])) - 0 articles) - feed (->> articles - (vim.tbl_map (fn [article] - (Article.format-to-feed article - max-columns))) - (vim.tbl_flatten))] - (set _G.forem-feed-articles {}) - (each [_ article (pairs articles)] - (tset _G.forem-feed-articles article.title - {:id article.id :url article.url})) - (M.write bufnr ["# Your Feed" - "" - "Press in a card to open the article in a new buffer" - "and to open it in the browser." - ""]) - (M.write bufnr feed 5))))) - -(λ M.open-article [article] - (vim.cmd (string.format ":edit forem://article/%s" article.title)) - (let [bufnr (vim.api.nvim_get_current_buf) - article-body (Article.get-body-lines article)] - (set-local :linebreak true) - (set-local :textwidth 80) - (M.write bufnr article-body)) - (set-feed-basic-options)) - -M diff --git a/fnl/forem-nvim/init.fnl b/fnl/forem-nvim/init.fnl deleted file mode 100644 index 63566fd..0000000 --- a/fnl/forem-nvim/init.fnl +++ /dev/null @@ -1,90 +0,0 @@ -(local api (require :forem-nvim.api)) -(local buffer (require :forem-nvim.buffer)) -(local notify (require :forem-nvim.notify)) -(local picker (require :forem-nvim.picker)) - -(local M {}) - -;; Utils - -(λ check-api-key [callback] - (if (not (api.key)) - (notify.error "forem.nvim: FOREM_API_KEY environment variable is missing") - (callback))) - -(λ my-articles [] - (api.my-articles picker.my-articles)) - -(λ save-article [] - (let [(content bufnr) (buffer.get-content) - id (vim.fn.expand "%:t") - response (api.save-article id content)] - (api.handle-api-error response - (fn [] - (notify.info "Article saved!") - (vim.api.nvim_buf_set_option bufnr :modified false))))) - -(λ new-article [] - (let [(status title) (pcall vim.fn.input "New article's title: ")] - (when (and status (not= title "")) - (let [response (api.new-article title)] - (api.handle-api-error response buffer.open-my-article))))) - -(λ feed [] - (vim.cmd.edit "forem://articles/feed")) - -(λ open-by-url [] - (let [(status url) (pcall vim.fn.input "Article's URL: ")] - (when (and status (not= url "")) - (let [path (string.match url "(%w+/[%w|-]+)$")] - (if path - (api.get-article-by-path path buffer.open-article) - (notify.error (.. "This URL is not valid: " url))))))) - -;; Autocmd - -(local forem_group (vim.api.nvim_create_augroup :forem_autocmds {:clear true})) - -(λ set-autocmds [] - (vim.api.nvim_create_autocmd [:BufWriteCmd] - {:group forem_group - :pattern "forem://my-article/*" - :callback #(save-article)}) - (vim.api.nvim_create_autocmd [:BufEnter] - {:group forem_group - :pattern "forem://articles/feed" - :callback #(buffer.load-feed)}) - (vim.api.nvim_create_autocmd [:CursorMoved] - {:group forem_group - :pattern "forem://*/floatmenu" - :callback (fn [] - (let [[buffer line column off] (vim.fn.getpos ".")] - (when (> column 1) - (vim.fn.setpos "." - [buffer - line - 1 - off]))))}) - (vim.api.nvim_create_autocmd [:BufEnter] - {:group forem_group - :pattern "forem://*/floatmenu" - :callback (fn [] - (vim.keymap.set :n : - #(vim.api.nvim_win_close 0 - false)))})) - -;; Setup - -(λ setup [] - (set-autocmds) - (set M.my_articles #(check-api-key my-articles)) - (set M.new_article #(check-api-key new-article)) - (set M.feed #(check-api-key feed)) - (set M.open_by_url #(check-api-key open-by-url))) - -(λ M.setup [] - (when (not (api.key)) - (notify.error "forem.nvim: FOREM_API_KEY environment variable is missing")) - (setup)) - -M diff --git a/fnl/forem-nvim/notify.fnl b/fnl/forem-nvim/notify.fnl deleted file mode 100644 index c079154..0000000 --- a/fnl/forem-nvim/notify.fnl +++ /dev/null @@ -1,12 +0,0 @@ -(local M {}) - -(λ notify [message level] - (vim.notify message level {:title :Forem.nvim})) - -(λ M.error [message] - (notify message vim.log.levels.ERROR)) - -(λ M.info [message] - (notify message vim.log.levels.INFO)) - -M diff --git a/fnl/forem-nvim/picker.fnl b/fnl/forem-nvim/picker.fnl deleted file mode 100644 index 3db0e2a..0000000 --- a/fnl/forem-nvim/picker.fnl +++ /dev/null @@ -1,50 +0,0 @@ -(local pickers (require :telescope.pickers)) -(local finders (require :telescope.finders)) -(local previewers (require :telescope.previewers)) -(local actions (require :telescope.actions)) -(local action_state (require :telescope.actions.state)) -(local conf (. (require :telescope.config) :values)) -(local Article (require :forem-nvim.article)) -(local buffer (require :forem-nvim.buffer)) -(local M {}) - -(λ my-articles-picker [articles] - (pickers.new {} - {:prompt_title "My Articles" - :finder (finders.new_table {:results articles - :entry_maker (fn [article] - {:value article - :display article.title - :type_of article.type_of - :ordinal (.. (tostring article.published_at) - article.title)})}) - :previewer (previewers.new_buffer_previewer {:title "Article Preview" - :dyn_title (fn [_ - entry] - entry.display) - :define_preview (fn [self - entry] - (when (not self.state.bufname) - (let [article entry.value - article-body (Article.get-body-lines article) - bufnr self.state.bufnr] - (vim.api.nvim_buf_set_option bufnr - :filetype - :markdown) - (buffer.write bufnr - article-body)))) - :get_buffer_by_name (fn [_ - entry] - entry.value.slug)}) - :sorter (conf.prefilter_sorter {:tag :type_of - :sorter (conf.generic_sorter {})}) - :attach_mappings (fn [prompt_bufnr _] - (actions.select_default:replace (fn [] - (let [selection (action_state.get_selected_entry prompt_bufnr)] - (actions.close prompt_bufnr) - (buffer.open-my-article selection.value)))))})) - -(λ M.my-articles [articles] - (: (my-articles-picker articles) :find)) - -M diff --git a/fnl/forem-nvim/util.fnl b/fnl/forem-nvim/util.fnl deleted file mode 100644 index 13b5259..0000000 --- a/fnl/forem-nvim/util.fnl +++ /dev/null @@ -1,56 +0,0 @@ -(local M {}) - -(λ M.fold [fun init list] - (var acc init) - (each [_ el (pairs list)] - (set acc (fun el acc))) - acc) - -(λ M.set-local [opt val] - (tset vim.opt_local opt val)) - -(λ M.set-locals [opt-vals] - (each [_ [opt val] (pairs opt-vals)] - (M.set-local opt val))) - -(λ M.is-executable? [file] - (= 1 (vim.fn.executable file))) - -(λ M.open-browser-url [url] - (let [?cmd (if (M.is-executable? :xdg-open) :xdg-open - (M.is-executable? :open) :open - (M.is-executable? :start) :start - nil)] - (if ?cmd - (vim.cmd (.. ":!" ?cmd " " url)) - (vim.api.nvim_err_writeln (.. "Could not find a command to open the URL: " - url))))) - -(λ M.get-plural [count noun ?plural] - (if (= 1 count) - noun - (or ?plural (.. noun :s)))) - -(λ M.open-float-menu [content ?options] - (let [width (accumulate [bigger 0 _ text (pairs content)] - (let [text-width (vim.fn.len text)] - (if (> text-width bigger) - text-width - bigger))) - buffer (vim.api.nvim_create_buf false true) - float-options (vim.tbl_extend :keep (or ?options {}) - {:relative :cursor - :col 0 - :row 1 - :style :minimal - : width - :border :rounded - :height (vim.fn.len content)}) - win (vim.api.nvim_open_win buffer 0 float-options)] - (vim.api.nvim_buf_set_lines buffer 0 -1 true content) - (vim.api.nvim_buf_set_name buffer "forem://feed/floatmenu") - (vim.api.nvim_buf_set_option buffer :modifiable false) - (vim.api.nvim_buf_set_option buffer :bufhidden :delete) - (vim.api.nvim_win_set_option win :cursorline true))) - -M diff --git a/lua/forem-nvim/api.lua b/lua/forem-nvim/api.lua index e7e68e5..4ddf337 100644 --- a/lua/forem-nvim/api.lua +++ b/lua/forem-nvim/api.lua @@ -1,106 +1,111 @@ +--[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]] +local ____exports = {} local curl = require("plenary.curl") -local notify = require("forem-nvim.notify") local job = require("plenary.job") -local M = {} -local base_url = "https://dev.to/api" -M.key = function() - return vim.env.FOREM_API_KEY -end -local function handle_async_error(response) - _G.assert((nil ~= response), "Missing argument response on fnl/forem-nvim/api.fnl:10") - return notify.error(("Error: " .. response.body.error)) +local notify = require("forem-nvim.notify") +local article = require("forem-nvim.article") +____exports.key = function() return vim.env.FOREM_API_KEY end +local baseUrl = "https://dev.to/api" +local function handleAsyncError(response) + notify.error("Error: " .. tostring(response.body.error)) end -local function get_article_template(title) - _G.assert((nil ~= title), "Missing argument title on fnl/forem-nvim/api.fnl:13") - return string.format("---\ntitle: %s\npublished: false\ndescription:\ntags:\n# cover_image: https://direct_url_to_image.jpg\n# Use a ratio of 100:42 for best results.\n---\n\n", title) +____exports.handleError = function(response, onSuccess) + local startStatus = string.sub(response.status, 1, 2) + local ____temp_0 + if startStatus == "20" then + ____temp_0 = onSuccess(response.body) + else + ____temp_0 = notify.error("Error: " .. tostring(response.body.error)) + end + return ____temp_0 end -local function request(request_fun, endpoint, options) - _G.assert((nil ~= options), "Missing argument options on fnl/forem-nvim/api.fnl:25") - _G.assert((nil ~= endpoint), "Missing argument endpoint on fnl/forem-nvim/api.fnl:25") - _G.assert((nil ~= request_fun), "Missing argument request-fun on fnl/forem-nvim/api.fnl:25") - local parameters = vim.tbl_extend("force", {url = (base_url .. endpoint), headers = {["api-key"] = M.key, content_type = "application/json", accept = "application/vnd.forem.api-v1+json"}}, options) - local response = request_fun(parameters) - if response.body then - return vim.tbl_extend("force", response, {body = vim.fn.json_decode(response.body)}) - else +local function request(requestFunction, endpoint, options) + local parameters = vim.tbl_extend( + "force", + { + url = baseUrl .. endpoint, + headers = { + ["api-key"] = ____exports.key(), + content_type = "application/json", + accept = "application/vnd.forem.api-v1+json" + } + }, + options + ) + local response = requestFunction(parameters) + if response.body then + return vim.tbl_extend( + "force", + response, + {body = vim.fn.json_decode(response.body)} + ) + end return response - end end -local function request_async(method, endpoint, options, on_success, _3fon_error) - _G.assert((nil ~= on_success), "Missing argument on-success on fnl/forem-nvim/api.fnl:38") - _G.assert((nil ~= options), "Missing argument options on fnl/forem-nvim/api.fnl:38") - _G.assert((nil ~= endpoint), "Missing argument endpoint on fnl/forem-nvim/api.fnl:38") - _G.assert((nil ~= method), "Missing argument method on fnl/forem-nvim/api.fnl:38") - local function _2_(this, code) - local function _3_() - local results = this:result() - local result = vim.fn.join(results, "\n") - local response = vim.fn.json_decode(result) - if (code == 0) then - return on_success(response) - else - handle_async_error(response) - if _3fon_error then - return _3fon_error(response) - else - return nil +local function requestAsync(method, endpoint, options, onSuccess, onError) + return job:new({ + command = "curl", + args = { + "-X", + method, + "-H", + "Content-Type: application/json", + "-H", + "Accept: application/vnd.forem.api-v1+json", + "-H", + "api-key: " .. tostring(____exports.key()), + "-d", + vim.fn.json_encode(options), + baseUrl .. endpoint + }, + on_exit = function(job, code) + vim.schedule(function() + local result = table.concat( + job:result(), + "\n" + ) + local response = vim.fn.json_decode(result) + if code == 0 then + onSuccess(response) + return + end + handleAsyncError(response) + if onError then + onError(response) + end + end) end - end - end - return vim.schedule(_3_) - end - return job:new({command = "curl", args = {"-X", method, "-H", "Content-Type: application/json", "-H", "Accept: application/vnd.forem.api-v1+json", "-H", ("api-key: " .. M.key()), "-d", vim.fn.json_encode(options), (base_url .. endpoint)}, on_exit = _2_}):start() + }):start() end -local function get(endpoint, on_sucess, _3fon_error) - _G.assert((nil ~= on_sucess), "Missing argument on-sucess on fnl/forem-nvim/api.fnl:63") - _G.assert((nil ~= endpoint), "Missing argument endpoint on fnl/forem-nvim/api.fnl:63") - return request_async("GET", endpoint, {}, on_sucess, _3fon_error) +local function get(endpoint, onSuccess, onError) + return requestAsync( + "GET", + endpoint, + {}, + onSuccess, + onError + ) end local function put(endpoint, body) - _G.assert((nil ~= body), "Missing argument body on fnl/forem-nvim/api.fnl:66") - _G.assert((nil ~= endpoint), "Missing argument endpoint on fnl/forem-nvim/api.fnl:66") - return request(curl.put, endpoint, {body = body}) + return request(curl.put, endpoint, {body = body}) end local function post(endpoint, body) - _G.assert((nil ~= body), "Missing argument body on fnl/forem-nvim/api.fnl:69") - _G.assert((nil ~= endpoint), "Missing argument endpoint on fnl/forem-nvim/api.fnl:69") - return request(curl.post, endpoint, {body = body}) -end -M["my-articles"] = function(on_success, _3fon_error) - _G.assert((nil ~= on_success), "Missing argument on-success on fnl/forem-nvim/api.fnl:72") - return get("/articles/me/all", on_success, _3fon_error) -end -M["save-article"] = function(id, content) - _G.assert((nil ~= content), "Missing argument content on fnl/forem-nvim/api.fnl:75") - _G.assert((nil ~= id), "Missing argument id on fnl/forem-nvim/api.fnl:75") - return put(("/articles/" .. id), vim.fn.json_encode({article = {body_markdown = content}})) -end -M["new-article"] = function(title) - _G.assert((nil ~= title), "Missing argument title on fnl/forem-nvim/api.fnl:79") - return post("/articles", vim.fn.json_encode({article = {body_markdown = get_article_template(title)}})) -end -M.feed = function(on_success, _3fon_error) - _G.assert((nil ~= on_success), "Missing argument on-success on fnl/forem-nvim/api.fnl:83") - return get("/articles", on_success, _3fon_error) -end -M["get-article"] = function(id, on_success, _3fon_error) - _G.assert((nil ~= on_success), "Missing argument on-success on fnl/forem-nvim/api.fnl:86") - _G.assert((nil ~= id), "Missing argument id on fnl/forem-nvim/api.fnl:86") - return get(("/articles/" .. id), on_success, _3fon_error) -end -M["get-article-by-path"] = function(path, on_success, _3fon_error) - _G.assert((nil ~= on_success), "Missing argument on-success on fnl/forem-nvim/api.fnl:89") - _G.assert((nil ~= path), "Missing argument path on fnl/forem-nvim/api.fnl:89") - return get(("/articles/" .. path), on_success, _3fon_error) -end -M["handle-api-error"] = function(response, on_success) - _G.assert((nil ~= on_success), "Missing argument on-success on fnl/forem-nvim/api.fnl:92") - _G.assert((nil ~= response), "Missing argument response on fnl/forem-nvim/api.fnl:92") - local start_status = string.sub(tostring(response.status), 1, 2) - if (start_status ~= "20") then - return notify.error(("Error: " .. response.body.error)) - else - return on_success(response) - end + return request(curl.post, endpoint, {body = body}) end -return M +____exports.myArticles = function(onSuccess, onError) return get("/articles/me/all", onSuccess, onError) end +____exports.saveArticle = function(id, content) return put( + "/articles/" .. tostring(id), + vim.fn.json_encode({article = {body_markdown = content}}) +) end +____exports.newArticle = function(title) return post( + "/articles", + vim.fn.json_encode({article = {body_markdown = article.getTemplate(title)}}) +) end +____exports.feed = function(onSuccess, onError) return get("/articles", onSuccess, onError) end +____exports.getArticle = function(id, onSuccess, onError) return get( + "/articles/" .. tostring(id), + onSuccess, + onError +) end +____exports.getArticleByPath = function(path, onSuccess, onError) return get("/articles/" .. path, onSuccess, onError) end +return ____exports diff --git a/lua/forem-nvim/article.lua b/lua/forem-nvim/article.lua index 39f5563..f43894e 100644 --- a/lua/forem-nvim/article.lua +++ b/lua/forem-nvim/article.lua @@ -1,21 +1,5 @@ -local M = {} -local _local_1_ = require("forem-nvim.util") -local get_plural = _local_1_["get-plural"] -local new_line = "\n" -M["get-body-lines"] = function(article) - _G.assert((nil ~= article), "Missing argument article on fnl/forem-nvim/article.fnl:6") - return vim.split(article.body_markdown, new_line) -end -local function tags_to_str(tags) - _G.assert((nil ~= tags), "Missing argument tags on fnl/forem-nvim/article.fnl:9") - local function _2_(tag) - return ("#" .. tag) - end - return vim.fn.join(vim.tbl_map(_2_, tags), ", ") -end -M["format-to-feed"] = function(article, max_columns) - _G.assert((nil ~= max_columns), "Missing argument max-columns on fnl/forem-nvim/article.fnl:14") - _G.assert((nil ~= article), "Missing argument article on fnl/forem-nvim/article.fnl:14") - return {("\240\159\173\189" .. string.rep("\226\150\148", max_columns) .. "\240\159\173\190"), (" ## " .. article.title), (" " .. article.description), (" \240\159\145\164" .. article.user.name .. " (" .. article.user.username .. ")"), "\226\150\143", (" \240\159\149\146 " .. article.reading_time_minutes .. " " .. get_plural(article.reading_time_minutes, "minute") .. " of reading time"), (" Tags: " .. tags_to_str(article.tag_list)), (" \240\159\146\149" .. tostring(article.positive_reactions_count) .. " \240\159\146\172" .. tostring(article.comments_count)), (" \240\159\147\134" .. article.readable_publish_date), "\226\150\143", ("\240\159\173\188" .. string.rep("\226\150\129", max_columns) .. "\240\159\173\191"), ""} -end -return M +--[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]] +local ____exports = {} +____exports.getBodyLines = function(article) return vim.split(article.body_markdown or "", "\n") end +____exports.getTemplate = function(title) return ("---\ntitle: " .. title) .. "\npublished: false\ndescription:\ntags:\n# cover_image: https://direct_url_to_image.jpg\n# Use a ratio of 100:42 for best results.\n---\n\n" end +return ____exports diff --git a/lua/forem-nvim/buffer.lua b/lua/forem-nvim/buffer.lua index 28090db..818af58 100644 --- a/lua/forem-nvim/buffer.lua +++ b/lua/forem-nvim/buffer.lua @@ -1,142 +1,49 @@ -local api = require("forem-nvim.api") +--[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]] +local ____exports = {} local Article = require("forem-nvim.article") -local notify = require("forem-nvim.notify") -local util = require("forem-nvim.util") -local _local_1_ = require("forem-nvim.util") -local fold = _local_1_["fold"] -local set_locals = _local_1_["set-locals"] -local set_local = _local_1_["set-local"] -local M = {} -local function set_basic_options() - return set_locals({{"filetype", "markdown"}, {"modified", false}}) -end -local function set_basic_options_and(opt_vals) - _G.assert((nil ~= opt_vals), "Missing argument opt-vals on fnl/forem-nvim/buffer.fnl:11") - set_basic_options() - return set_locals(opt_vals) -end -local function set_feed_basic_options() - return set_basic_options_and({{"modifiable", false}, {"spell", false}, {"buftype", "nowrite"}, {"swapfile", false}}) -end -M.write = function(bufnr, text, _3finit) - _G.assert((nil ~= text), "Missing argument text on fnl/forem-nvim/buffer.fnl:21") - _G.assert((nil ~= bufnr), "Missing argument bufnr on fnl/forem-nvim/buffer.fnl:21") - local modifiable = (vim.opt_local.modifiable):get() - set_local("modifiable", true) - vim.api.nvim_buf_set_lines(bufnr, (_3finit or 0), -1, false, text) - return set_local("modifiable", modifiable) -end -M["get-content"] = function() - local bufnr = vim.api.nvim_get_current_buf() - return vim.fn.join(vim.api.nvim_buf_get_lines(bufnr, 0, -1, 1), "\n"), bufnr -end -M["open-my-article"] = function(article) - _G.assert((nil ~= article), "Missing argument article on fnl/forem-nvim/buffer.fnl:32") - vim.cmd(string.format(":edit forem://my-article/%s", article.id)) - do - local bufnr = vim.api.nvim_get_current_buf() - local article_body = Article["get-body-lines"](article) - M.write(bufnr, article_body) - end - return set_basic_options_and({{"buftype", "acwrite"}, {"swapfile", false}}) -end -local function seek_feed_title(line_number, get_next_line_number, count) - _G.assert((nil ~= count), "Missing argument count on fnl/forem-nvim/buffer.fnl:39") - _G.assert((nil ~= get_next_line_number), "Missing argument get-next-line-number on fnl/forem-nvim/buffer.fnl:39") - _G.assert((nil ~= line_number), "Missing argument line-number on fnl/forem-nvim/buffer.fnl:39") - local line_content = vim.fn.getline(line_number) - local _3ftitle = string.match(line_content, " ## (.+)", 1) - if _3ftitle then - return _3ftitle - elseif (count > 1000) then - return notify.error("Could not find title") - else - return seek_feed_title(get_next_line_number(line_number), get_next_line_number, (count + 1)) - end -end -local function get_card_title(line_number) - _G.assert((nil ~= line_number), "Missing argument line-number on fnl/forem-nvim/buffer.fnl:49") - local line_content = vim.fn.getline(line_number) - local is_card_upper_border_3f = string.match(line_content, "\240\159\173\189", 1) - local is_out_of_card_3f = not string.match(line_content, "^[ |\240\159\173\189|\226\150\143|\240\159\173\188]", 1) - if is_out_of_card_3f then - return nil - else - local _3_ - if is_card_upper_border_3f then - local function _4_(n) - return (n + 1) - end - _3_ = _4_ - else - local function _5_(n) - return (n - 1) - end - _3_ = _5_ - end - return seek_feed_title(line_number, _3_, 0) - end -end -local function open_feed_article(location) - _G.assert((nil ~= location), "Missing argument location on fnl/forem-nvim/buffer.fnl:62") - local title = get_card_title(vim.fn.line(".")) - local _3farticle_data = (_G["forem-feed-articles"])[title] - if title then - if _3farticle_data then - if (location == "browser") then - return util["open-browser-url"](_3farticle_data.url) - else - return api["get-article"](_3farticle_data.id, M["open-article"]) - end - else - return notify.error("Could not find article data. Please reopen the feed.") - end - else - return nil - end -end -M["load-feed"] = function() - set_feed_basic_options() - local bufnr = vim.api.nvim_get_current_buf() - M.write(bufnr, {"Loading feed..."}) - local function _11_(articles) - local function _12_() - return open_feed_article("buffer") - end - vim.keymap.set("n", "", _12_, {buffer = true, silent = true}) - local function _13_() - return open_feed_article("browser") - end - vim.keymap.set("n", "", _13_, {buffer = true, silent = true}) - local max_columns - local function _14_(article, total) - return vim.fn.max({#article.title, #article.description, total}) - end - max_columns = fold(_14_, 0, articles) - local feed - local function _15_(article) - return Article["format-to-feed"](article, max_columns) - end - feed = vim.tbl_flatten(vim.tbl_map(_15_, articles)) - _G["forem-feed-articles"] = {} - for _, article in pairs(articles) do - _G["forem-feed-articles"][article.title] = {id = article.id, url = article.url} - end - M.write(bufnr, {"# Your Feed", "", "Press in a card to open the article in a new buffer", "and to open it in the browser.", ""}) - return M.write(bufnr, feed, 5) - end - return api.feed(_11_) -end -M["open-article"] = function(article) - _G.assert((nil ~= article), "Missing argument article on fnl/forem-nvim/buffer.fnl:102") - vim.cmd(string.format(":edit forem://article/%s", article.title)) - do - local bufnr = vim.api.nvim_get_current_buf() - local article_body = Article["get-body-lines"](article) - set_local("linebreak", true) - set_local("textwidth", 80) - M.write(bufnr, article_body) - end - return set_feed_basic_options() -end -return M +local ____util = require("forem-nvim.util") +local setLocals = ____util.setLocals +local getOption = ____util.getOption +____exports.setBasicOptions = function() + setLocals({{"filetype", "markdown"}, {"modified", false}}) +end +____exports.write = function(buffer, lines, offset) + local modifiable = getOption(vim.opt_local.modifiable) + vim.opt_local.modifiable = true + vim.api.nvim_buf_set_lines( + buffer, + offset or 0, + -1, + false, + lines + ) + vim.opt_local.modifiable = modifiable +end +____exports.getContent = function() + local buffer = vim.api.nvim_get_current_buf() + local lines = vim.api.nvim_buf_get_lines(buffer, 0, -1, true) + return { + content = table.concat(lines, "\n"), + bufnr = buffer + } +end +____exports.openMyArticle = function(article) + vim.cmd(":edit forem://my-article/" .. tostring(article.id)) + local buffer = vim.api.nvim_get_current_buf() + ____exports.write( + buffer, + Article.getBodyLines(article) + ) + ____exports.setBasicOptions() + setLocals({{"buftype", "acwrite"}, {"swapfile", false}}) +end +____exports.loadArticle = function(article) + vim.cmd(":edit forem://article/" .. article.title) + setLocals({{"linebreak", true}, {"textwidth", 80}}) + local buffer = vim.api.nvim_get_current_buf() + local body = Article.getBodyLines(article) + ____exports.write(buffer, body) + ____exports.setBasicOptions() + setLocals({{"modifiable", false}, {"spell", false}, {"buftype", "nowrite"}, {"swapfile", false}}) +end +return ____exports diff --git a/lua/forem-nvim/feed.lua b/lua/forem-nvim/feed.lua new file mode 100644 index 0000000..aea9b97 --- /dev/null +++ b/lua/forem-nvim/feed.lua @@ -0,0 +1,137 @@ +local ____lualib = require("lualib_bundle") +local __TS__ArrayReduce = ____lualib.__TS__ArrayReduce +local __TS__ArrayFlatMap = ____lualib.__TS__ArrayFlatMap +local __TS__ArrayMap = ____lualib.__TS__ArrayMap +local Map = ____lualib.Map +local __TS__New = ____lualib.__TS__New +local ____exports = {} +local getCardTitle, setKeyMaps, populateGlobalFeedArticles, articleToFeed +local api = require("forem-nvim.api") +local buffer = require("forem-nvim.buffer") +local notify = require("forem-nvim.notify") +local util = require("forem-nvim.util") +local ____util = require("forem-nvim.util") +local setLocals = ____util.setLocals +____exports.open = function() return vim.cmd("edit forem://articles/feed") end +local function setBasicOptions() + buffer.setBasicOptions() + setLocals({{"modifiable", false}, {"spell", false}, {"buftype", "nowrite"}, {"swapfile", false}}) +end +local seekTitle +seekTitle = function(line, getNextLine, count) + local lineContent = vim.fn.getline(line) + local title = string.match(lineContent, " ## (.+)", 1) + if title ~= nil then + return title + end + if count > 1000 then + notify.error("Could not find the title of the article") + return nil + end + return seekTitle( + getNextLine(line), + getNextLine, + count + 1 + ) +end +____exports.openArticle = function(location) + local title = getCardTitle(vim.fn.line(".")) + if not title then + notify.error("Could not find article data. Please reopen the feed.") + return + end + local articleData = foremFeedArticles and foremFeedArticles:get(title) + if not articleData then + notify.error("Could not find article data. Please reload the feed.") + return + end + if location == "browser" then + util.openUrlOnBrowser(articleData.url) + else + api.getArticle(articleData.id, buffer.loadArticle) + end +end +getCardTitle = function(line) + local content = vim.fn.getline(line) + local isInsideOfCard = string.match(content, "^[ |🭽|▏|🭼]", 1) + if not isInsideOfCard then + return nil + end + local isUpperBorder = string.match(content, "🭽", 1) + local getNextLine = isUpperBorder ~= nil and (function(line) return line + 1 end) or (function(line) return line - 1 end) + return seekTitle(line, getNextLine, 0) +end +____exports.load = function() + setBasicOptions() + local bufnr = vim.api.nvim_get_current_buf() + buffer.write(bufnr, {"Loading feed..."}) + api.feed(function(articles) + setKeyMaps() + populateGlobalFeedArticles(articles) + local maxColumn = __TS__ArrayReduce( + articles, + function(____, max, article) return math.max(#article.title, #article.description, max) end, + 0 + ) + local feed = __TS__ArrayFlatMap( + articles, + function(____, article) return articleToFeed(article, maxColumn) end + ) + buffer.write( + bufnr, + { + "# Your Feed", + "", + "Press in a card to open the article in a new buffer", + "and to open it in the browser.", + "", + unpack(feed) + } + ) + end) +end +setKeyMaps = function() + vim.keymap.set( + "n", + "", + function() return ____exports.openArticle("buffer") end, + {buffer = true, silent = true} + ) + vim.keymap.set( + "n", + "", + function() return ____exports.openArticle("browser") end, + {buffer = true, silent = true} + ) +end +populateGlobalFeedArticles = function(articles) + foremFeedArticles = __TS__New( + Map, + __TS__ArrayMap( + articles, + function(____, article) return {article.title, {id = article.id, url = article.url}} end + ) + ) +end +____exports.tagsToString = function(tags) return table.concat( + __TS__ArrayMap( + tags, + function(____, tag) return "#" .. tag end + ), + ", " +) end +articleToFeed = function(article, maxColumns) return { + ("🭽" .. string.rep("▔", maxColumns)) .. "🭾", + " ## " .. article.title, + " " .. article.description, + (((" 👤" .. article.user.name) .. " (") .. article.user.username) .. ")", + "▏", + (((" 🕒 " .. tostring(article.reading_time_minutes)) .. " ") .. util.pluralize(article.reading_time_minutes, "minute")) .. " of reading time", + " Tags: " .. ____exports.tagsToString(article.tag_list), + ((" 💕" .. tostring(article.positive_reactions_count)) .. " 💬") .. tostring(article.comments_count), + " 📆" .. article.readable_publish_date, + "▏", + ("🭼" .. string.rep("▁", maxColumns)) .. "🭿", + "" +} end +return ____exports diff --git a/lua/forem-nvim/init.lua b/lua/forem-nvim/init.lua index 4641aa3..39f1e5b 100644 --- a/lua/forem-nvim/init.lua +++ b/lua/forem-nvim/init.lua @@ -1,110 +1,110 @@ +local ____lualib = require("lualib_bundle") +local __TS__Number = ____lualib.__TS__Number +local ____exports = {} local api = require("forem-nvim.api") local buffer = require("forem-nvim.buffer") +local Feed = require("forem-nvim.feed") local notify = require("forem-nvim.notify") local picker = require("forem-nvim.picker") -local M = {} -local function check_api_key(callback) - _G.assert((nil ~= callback), "Missing argument callback on fnl/forem-nvim/init.fnl:10") - if not api.key() then - return notify.error("forem.nvim: FOREM_API_KEY environment variable is missing") - else - return callback() - end -end -local function my_articles() - return api["my-articles"](picker["my-articles"]) -end -local function save_article() - local content, bufnr = buffer["get-content"]() - local id = vim.fn.expand("%:t") - local response = api["save-article"](id, content) - local function _2_() - notify.info("Article saved!") - return vim.api.nvim_buf_set_option(bufnr, "modified", false) - end - return api["handle-api-error"](response, _2_) +local NO_API_KEY_ERROR = "forem.nvim: FOREM_API_KEY environment variable is missing" +local function checkApiKey(callback) + if api.key() then + callback() + return + end + notify.error(NO_API_KEY_ERROR) end -local function new_article() - local status, title = pcall(vim.fn.input, "New article's title: ") - if (status and (title ~= "")) then - local response = api["new-article"](title) - return api["handle-api-error"](response, buffer["open-my-article"]) - else - return nil - end +local function myArticles() + return api.myArticles(picker.myArticles) end -local function feed() - return vim.cmd.edit("forem://articles/feed") +local function saveArticle() + local ____buffer_getContent_result_0 = buffer.getContent() + local bufnr = ____buffer_getContent_result_0.bufnr + local content = ____buffer_getContent_result_0.content + local id = __TS__Number(vim.fn.expand("%:t")) + if not id then + notify.error("forem.nvim: Could not find article id") + return + end + local response = api.saveArticle(id, content) + api.handleError( + response, + function() + notify.info("Article saved") + vim.api.nvim_set_option_value("modified", false, {buf = bufnr}) + end + ) end -local function open_by_url() - local status, url = pcall(vim.fn.input, "Article's URL: ") - if (status and (url ~= "")) then - local path = string.match(url, "(%w+/[%w|-]+)$") - if path then - return api["get-article-by-path"](path, buffer["open-article"]) - else - return notify.error(("This URL is not valid: " .. url)) +local function newArticle() + local status, title = pcall( + function(prompt) return vim.fn.input(prompt) end, + "Article's Title: " + ) + if not status or title == "" then + return end - else - return nil - end + local response = api.newArticle(title) + api.handleError( + response, + function(article) + buffer.openMyArticle(article) + end + ) end -local forem_group = vim.api.nvim_create_augroup("forem_autocmds", {clear = true}) -local function set_autocmds() - local function _6_() - return save_article() - end - vim.api.nvim_create_autocmd({"BufWriteCmd"}, {group = forem_group, pattern = "forem://my-article/*", callback = _6_}) - local function _7_() - return buffer["load-feed"]() - end - vim.api.nvim_create_autocmd({"BufEnter"}, {group = forem_group, pattern = "forem://articles/feed", callback = _7_}) - local function _8_() - local _let_9_ = vim.fn.getpos(".") - local buffer0 = _let_9_[1] - local line = _let_9_[2] - local column = _let_9_[3] - local off = _let_9_[4] - if (column > 1) then - return vim.fn.setpos(".", {buffer0, line, 1, off}) - else - return nil +local function openByUrl() + local status, url = pcall( + function(prompt) return vim.fn.input(prompt) end, + "Article's URL: " + ) + if not status or url == "" then + return end - end - vim.api.nvim_create_autocmd({"CursorMoved"}, {group = forem_group, pattern = "forem://*/floatmenu", callback = _8_}) - local function _11_() - local function _12_() - return vim.api.nvim_win_close(0, false) + local path = string.match(url, "(%w+/[%w|-]+)$") + if path == nil then + notify.error("This URL is not valid: " .. url) + return end - return vim.keymap.set("n", "", _12_) - end - return vim.api.nvim_create_autocmd({"BufEnter"}, {group = forem_group, pattern = "forem://*/floatmenu", callback = _11_}) + api.getArticleByPath(path, buffer.loadArticle) end -local function setup() - set_autocmds() - local function _13_() - return check_api_key(my_articles) - end - M.my_articles = _13_ - local function _14_() - return check_api_key(new_article) - end - M.new_article = _14_ - local function _15_() - return check_api_key(feed) - end - M.feed = _15_ - local function _16_() - return check_api_key(open_by_url) - end - M.open_by_url = _16_ - return nil +local foremAuGroup = vim.api.nvim_create_augroup("forem_autocmds", {}) +local function setAutoCmds() + vim.api.nvim_create_autocmd("BufWriteCmd", {group = foremAuGroup, pattern = "forem://my-article/*", callback = saveArticle}) + vim.api.nvim_create_autocmd("BufEnter", {group = foremAuGroup, pattern = "forem://articles/feed", callback = Feed.load}) + vim.api.nvim_create_autocmd( + "CursorMoved", + { + group = foremAuGroup, + pattern = "forem://*/floatmenu", + callback = function() + local buffer, line, column, off = unpack(vim.fn.getpos(".")) + if column <= 1 then + return + end + vim.fn.setpos(".", {buffer, line, 1, off}) + end + } + ) + vim.api.nvim_create_autocmd( + "BufEnter", + { + group = foremAuGroup, + pattern = "forem://*/floatmenu", + callback = function() + vim.keymap.set( + "n", + "", + function() return vim.api.nvim_win_close(0, false) end + ) + end + } + ) end -M.setup = function() - if not api.key() then - notify.error("forem.nvim: FOREM_API_KEY environment variable is missing") - else - end - return setup() +setAutoCmds() +if not api.key() then + notify.error(NO_API_KEY_ERROR) end -return M +____exports.my_articles = function() return checkApiKey(myArticles) end +____exports.new_article = function() return checkApiKey(newArticle) end +____exports.feed = function() return checkApiKey(Feed.open) end +____exports.open_url = function() return checkApiKey(openByUrl) end +return ____exports diff --git a/lua/forem-nvim/notify.lua b/lua/forem-nvim/notify.lua index 7edfe12..47c0cbc 100644 --- a/lua/forem-nvim/notify.lua +++ b/lua/forem-nvim/notify.lua @@ -1,15 +1,8 @@ -local M = {} +--[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]] +local ____exports = {} local function notify(message, level) - _G.assert((nil ~= level), "Missing argument level on fnl/forem-nvim/notify.fnl:3") - _G.assert((nil ~= message), "Missing argument message on fnl/forem-nvim/notify.fnl:3") - return vim.notify(message, level, {title = "Forem.nvim"}) + vim.notify(message, vim.log.levels[level], {title = "Forem.nvim"}) end -M.error = function(message) - _G.assert((nil ~= message), "Missing argument message on fnl/forem-nvim/notify.fnl:6") - return notify(message, vim.log.levels.ERROR) -end -M.info = function(message) - _G.assert((nil ~= message), "Missing argument message on fnl/forem-nvim/notify.fnl:9") - return notify(message, vim.log.levels.INFO) -end -return M +____exports.error = function(message) return notify(message, "ERROR") end +____exports.info = function(message) return notify(message, "INFO") end +return ____exports diff --git a/lua/forem-nvim/picker.lua b/lua/forem-nvim/picker.lua index 2e99808..9668a47 100644 --- a/lua/forem-nvim/picker.lua +++ b/lua/forem-nvim/picker.lua @@ -1,46 +1,52 @@ -local pickers = require("telescope.pickers") +--[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]] +local ____exports = {} +local actions = require("telescope.actions") +local actionState = require("telescope.actions.state") local finders = require("telescope.finders") +local pickers = require("telescope.pickers") local previewers = require("telescope.previewers") -local actions = require("telescope.actions") -local action_state = require("telescope.actions.state") -local conf = (require("telescope.config")).values -local Article = require("forem-nvim.article") local buffer = require("forem-nvim.buffer") -local M = {} -local function my_articles_picker(articles) - _G.assert((nil ~= articles), "Missing argument articles on fnl/forem-nvim/picker.fnl:11") - local function _1_(article) - return {value = article, display = article.title, type_of = article.type_of, ordinal = (tostring(article.published_at) .. article.title)} - end - local function _2_(_, entry) - return entry.display - end - local function _3_(self, entry) - if not self.state.bufname then - local article = entry.value - local article_body = Article["get-body-lines"](article) - local bufnr = self.state.bufnr - vim.api.nvim_buf_set_option(bufnr, "filetype", "markdown") - return buffer.write(bufnr, article_body) - else - return nil - end - end - local function _5_(_, entry) - return entry.value.slug - end - local function _6_(prompt_bufnr, _) - local function _7_() - local selection = action_state.get_selected_entry(prompt_bufnr) - actions.close(prompt_bufnr) - return buffer["open-my-article"](selection.value) - end - return (actions.select_default):replace(_7_) - end - return pickers.new({}, {prompt_title = "My Articles", finder = finders.new_table({results = articles, entry_maker = _1_}), previewer = previewers.new_buffer_previewer({title = "Article Preview", dyn_title = _2_, define_preview = _3_, get_buffer_by_name = _5_}), sorter = conf.prefilter_sorter({tag = "type_of", sorter = conf.generic_sorter({})}), attach_mappings = _6_}) +local ____article = require("forem-nvim.article") +local getBodyLines = ____article.getBodyLines +local ____telescope_2Econfig = require("telescope.config") +local configValues = ____telescope_2Econfig.values +local function myArticlesPicker(articles) + return pickers.new( + {}, + { + prompt_title = "My Articles", + finder = finders.new_table({ + results = articles, + entry_maker = function(article) + return {value = article, display = article.title, type_of = article.type_of, ordinal = article.title} + end + }), + previewer = previewers.new_buffer_previewer({ + title = "Article Preview", + dyn_title = function(_, entry) return entry.display end, + define_preview = function(____self, entry) + if ____self.state.bufname then + return + end + local body = getBodyLines(entry.value) + vim.api.nvim_set_option_value("filetype", "markdown", {buf = ____self.state.bufnr}) + buffer.write(____self.state.bufnr, body) + end, + get_buffer_by_name = function(_self, entry) return entry.value.slug end + }), + sorter = configValues.prefilter_sorter({ + tag = "type_of", + sorter = configValues.generic_sorter({}) + }), + attach_mappings = function(prompt_bufnr) return actions.select_default:replace(function() + local selection = actionState.get_selected_entry(prompt_bufnr) + actions.close(prompt_bufnr) + buffer.openMyArticle(selection.value) + end) end + } + ) end -M["my-articles"] = function(articles) - _G.assert((nil ~= articles), "Missing argument articles on fnl/forem-nvim/picker.fnl:47") - return my_articles_picker(articles):find() +____exports.myArticles = function(articles) + myArticlesPicker(articles):find() end -return M +return ____exports diff --git a/lua/forem-nvim/util.lua b/lua/forem-nvim/util.lua index e46d879..ab88d95 100644 --- a/lua/forem-nvim/util.lua +++ b/lua/forem-nvim/util.lua @@ -1,83 +1,79 @@ -local M = {} -M.fold = function(fun, init, list) - _G.assert((nil ~= list), "Missing argument list on fnl/forem-nvim/util.fnl:3") - _G.assert((nil ~= init), "Missing argument init on fnl/forem-nvim/util.fnl:3") - _G.assert((nil ~= fun), "Missing argument fun on fnl/forem-nvim/util.fnl:3") - local acc = init - for _, el in pairs(list) do - acc = fun(el, acc) - end - return acc +local ____lualib = require("lualib_bundle") +local __TS__ArrayForEach = ____lualib.__TS__ArrayForEach +local __TS__ArrayReduce = ____lualib.__TS__ArrayReduce +local ____exports = {} +local getOpenCommand +____exports.setLocals = function(values) + __TS__ArrayForEach( + values, + function(____, ____bindingPattern0) + local value + local key + key = ____bindingPattern0[1] + value = ____bindingPattern0[2] + local ____value_0 = value + vim.opt_local[key] = ____value_0 + return ____value_0 + end + ) end -M["set-local"] = function(opt, val) - _G.assert((nil ~= val), "Missing argument val on fnl/forem-nvim/util.fnl:9") - _G.assert((nil ~= opt), "Missing argument opt on fnl/forem-nvim/util.fnl:9") - do end (vim.opt_local)[opt] = val - return nil +____exports.getOption = function(option) + return option.get(option) end -M["set-locals"] = function(opt_vals) - _G.assert((nil ~= opt_vals), "Missing argument opt-vals on fnl/forem-nvim/util.fnl:12") - for _, _1_ in pairs(opt_vals) do - local _each_2_ = _1_ - local opt = _each_2_[1] - local val = _each_2_[2] - M["set-local"](opt, val) - end - return nil -end -M["is-executable?"] = function(file) - _G.assert((nil ~= file), "Missing argument file on fnl/forem-nvim/util.fnl:16") - return (1 == vim.fn.executable(file)) -end -M["open-browser-url"] = function(url) - _G.assert((nil ~= url), "Missing argument url on fnl/forem-nvim/util.fnl:19") - local _3fcmd - if M["is-executable?"]("xdg-open") then - _3fcmd = "xdg-open" - elseif M["is-executable?"]("open") then - _3fcmd = "open" - elseif M["is-executable?"]("start") then - _3fcmd = "start" - else - _3fcmd = nil - end - if _3fcmd then - return vim.cmd((":!" .. _3fcmd .. " " .. url)) - else - return vim.api.nvim_err_writeln(("Could not find a command to open the URL: " .. url)) - end +____exports.isExecutable = function(path) return vim.fn.executable(path) == 1 end +____exports.openUrlOnBrowser = function(url) + local cmd = getOpenCommand() + if not cmd then + vim.api.nvim_err_writeln("Could not find a command to open the URL: $[url]") + return + end + vim.fn.system((cmd .. " ") .. url) end -M["get-plural"] = function(count, noun, _3fplural) - _G.assert((nil ~= noun), "Missing argument noun on fnl/forem-nvim/util.fnl:29") - _G.assert((nil ~= count), "Missing argument count on fnl/forem-nvim/util.fnl:29") - if (1 == count) then - return noun - else - return (_3fplural or (noun .. "s")) - end +getOpenCommand = function() + if ____exports.isExecutable("xdg-open") then + return "xdg-open" + end + if ____exports.isExecutable("open") then + return "open" + end + if ____exports.isExecutable("start") then + return "start" + end + return nil end -M["open-float-menu"] = function(content, _3foptions) - _G.assert((nil ~= content), "Missing argument content on fnl/forem-nvim/util.fnl:34") - local width - do - local bigger = 0 - for _, text in pairs(content) do - local text_width = vim.fn.len(text) - if (text_width > bigger) then - bigger = text_width - else - bigger = bigger - end +____exports.pluralize = function(count, singular, plural) + if count == 1 then + return singular end - width = bigger - end - local buffer = vim.api.nvim_create_buf(false, true) - local float_options = vim.tbl_extend("keep", (_3foptions or {}), {relative = "cursor", col = 0, row = 1, style = "minimal", width = width, border = "rounded", height = vim.fn.len(content)}) - local win = vim.api.nvim_open_win(buffer, 0, float_options) - vim.api.nvim_buf_set_lines(buffer, 0, -1, true, content) - vim.api.nvim_buf_set_name(buffer, "forem://feed/floatmenu") - vim.api.nvim_buf_set_option(buffer, "modifiable", false) - vim.api.nvim_buf_set_option(buffer, "bufhidden", "delete") - return vim.api.nvim_win_set_option(win, "cursorline", true) + return plural or singular .. "s" +end +____exports.openFloatMenu = function(content, options) + local width = __TS__ArrayReduce( + content, + function(____, acc, line) return math.max(acc, #line) end, + 0 + ) + local bufnr = vim.api.nvim_create_buf(false, true) + local floatOptions = vim.tbl_extend("keep", options or ({}), { + relative = "cursor", + col = 0, + row = 1, + style = "minimal", + width = width, + border = "rounded", + height = #content + }) + local window = vim.api.nvim_open_win(bufnr, false, floatOptions) + vim.api.nvim_buf_set_lines( + bufnr, + 0, + -1, + true, + content + ) + vim.api.nvim_buf_set_name(bufnr, "forem://feed/floatmenu") + vim.api.nvim_set_option_value("modifiable", false, {buf = bufnr}) + vim.api.nvim_set_option_value("bufhidden", "delete", {buf = bufnr}) + vim.api.nvim_set_option_value("cursorline", true, {win = window}) end -return M +return ____exports diff --git a/lua/lualib_bundle.lua b/lua/lualib_bundle.lua new file mode 100644 index 0000000..100c890 --- /dev/null +++ b/lua/lualib_bundle.lua @@ -0,0 +1,2629 @@ +local function __TS__ArrayAt(self, relativeIndex) + local absoluteIndex = relativeIndex < 0 and #self + relativeIndex or relativeIndex + if absoluteIndex >= 0 and absoluteIndex < #self then + return self[absoluteIndex + 1] + end + return nil +end + +local function __TS__ArrayIsArray(value) + return type(value) == "table" and (value[1] ~= nil or next(value) == nil) +end + +local function __TS__ArrayConcat(self, ...) + local items = {...} + local result = {} + local len = 0 + for i = 1, #self do + len = len + 1 + result[len] = self[i] + end + for i = 1, #items do + local item = items[i] + if __TS__ArrayIsArray(item) then + for j = 1, #item do + len = len + 1 + result[len] = item[j] + end + else + len = len + 1 + result[len] = item + end + end + return result +end + +local __TS__Symbol, Symbol +do + local symbolMetatable = {__tostring = function(self) + return ("Symbol(" .. (self.description or "")) .. ")" + end} + function __TS__Symbol(description) + return setmetatable({description = description}, symbolMetatable) + end + Symbol = { + asyncDispose = __TS__Symbol("Symbol.asyncDispose"), + dispose = __TS__Symbol("Symbol.dispose"), + iterator = __TS__Symbol("Symbol.iterator"), + hasInstance = __TS__Symbol("Symbol.hasInstance"), + species = __TS__Symbol("Symbol.species"), + toStringTag = __TS__Symbol("Symbol.toStringTag") + } +end + +local function __TS__ArrayEntries(array) + local key = 0 + return { + [Symbol.iterator] = function(self) + return self + end, + next = function(self) + local result = {done = array[key + 1] == nil, value = {key, array[key + 1]}} + key = key + 1 + return result + end + } +end + +local function __TS__ArrayEvery(self, callbackfn, thisArg) + for i = 1, #self do + if not callbackfn(thisArg, self[i], i - 1, self) then + return false + end + end + return true +end + +local function __TS__ArrayFill(self, value, start, ____end) + local relativeStart = start or 0 + local relativeEnd = ____end or #self + if relativeStart < 0 then + relativeStart = relativeStart + #self + end + if relativeEnd < 0 then + relativeEnd = relativeEnd + #self + end + do + local i = relativeStart + while i < relativeEnd do + self[i + 1] = value + i = i + 1 + end + end + return self +end + +local function __TS__ArrayFilter(self, callbackfn, thisArg) + local result = {} + local len = 0 + for i = 1, #self do + if callbackfn(thisArg, self[i], i - 1, self) then + len = len + 1 + result[len] = self[i] + end + end + return result +end + +local function __TS__ArrayForEach(self, callbackFn, thisArg) + for i = 1, #self do + callbackFn(thisArg, self[i], i - 1, self) + end +end + +local function __TS__ArrayFind(self, predicate, thisArg) + for i = 1, #self do + local elem = self[i] + if predicate(thisArg, elem, i - 1, self) then + return elem + end + end + return nil +end + +local function __TS__ArrayFindIndex(self, callbackFn, thisArg) + for i = 1, #self do + if callbackFn(thisArg, self[i], i - 1, self) then + return i - 1 + end + end + return -1 +end + +local __TS__Iterator +do + local function iteratorGeneratorStep(self) + local co = self.____coroutine + local status, value = coroutine.resume(co) + if not status then + error(value, 0) + end + if coroutine.status(co) == "dead" then + return + end + return true, value + end + local function iteratorIteratorStep(self) + local result = self:next() + if result.done then + return + end + return true, result.value + end + local function iteratorStringStep(self, index) + index = index + 1 + if index > #self then + return + end + return index, string.sub(self, index, index) + end + function __TS__Iterator(iterable) + if type(iterable) == "string" then + return iteratorStringStep, iterable, 0 + elseif iterable.____coroutine ~= nil then + return iteratorGeneratorStep, iterable + elseif iterable[Symbol.iterator] then + local iterator = iterable[Symbol.iterator](iterable) + return iteratorIteratorStep, iterator + else + return ipairs(iterable) + end + end +end + +local __TS__ArrayFrom +do + local function arrayLikeStep(self, index) + index = index + 1 + if index > self.length then + return + end + return index, self[index] + end + local function arrayLikeIterator(arr) + if type(arr.length) == "number" then + return arrayLikeStep, arr, 0 + end + return __TS__Iterator(arr) + end + function __TS__ArrayFrom(arrayLike, mapFn, thisArg) + local result = {} + if mapFn == nil then + for ____, v in arrayLikeIterator(arrayLike) do + result[#result + 1] = v + end + else + for i, v in arrayLikeIterator(arrayLike) do + result[#result + 1] = mapFn(thisArg, v, i - 1) + end + end + return result + end +end + +local function __TS__ArrayIncludes(self, searchElement, fromIndex) + if fromIndex == nil then + fromIndex = 0 + end + local len = #self + local k = fromIndex + if fromIndex < 0 then + k = len + fromIndex + end + if k < 0 then + k = 0 + end + for i = k + 1, len do + if self[i] == searchElement then + return true + end + end + return false +end + +local function __TS__ArrayIndexOf(self, searchElement, fromIndex) + if fromIndex == nil then + fromIndex = 0 + end + local len = #self + if len == 0 then + return -1 + end + if fromIndex >= len then + return -1 + end + if fromIndex < 0 then + fromIndex = len + fromIndex + if fromIndex < 0 then + fromIndex = 0 + end + end + for i = fromIndex + 1, len do + if self[i] == searchElement then + return i - 1 + end + end + return -1 +end + +local function __TS__ArrayJoin(self, separator) + if separator == nil then + separator = "," + end + local parts = {} + for i = 1, #self do + parts[i] = tostring(self[i]) + end + return table.concat(parts, separator) +end + +local function __TS__ArrayMap(self, callbackfn, thisArg) + local result = {} + for i = 1, #self do + result[i] = callbackfn(thisArg, self[i], i - 1, self) + end + return result +end + +local function __TS__ArrayPush(self, ...) + local items = {...} + local len = #self + for i = 1, #items do + len = len + 1 + self[len] = items[i] + end + return len +end + +local function __TS__ArrayPushArray(self, items) + local len = #self + for i = 1, #items do + len = len + 1 + self[len] = items[i] + end + return len +end + +local function __TS__CountVarargs(...) + return select("#", ...) +end + +local function __TS__ArrayReduce(self, callbackFn, ...) + local len = #self + local k = 0 + local accumulator = nil + if __TS__CountVarargs(...) ~= 0 then + accumulator = ... + elseif len > 0 then + accumulator = self[1] + k = 1 + else + error("Reduce of empty array with no initial value", 0) + end + for i = k + 1, len do + accumulator = callbackFn( + nil, + accumulator, + self[i], + i - 1, + self + ) + end + return accumulator +end + +local function __TS__ArrayReduceRight(self, callbackFn, ...) + local len = #self + local k = len - 1 + local accumulator = nil + if __TS__CountVarargs(...) ~= 0 then + accumulator = ... + elseif len > 0 then + accumulator = self[k + 1] + k = k - 1 + else + error("Reduce of empty array with no initial value", 0) + end + for i = k + 1, 1, -1 do + accumulator = callbackFn( + nil, + accumulator, + self[i], + i - 1, + self + ) + end + return accumulator +end + +local function __TS__ArrayReverse(self) + local i = 1 + local j = #self + while i < j do + local temp = self[j] + self[j] = self[i] + self[i] = temp + i = i + 1 + j = j - 1 + end + return self +end + +local function __TS__ArrayUnshift(self, ...) + local items = {...} + local numItemsToInsert = #items + if numItemsToInsert == 0 then + return #self + end + for i = #self, 1, -1 do + self[i + numItemsToInsert] = self[i] + end + for i = 1, numItemsToInsert do + self[i] = items[i] + end + return #self +end + +local function __TS__ArraySort(self, compareFn) + if compareFn ~= nil then + table.sort( + self, + function(a, b) return compareFn(nil, a, b) < 0 end + ) + else + table.sort(self) + end + return self +end + +local function __TS__ArraySlice(self, first, last) + local len = #self + first = first or 0 + if first < 0 then + first = len + first + if first < 0 then + first = 0 + end + else + if first > len then + first = len + end + end + last = last or len + if last < 0 then + last = len + last + if last < 0 then + last = 0 + end + else + if last > len then + last = len + end + end + local out = {} + first = first + 1 + last = last + 1 + local n = 1 + while first < last do + out[n] = self[first] + first = first + 1 + n = n + 1 + end + return out +end + +local function __TS__ArraySome(self, callbackfn, thisArg) + for i = 1, #self do + if callbackfn(thisArg, self[i], i - 1, self) then + return true + end + end + return false +end + +local function __TS__ArraySplice(self, ...) + local args = {...} + local len = #self + local actualArgumentCount = __TS__CountVarargs(...) + local start = args[1] + local deleteCount = args[2] + if start < 0 then + start = len + start + if start < 0 then + start = 0 + end + elseif start > len then + start = len + end + local itemCount = actualArgumentCount - 2 + if itemCount < 0 then + itemCount = 0 + end + local actualDeleteCount + if actualArgumentCount == 0 then + actualDeleteCount = 0 + elseif actualArgumentCount == 1 then + actualDeleteCount = len - start + else + actualDeleteCount = deleteCount or 0 + if actualDeleteCount < 0 then + actualDeleteCount = 0 + end + if actualDeleteCount > len - start then + actualDeleteCount = len - start + end + end + local out = {} + for k = 1, actualDeleteCount do + local from = start + k + if self[from] ~= nil then + out[k] = self[from] + end + end + if itemCount < actualDeleteCount then + for k = start + 1, len - actualDeleteCount do + local from = k + actualDeleteCount + local to = k + itemCount + if self[from] then + self[to] = self[from] + else + self[to] = nil + end + end + for k = len - actualDeleteCount + itemCount + 1, len do + self[k] = nil + end + elseif itemCount > actualDeleteCount then + for k = len - actualDeleteCount, start + 1, -1 do + local from = k + actualDeleteCount + local to = k + itemCount + if self[from] then + self[to] = self[from] + else + self[to] = nil + end + end + end + local j = start + 1 + for i = 3, actualArgumentCount do + self[j] = args[i] + j = j + 1 + end + for k = #self, len - actualDeleteCount + itemCount + 1, -1 do + self[k] = nil + end + return out +end + +local function __TS__ArrayToObject(self) + local object = {} + for i = 1, #self do + object[i - 1] = self[i] + end + return object +end + +local function __TS__ArrayFlat(self, depth) + if depth == nil then + depth = 1 + end + local result = {} + local len = 0 + for i = 1, #self do + local value = self[i] + if depth > 0 and __TS__ArrayIsArray(value) then + local toAdd + if depth == 1 then + toAdd = value + else + toAdd = __TS__ArrayFlat(value, depth - 1) + end + for j = 1, #toAdd do + local val = toAdd[j] + len = len + 1 + result[len] = val + end + else + len = len + 1 + result[len] = value + end + end + return result +end + +local function __TS__ArrayFlatMap(self, callback, thisArg) + local result = {} + local len = 0 + for i = 1, #self do + local value = callback(thisArg, self[i], i - 1, self) + if __TS__ArrayIsArray(value) then + for j = 1, #value do + len = len + 1 + result[len] = value[j] + end + else + len = len + 1 + result[len] = value + end + end + return result +end + +local function __TS__ArraySetLength(self, length) + if length < 0 or length ~= length or length == math.huge or math.floor(length) ~= length then + error( + "invalid array length: " .. tostring(length), + 0 + ) + end + for i = length + 1, #self do + self[i] = nil + end + return length +end + +local __TS__Unpack = table.unpack or unpack + +local function __TS__ArrayToReversed(self) + local copy = {__TS__Unpack(self)} + __TS__ArrayReverse(copy) + return copy +end + +local function __TS__ArrayToSorted(self, compareFn) + local copy = {__TS__Unpack(self)} + __TS__ArraySort(copy, compareFn) + return copy +end + +local function __TS__ArrayToSpliced(self, start, deleteCount, ...) + local copy = {__TS__Unpack(self)} + __TS__ArraySplice(copy, start, deleteCount, ...) + return copy +end + +local function __TS__ArrayWith(self, index, value) + local copy = {__TS__Unpack(self)} + copy[index + 1] = value + return copy +end + +local function __TS__New(target, ...) + local instance = setmetatable({}, target.prototype) + instance:____constructor(...) + return instance +end + +local function __TS__InstanceOf(obj, classTbl) + if type(classTbl) ~= "table" then + error("Right-hand side of 'instanceof' is not an object", 0) + end + if classTbl[Symbol.hasInstance] ~= nil then + return not not classTbl[Symbol.hasInstance](classTbl, obj) + end + if type(obj) == "table" then + local luaClass = obj.constructor + while luaClass ~= nil do + if luaClass == classTbl then + return true + end + luaClass = luaClass.____super + end + end + return false +end + +local function __TS__Class(self) + local c = {prototype = {}} + c.prototype.__index = c.prototype + c.prototype.constructor = c + return c +end + +local __TS__Promise +do + local function makeDeferredPromiseFactory() + local resolve + local reject + local function executor(____, res, rej) + resolve = res + reject = rej + end + return function() + local promise = __TS__New(__TS__Promise, executor) + return promise, resolve, reject + end + end + local makeDeferredPromise = makeDeferredPromiseFactory() + local function isPromiseLike(value) + return __TS__InstanceOf(value, __TS__Promise) + end + local function doNothing(self) + end + local ____pcall = _G.pcall + __TS__Promise = __TS__Class() + __TS__Promise.name = "__TS__Promise" + function __TS__Promise.prototype.____constructor(self, executor) + self.state = 0 + self.fulfilledCallbacks = {} + self.rejectedCallbacks = {} + self.finallyCallbacks = {} + local success, ____error = ____pcall( + executor, + nil, + function(____, v) return self:resolve(v) end, + function(____, err) return self:reject(err) end + ) + if not success then + self:reject(____error) + end + end + function __TS__Promise.resolve(value) + if __TS__InstanceOf(value, __TS__Promise) then + return value + end + local promise = __TS__New(__TS__Promise, doNothing) + promise.state = 1 + promise.value = value + return promise + end + function __TS__Promise.reject(reason) + local promise = __TS__New(__TS__Promise, doNothing) + promise.state = 2 + promise.rejectionReason = reason + return promise + end + __TS__Promise.prototype["then"] = function(self, onFulfilled, onRejected) + local promise, resolve, reject = makeDeferredPromise() + self:addCallbacks( + onFulfilled and self:createPromiseResolvingCallback(onFulfilled, resolve, reject) or resolve, + onRejected and self:createPromiseResolvingCallback(onRejected, resolve, reject) or reject + ) + return promise + end + function __TS__Promise.prototype.addCallbacks(self, fulfilledCallback, rejectedCallback) + if self.state == 1 then + return fulfilledCallback(nil, self.value) + end + if self.state == 2 then + return rejectedCallback(nil, self.rejectionReason) + end + local ____self_fulfilledCallbacks_0 = self.fulfilledCallbacks + ____self_fulfilledCallbacks_0[#____self_fulfilledCallbacks_0 + 1] = fulfilledCallback + local ____self_rejectedCallbacks_1 = self.rejectedCallbacks + ____self_rejectedCallbacks_1[#____self_rejectedCallbacks_1 + 1] = rejectedCallback + end + function __TS__Promise.prototype.catch(self, onRejected) + return self["then"](self, nil, onRejected) + end + function __TS__Promise.prototype.finally(self, onFinally) + if onFinally then + local ____self_finallyCallbacks_2 = self.finallyCallbacks + ____self_finallyCallbacks_2[#____self_finallyCallbacks_2 + 1] = onFinally + if self.state ~= 0 then + onFinally(nil) + end + end + return self + end + function __TS__Promise.prototype.resolve(self, value) + if isPromiseLike(value) then + return value:addCallbacks( + function(____, v) return self:resolve(v) end, + function(____, err) return self:reject(err) end + ) + end + if self.state == 0 then + self.state = 1 + self.value = value + return self:invokeCallbacks(self.fulfilledCallbacks, value) + end + end + function __TS__Promise.prototype.reject(self, reason) + if self.state == 0 then + self.state = 2 + self.rejectionReason = reason + return self:invokeCallbacks(self.rejectedCallbacks, reason) + end + end + function __TS__Promise.prototype.invokeCallbacks(self, callbacks, value) + local callbacksLength = #callbacks + local finallyCallbacks = self.finallyCallbacks + local finallyCallbacksLength = #finallyCallbacks + if callbacksLength ~= 0 then + for i = 1, callbacksLength - 1 do + callbacks[i](callbacks, value) + end + if finallyCallbacksLength == 0 then + return callbacks[callbacksLength](callbacks, value) + end + callbacks[callbacksLength](callbacks, value) + end + if finallyCallbacksLength ~= 0 then + for i = 1, finallyCallbacksLength - 1 do + finallyCallbacks[i](finallyCallbacks) + end + return finallyCallbacks[finallyCallbacksLength](finallyCallbacks) + end + end + function __TS__Promise.prototype.createPromiseResolvingCallback(self, f, resolve, reject) + return function(____, value) + local success, resultOrError = ____pcall(f, nil, value) + if not success then + return reject(nil, resultOrError) + end + return self:handleCallbackValue(resultOrError, resolve, reject) + end + end + function __TS__Promise.prototype.handleCallbackValue(self, value, resolve, reject) + if isPromiseLike(value) then + local nextpromise = value + if nextpromise.state == 1 then + return resolve(nil, nextpromise.value) + elseif nextpromise.state == 2 then + return reject(nil, nextpromise.rejectionReason) + else + return nextpromise:addCallbacks(resolve, reject) + end + else + return resolve(nil, value) + end + end +end + +local __TS__AsyncAwaiter, __TS__Await +do + local cocreate = coroutine.create + local coresume = coroutine.resume + local costatus = coroutine.status + local coyield = coroutine.yield + function __TS__AsyncAwaiter(generator) + return __TS__New( + __TS__Promise, + function(____, resolve, reject) + local fulfilled, step, resolved, asyncCoroutine + function fulfilled(self, value) + local success, resultOrError = coresume(asyncCoroutine, value) + if success then + return step(resultOrError) + end + return reject(nil, resultOrError) + end + function step(result) + if resolved then + return + end + if costatus(asyncCoroutine) == "dead" then + return resolve(nil, result) + end + return __TS__Promise.resolve(result):addCallbacks(fulfilled, reject) + end + resolved = false + asyncCoroutine = cocreate(generator) + local success, resultOrError = coresume( + asyncCoroutine, + function(____, v) + resolved = true + return __TS__Promise.resolve(v):addCallbacks(resolve, reject) + end + ) + if success then + return step(resultOrError) + else + return reject(nil, resultOrError) + end + end + ) + end + function __TS__Await(thing) + return coyield(thing) + end +end + +local function __TS__ClassExtends(target, base) + target.____super = base + local staticMetatable = setmetatable({__index = base}, base) + setmetatable(target, staticMetatable) + local baseMetatable = getmetatable(base) + if baseMetatable then + if type(baseMetatable.__index) == "function" then + staticMetatable.__index = baseMetatable.__index + end + if type(baseMetatable.__newindex) == "function" then + staticMetatable.__newindex = baseMetatable.__newindex + end + end + setmetatable(target.prototype, base.prototype) + if type(base.prototype.__index) == "function" then + target.prototype.__index = base.prototype.__index + end + if type(base.prototype.__newindex) == "function" then + target.prototype.__newindex = base.prototype.__newindex + end + if type(base.prototype.__tostring) == "function" then + target.prototype.__tostring = base.prototype.__tostring + end +end + +local function __TS__CloneDescriptor(____bindingPattern0) + local value + local writable + local set + local get + local configurable + local enumerable + enumerable = ____bindingPattern0.enumerable + configurable = ____bindingPattern0.configurable + get = ____bindingPattern0.get + set = ____bindingPattern0.set + writable = ____bindingPattern0.writable + value = ____bindingPattern0.value + local descriptor = {enumerable = enumerable == true, configurable = configurable == true} + local hasGetterOrSetter = get ~= nil or set ~= nil + local hasValueOrWritableAttribute = writable ~= nil or value ~= nil + if hasGetterOrSetter and hasValueOrWritableAttribute then + error("Invalid property descriptor. Cannot both specify accessors and a value or writable attribute.", 0) + end + if get or set then + descriptor.get = get + descriptor.set = set + else + descriptor.value = value + descriptor.writable = writable == true + end + return descriptor +end + +local function __TS__Decorate(self, originalValue, decorators, context) + local result = originalValue + do + local i = #decorators + while i >= 0 do + local decorator = decorators[i + 1] + if decorator ~= nil then + local ____decorator_result_0 = decorator(self, result, context) + if ____decorator_result_0 == nil then + ____decorator_result_0 = result + end + result = ____decorator_result_0 + end + i = i - 1 + end + end + return result +end + +local function __TS__ObjectAssign(target, ...) + local sources = {...} + for i = 1, #sources do + local source = sources[i] + for key in pairs(source) do + target[key] = source[key] + end + end + return target +end + +local function __TS__ObjectGetOwnPropertyDescriptor(object, key) + local metatable = getmetatable(object) + if not metatable then + return + end + if not rawget(metatable, "_descriptors") then + return + end + return rawget(metatable, "_descriptors")[key] +end + +local __TS__DescriptorGet +do + local getmetatable = _G.getmetatable + local ____rawget = _G.rawget + function __TS__DescriptorGet(self, metatable, key) + while metatable do + local rawResult = ____rawget(metatable, key) + if rawResult ~= nil then + return rawResult + end + local descriptors = ____rawget(metatable, "_descriptors") + if descriptors then + local descriptor = descriptors[key] + if descriptor ~= nil then + if descriptor.get then + return descriptor.get(self) + end + return descriptor.value + end + end + metatable = getmetatable(metatable) + end + end +end + +local __TS__DescriptorSet +do + local getmetatable = _G.getmetatable + local ____rawget = _G.rawget + local rawset = _G.rawset + function __TS__DescriptorSet(self, metatable, key, value) + while metatable do + local descriptors = ____rawget(metatable, "_descriptors") + if descriptors then + local descriptor = descriptors[key] + if descriptor ~= nil then + if descriptor.set then + descriptor.set(self, value) + else + if descriptor.writable == false then + error( + ((("Cannot assign to read only property '" .. key) .. "' of object '") .. tostring(self)) .. "'", + 0 + ) + end + descriptor.value = value + end + return + end + end + metatable = getmetatable(metatable) + end + rawset(self, key, value) + end +end + +local __TS__SetDescriptor +do + local getmetatable = _G.getmetatable + local function descriptorIndex(self, key) + return __TS__DescriptorGet( + self, + getmetatable(self), + key + ) + end + local function descriptorNewIndex(self, key, value) + return __TS__DescriptorSet( + self, + getmetatable(self), + key, + value + ) + end + function __TS__SetDescriptor(target, key, desc, isPrototype) + if isPrototype == nil then + isPrototype = false + end + local ____isPrototype_0 + if isPrototype then + ____isPrototype_0 = target + else + ____isPrototype_0 = getmetatable(target) + end + local metatable = ____isPrototype_0 + if not metatable then + metatable = {} + setmetatable(target, metatable) + end + local value = rawget(target, key) + if value ~= nil then + rawset(target, key, nil) + end + if not rawget(metatable, "_descriptors") then + metatable._descriptors = {} + end + metatable._descriptors[key] = __TS__CloneDescriptor(desc) + metatable.__index = descriptorIndex + metatable.__newindex = descriptorNewIndex + end +end + +local function __TS__DecorateLegacy(decorators, target, key, desc) + local result = target + do + local i = #decorators + while i >= 0 do + local decorator = decorators[i + 1] + if decorator ~= nil then + local oldResult = result + if key == nil then + result = decorator(nil, result) + elseif desc == true then + local value = rawget(target, key) + local descriptor = __TS__ObjectGetOwnPropertyDescriptor(target, key) or ({configurable = true, writable = true, value = value}) + local desc = decorator(nil, target, key, descriptor) or descriptor + local isSimpleValue = desc.configurable == true and desc.writable == true and not desc.get and not desc.set + if isSimpleValue then + rawset(target, key, desc.value) + else + __TS__SetDescriptor( + target, + key, + __TS__ObjectAssign({}, descriptor, desc) + ) + end + elseif desc == false then + result = decorator(nil, target, key, desc) + else + result = decorator(nil, target, key) + end + result = result or oldResult + end + i = i - 1 + end + end + return result +end + +local function __TS__DecorateParam(paramIndex, decorator) + return function(____, target, key) return decorator(nil, target, key, paramIndex) end +end + +local function __TS__StringIncludes(self, searchString, position) + if not position then + position = 1 + else + position = position + 1 + end + local index = string.find(self, searchString, position, true) + return index ~= nil +end + +local Error, RangeError, ReferenceError, SyntaxError, TypeError, URIError +do + local function getErrorStack(self, constructor) + if debug == nil then + return nil + end + local level = 1 + while true do + local info = debug.getinfo(level, "f") + level = level + 1 + if not info then + level = 1 + break + elseif info.func == constructor then + break + end + end + if __TS__StringIncludes(_VERSION, "Lua 5.0") then + return debug.traceback(("[Level " .. tostring(level)) .. "]") + else + return debug.traceback(nil, level) + end + end + local function wrapErrorToString(self, getDescription) + return function(self) + local description = getDescription(self) + local caller = debug.getinfo(3, "f") + local isClassicLua = __TS__StringIncludes(_VERSION, "Lua 5.0") or _VERSION == "Lua 5.1" + if isClassicLua or caller and caller.func ~= error then + return description + else + return (description .. "\n") .. tostring(self.stack) + end + end + end + local function initErrorClass(self, Type, name) + Type.name = name + return setmetatable( + Type, + {__call = function(____, _self, message) return __TS__New(Type, message) end} + ) + end + local ____initErrorClass_1 = initErrorClass + local ____class_0 = __TS__Class() + ____class_0.name = "" + function ____class_0.prototype.____constructor(self, message) + if message == nil then + message = "" + end + self.message = message + self.name = "Error" + self.stack = getErrorStack(nil, self.constructor.new) + local metatable = getmetatable(self) + if metatable and not metatable.__errorToStringPatched then + metatable.__errorToStringPatched = true + metatable.__tostring = wrapErrorToString(nil, metatable.__tostring) + end + end + function ____class_0.prototype.__tostring(self) + return self.message ~= "" and (self.name .. ": ") .. self.message or self.name + end + Error = ____initErrorClass_1(nil, ____class_0, "Error") + local function createErrorClass(self, name) + local ____initErrorClass_3 = initErrorClass + local ____class_2 = __TS__Class() + ____class_2.name = ____class_2.name + __TS__ClassExtends(____class_2, Error) + function ____class_2.prototype.____constructor(self, ...) + ____class_2.____super.prototype.____constructor(self, ...) + self.name = name + end + return ____initErrorClass_3(nil, ____class_2, name) + end + RangeError = createErrorClass(nil, "RangeError") + ReferenceError = createErrorClass(nil, "ReferenceError") + SyntaxError = createErrorClass(nil, "SyntaxError") + TypeError = createErrorClass(nil, "TypeError") + URIError = createErrorClass(nil, "URIError") +end + +local function __TS__ObjectGetOwnPropertyDescriptors(object) + local metatable = getmetatable(object) + if not metatable then + return {} + end + return rawget(metatable, "_descriptors") or ({}) +end + +local function __TS__Delete(target, key) + local descriptors = __TS__ObjectGetOwnPropertyDescriptors(target) + local descriptor = descriptors[key] + if descriptor then + if not descriptor.configurable then + error( + __TS__New( + TypeError, + ((("Cannot delete property " .. tostring(key)) .. " of ") .. tostring(target)) .. "." + ), + 0 + ) + end + descriptors[key] = nil + return true + end + target[key] = nil + return true +end + +local function __TS__StringAccess(self, index) + if index >= 0 and index < #self then + return string.sub(self, index + 1, index + 1) + end +end + +local function __TS__DelegatedYield(iterable) + if type(iterable) == "string" then + for index = 0, #iterable - 1 do + coroutine.yield(__TS__StringAccess(iterable, index)) + end + elseif iterable.____coroutine ~= nil then + local co = iterable.____coroutine + while true do + local status, value = coroutine.resume(co) + if not status then + error(value, 0) + end + if coroutine.status(co) == "dead" then + return value + else + coroutine.yield(value) + end + end + elseif iterable[Symbol.iterator] then + local iterator = iterable[Symbol.iterator](iterable) + while true do + local result = iterator:next() + if result.done then + return result.value + else + coroutine.yield(result.value) + end + end + else + for ____, value in ipairs(iterable) do + coroutine.yield(value) + end + end +end + +local function __TS__FunctionBind(fn, ...) + local boundArgs = {...} + return function(____, ...) + local args = {...} + __TS__ArrayUnshift( + args, + __TS__Unpack(boundArgs) + ) + return fn(__TS__Unpack(args)) + end +end + +local __TS__Generator +do + local function generatorIterator(self) + return self + end + local function generatorNext(self, ...) + local co = self.____coroutine + if coroutine.status(co) == "dead" then + return {done = true} + end + local status, value = coroutine.resume(co, ...) + if not status then + error(value, 0) + end + return { + value = value, + done = coroutine.status(co) == "dead" + } + end + function __TS__Generator(fn) + return function(...) + local args = {...} + local argsLength = __TS__CountVarargs(...) + return { + ____coroutine = coroutine.create(function() return fn(__TS__Unpack(args, 1, argsLength)) end), + [Symbol.iterator] = generatorIterator, + next = generatorNext + } + end + end +end + +local function __TS__InstanceOfObject(value) + local valueType = type(value) + return valueType == "table" or valueType == "function" +end + +local function __TS__LuaIteratorSpread(self, state, firstKey) + local results = {} + local key, value = self(state, firstKey) + while key do + results[#results + 1] = {key, value} + key, value = self(state, key) + end + return __TS__Unpack(results) +end + +local Map +do + Map = __TS__Class() + Map.name = "Map" + function Map.prototype.____constructor(self, entries) + self[Symbol.toStringTag] = "Map" + self.items = {} + self.size = 0 + self.nextKey = {} + self.previousKey = {} + if entries == nil then + return + end + local iterable = entries + if iterable[Symbol.iterator] then + local iterator = iterable[Symbol.iterator](iterable) + while true do + local result = iterator:next() + if result.done then + break + end + local value = result.value + self:set(value[1], value[2]) + end + else + local array = entries + for ____, kvp in ipairs(array) do + self:set(kvp[1], kvp[2]) + end + end + end + function Map.prototype.clear(self) + self.items = {} + self.nextKey = {} + self.previousKey = {} + self.firstKey = nil + self.lastKey = nil + self.size = 0 + end + function Map.prototype.delete(self, key) + local contains = self:has(key) + if contains then + self.size = self.size - 1 + local next = self.nextKey[key] + local previous = self.previousKey[key] + if next ~= nil and previous ~= nil then + self.nextKey[previous] = next + self.previousKey[next] = previous + elseif next ~= nil then + self.firstKey = next + self.previousKey[next] = nil + elseif previous ~= nil then + self.lastKey = previous + self.nextKey[previous] = nil + else + self.firstKey = nil + self.lastKey = nil + end + self.nextKey[key] = nil + self.previousKey[key] = nil + end + self.items[key] = nil + return contains + end + function Map.prototype.forEach(self, callback) + for ____, key in __TS__Iterator(self:keys()) do + callback(nil, self.items[key], key, self) + end + end + function Map.prototype.get(self, key) + return self.items[key] + end + function Map.prototype.has(self, key) + return self.nextKey[key] ~= nil or self.lastKey == key + end + function Map.prototype.set(self, key, value) + local isNewValue = not self:has(key) + if isNewValue then + self.size = self.size + 1 + end + self.items[key] = value + if self.firstKey == nil then + self.firstKey = key + self.lastKey = key + elseif isNewValue then + self.nextKey[self.lastKey] = key + self.previousKey[key] = self.lastKey + self.lastKey = key + end + return self + end + Map.prototype[Symbol.iterator] = function(self) + return self:entries() + end + function Map.prototype.entries(self) + local items = self.items + local nextKey = self.nextKey + local key = self.firstKey + return { + [Symbol.iterator] = function(self) + return self + end, + next = function(self) + local result = {done = not key, value = {key, items[key]}} + key = nextKey[key] + return result + end + } + end + function Map.prototype.keys(self) + local nextKey = self.nextKey + local key = self.firstKey + return { + [Symbol.iterator] = function(self) + return self + end, + next = function(self) + local result = {done = not key, value = key} + key = nextKey[key] + return result + end + } + end + function Map.prototype.values(self) + local items = self.items + local nextKey = self.nextKey + local key = self.firstKey + return { + [Symbol.iterator] = function(self) + return self + end, + next = function(self) + local result = {done = not key, value = items[key]} + key = nextKey[key] + return result + end + } + end + Map[Symbol.species] = Map +end + +local __TS__Match = string.match + +local __TS__MathAtan2 = math.atan2 or math.atan + +local __TS__MathModf = math.modf + +local function __TS__MathSign(val) + if val > 0 then + return 1 + elseif val < 0 then + return -1 + end + return 0 +end + +local function __TS__Number(value) + local valueType = type(value) + if valueType == "number" then + return value + elseif valueType == "string" then + local numberValue = tonumber(value) + if numberValue then + return numberValue + end + if value == "Infinity" then + return math.huge + end + if value == "-Infinity" then + return -math.huge + end + local stringWithoutSpaces = string.gsub(value, "%s", "") + if stringWithoutSpaces == "" then + return 0 + end + return 0 / 0 + elseif valueType == "boolean" then + return value and 1 or 0 + else + return 0 / 0 + end +end + +local function __TS__NumberIsFinite(value) + return type(value) == "number" and value == value and value ~= math.huge and value ~= -math.huge +end + +local function __TS__NumberIsInteger(value) + return __TS__NumberIsFinite(value) and math.floor(value) == value +end + +local function __TS__NumberIsNaN(value) + return value ~= value +end + +local function __TS__StringSubstring(self, start, ____end) + if ____end ~= ____end then + ____end = 0 + end + if ____end ~= nil and start > ____end then + start, ____end = ____end, start + end + if start >= 0 then + start = start + 1 + else + start = 1 + end + if ____end ~= nil and ____end < 0 then + ____end = 0 + end + return string.sub(self, start, ____end) +end + +local __TS__ParseInt +do + local parseIntBasePattern = "0123456789aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTvVwWxXyYzZ" + function __TS__ParseInt(numberString, base) + if base == nil then + base = 10 + local hexMatch = __TS__Match(numberString, "^%s*-?0[xX]") + if hexMatch ~= nil then + base = 16 + numberString = (__TS__Match(hexMatch, "-")) and "-" .. __TS__StringSubstring(numberString, #hexMatch) or __TS__StringSubstring(numberString, #hexMatch) + end + end + if base < 2 or base > 36 then + return 0 / 0 + end + local allowedDigits = base <= 10 and __TS__StringSubstring(parseIntBasePattern, 0, base) or __TS__StringSubstring(parseIntBasePattern, 0, 10 + 2 * (base - 10)) + local pattern = ("^%s*(-?[" .. allowedDigits) .. "]*)" + local number = tonumber((__TS__Match(numberString, pattern)), base) + if number == nil then + return 0 / 0 + end + if number >= 0 then + return math.floor(number) + else + return math.ceil(number) + end + end +end + +local function __TS__ParseFloat(numberString) + local infinityMatch = __TS__Match(numberString, "^%s*(-?Infinity)") + if infinityMatch ~= nil then + return __TS__StringAccess(infinityMatch, 0) == "-" and -math.huge or math.huge + end + local number = tonumber((__TS__Match(numberString, "^%s*(-?%d+%.?%d*)"))) + return number or 0 / 0 +end + +local __TS__NumberToString +do + local radixChars = "0123456789abcdefghijklmnopqrstuvwxyz" + function __TS__NumberToString(self, radix) + if radix == nil or radix == 10 or self == math.huge or self == -math.huge or self ~= self then + return tostring(self) + end + radix = math.floor(radix) + if radix < 2 or radix > 36 then + error("toString() radix argument must be between 2 and 36", 0) + end + local integer, fraction = __TS__MathModf(math.abs(self)) + local result = "" + if radix == 8 then + result = string.format("%o", integer) + elseif radix == 16 then + result = string.format("%x", integer) + else + repeat + do + result = __TS__StringAccess(radixChars, integer % radix) .. result + integer = math.floor(integer / radix) + end + until not (integer ~= 0) + end + if fraction ~= 0 then + result = result .. "." + local delta = 1e-16 + repeat + do + fraction = fraction * radix + delta = delta * radix + local digit = math.floor(fraction) + result = result .. __TS__StringAccess(radixChars, digit) + fraction = fraction - digit + end + until not (fraction >= delta) + end + if self < 0 then + result = "-" .. result + end + return result + end +end + +local function __TS__NumberToFixed(self, fractionDigits) + if math.abs(self) >= 1e+21 or self ~= self then + return tostring(self) + end + local f = math.floor(fractionDigits or 0) + if f < 0 or f > 99 then + error("toFixed() digits argument must be between 0 and 99", 0) + end + return string.format( + ("%." .. tostring(f)) .. "f", + self + ) +end + +local function __TS__ObjectDefineProperty(target, key, desc) + local luaKey = type(key) == "number" and key + 1 or key + local value = rawget(target, luaKey) + local hasGetterOrSetter = desc.get ~= nil or desc.set ~= nil + local descriptor + if hasGetterOrSetter then + if value ~= nil then + error( + "Cannot redefine property: " .. tostring(key), + 0 + ) + end + descriptor = desc + else + local valueExists = value ~= nil + local ____desc_set_4 = desc.set + local ____desc_get_5 = desc.get + local ____temp_0 + if desc.configurable ~= nil then + ____temp_0 = desc.configurable + else + ____temp_0 = valueExists + end + local ____temp_1 + if desc.enumerable ~= nil then + ____temp_1 = desc.enumerable + else + ____temp_1 = valueExists + end + local ____temp_2 + if desc.writable ~= nil then + ____temp_2 = desc.writable + else + ____temp_2 = valueExists + end + local ____temp_3 + if desc.value ~= nil then + ____temp_3 = desc.value + else + ____temp_3 = value + end + descriptor = { + set = ____desc_set_4, + get = ____desc_get_5, + configurable = ____temp_0, + enumerable = ____temp_1, + writable = ____temp_2, + value = ____temp_3 + } + end + __TS__SetDescriptor(target, luaKey, descriptor) + return target +end + +local function __TS__ObjectEntries(obj) + local result = {} + local len = 0 + for key in pairs(obj) do + len = len + 1 + result[len] = {key, obj[key]} + end + return result +end + +local function __TS__ObjectFromEntries(entries) + local obj = {} + local iterable = entries + if iterable[Symbol.iterator] then + local iterator = iterable[Symbol.iterator](iterable) + while true do + local result = iterator:next() + if result.done then + break + end + local value = result.value + obj[value[1]] = value[2] + end + else + for ____, entry in ipairs(entries) do + obj[entry[1]] = entry[2] + end + end + return obj +end + +local function __TS__ObjectKeys(obj) + local result = {} + local len = 0 + for key in pairs(obj) do + len = len + 1 + result[len] = key + end + return result +end + +local function __TS__ObjectRest(target, usedProperties) + local result = {} + for property in pairs(target) do + if not usedProperties[property] then + result[property] = target[property] + end + end + return result +end + +local function __TS__ObjectValues(obj) + local result = {} + local len = 0 + for key in pairs(obj) do + len = len + 1 + result[len] = obj[key] + end + return result +end + +local function __TS__PromiseAll(iterable) + local results = {} + local toResolve = {} + local numToResolve = 0 + local i = 0 + for ____, item in __TS__Iterator(iterable) do + if __TS__InstanceOf(item, __TS__Promise) then + if item.state == 1 then + results[i + 1] = item.value + elseif item.state == 2 then + return __TS__Promise.reject(item.rejectionReason) + else + numToResolve = numToResolve + 1 + toResolve[i] = item + end + else + results[i + 1] = item + end + i = i + 1 + end + if numToResolve == 0 then + return __TS__Promise.resolve(results) + end + return __TS__New( + __TS__Promise, + function(____, resolve, reject) + for index, promise in pairs(toResolve) do + promise["then"]( + promise, + function(____, data) + results[index + 1] = data + numToResolve = numToResolve - 1 + if numToResolve == 0 then + resolve(nil, results) + end + end, + function(____, reason) + reject(nil, reason) + end + ) + end + end + ) +end + +local function __TS__PromiseAllSettled(iterable) + local results = {} + local toResolve = {} + local numToResolve = 0 + local i = 0 + for ____, item in __TS__Iterator(iterable) do + if __TS__InstanceOf(item, __TS__Promise) then + if item.state == 1 then + results[i + 1] = {status = "fulfilled", value = item.value} + elseif item.state == 2 then + results[i + 1] = {status = "rejected", reason = item.rejectionReason} + else + numToResolve = numToResolve + 1 + toResolve[i] = item + end + else + results[i + 1] = {status = "fulfilled", value = item} + end + i = i + 1 + end + if numToResolve == 0 then + return __TS__Promise.resolve(results) + end + return __TS__New( + __TS__Promise, + function(____, resolve) + for index, promise in pairs(toResolve) do + promise["then"]( + promise, + function(____, data) + results[index + 1] = {status = "fulfilled", value = data} + numToResolve = numToResolve - 1 + if numToResolve == 0 then + resolve(nil, results) + end + end, + function(____, reason) + results[index + 1] = {status = "rejected", reason = reason} + numToResolve = numToResolve - 1 + if numToResolve == 0 then + resolve(nil, results) + end + end + ) + end + end + ) +end + +local function __TS__PromiseAny(iterable) + local rejections = {} + local pending = {} + for ____, item in __TS__Iterator(iterable) do + if __TS__InstanceOf(item, __TS__Promise) then + if item.state == 1 then + return __TS__Promise.resolve(item.value) + elseif item.state == 2 then + rejections[#rejections + 1] = item.rejectionReason + else + pending[#pending + 1] = item + end + else + return __TS__Promise.resolve(item) + end + end + if #pending == 0 then + return __TS__Promise.reject("No promises to resolve with .any()") + end + local numResolved = 0 + return __TS__New( + __TS__Promise, + function(____, resolve, reject) + for ____, promise in ipairs(pending) do + promise["then"]( + promise, + function(____, data) + resolve(nil, data) + end, + function(____, reason) + rejections[#rejections + 1] = reason + numResolved = numResolved + 1 + if numResolved == #pending then + reject(nil, {name = "AggregateError", message = "All Promises rejected", errors = rejections}) + end + end + ) + end + end + ) +end + +local function __TS__PromiseRace(iterable) + local pending = {} + for ____, item in __TS__Iterator(iterable) do + if __TS__InstanceOf(item, __TS__Promise) then + if item.state == 1 then + return __TS__Promise.resolve(item.value) + elseif item.state == 2 then + return __TS__Promise.reject(item.rejectionReason) + else + pending[#pending + 1] = item + end + else + return __TS__Promise.resolve(item) + end + end + return __TS__New( + __TS__Promise, + function(____, resolve, reject) + for ____, promise in ipairs(pending) do + promise["then"]( + promise, + function(____, value) return resolve(nil, value) end, + function(____, reason) return reject(nil, reason) end + ) + end + end + ) +end + +local Set +do + Set = __TS__Class() + Set.name = "Set" + function Set.prototype.____constructor(self, values) + self[Symbol.toStringTag] = "Set" + self.size = 0 + self.nextKey = {} + self.previousKey = {} + if values == nil then + return + end + local iterable = values + if iterable[Symbol.iterator] then + local iterator = iterable[Symbol.iterator](iterable) + while true do + local result = iterator:next() + if result.done then + break + end + self:add(result.value) + end + else + local array = values + for ____, value in ipairs(array) do + self:add(value) + end + end + end + function Set.prototype.add(self, value) + local isNewValue = not self:has(value) + if isNewValue then + self.size = self.size + 1 + end + if self.firstKey == nil then + self.firstKey = value + self.lastKey = value + elseif isNewValue then + self.nextKey[self.lastKey] = value + self.previousKey[value] = self.lastKey + self.lastKey = value + end + return self + end + function Set.prototype.clear(self) + self.nextKey = {} + self.previousKey = {} + self.firstKey = nil + self.lastKey = nil + self.size = 0 + end + function Set.prototype.delete(self, value) + local contains = self:has(value) + if contains then + self.size = self.size - 1 + local next = self.nextKey[value] + local previous = self.previousKey[value] + if next ~= nil and previous ~= nil then + self.nextKey[previous] = next + self.previousKey[next] = previous + elseif next ~= nil then + self.firstKey = next + self.previousKey[next] = nil + elseif previous ~= nil then + self.lastKey = previous + self.nextKey[previous] = nil + else + self.firstKey = nil + self.lastKey = nil + end + self.nextKey[value] = nil + self.previousKey[value] = nil + end + return contains + end + function Set.prototype.forEach(self, callback) + for ____, key in __TS__Iterator(self:keys()) do + callback(nil, key, key, self) + end + end + function Set.prototype.has(self, value) + return self.nextKey[value] ~= nil or self.lastKey == value + end + Set.prototype[Symbol.iterator] = function(self) + return self:values() + end + function Set.prototype.entries(self) + local nextKey = self.nextKey + local key = self.firstKey + return { + [Symbol.iterator] = function(self) + return self + end, + next = function(self) + local result = {done = not key, value = {key, key}} + key = nextKey[key] + return result + end + } + end + function Set.prototype.keys(self) + local nextKey = self.nextKey + local key = self.firstKey + return { + [Symbol.iterator] = function(self) + return self + end, + next = function(self) + local result = {done = not key, value = key} + key = nextKey[key] + return result + end + } + end + function Set.prototype.values(self) + local nextKey = self.nextKey + local key = self.firstKey + return { + [Symbol.iterator] = function(self) + return self + end, + next = function(self) + local result = {done = not key, value = key} + key = nextKey[key] + return result + end + } + end + Set[Symbol.species] = Set +end + +local function __TS__SparseArrayNew(...) + local sparseArray = {...} + sparseArray.sparseLength = __TS__CountVarargs(...) + return sparseArray +end + +local function __TS__SparseArrayPush(sparseArray, ...) + local args = {...} + local argsLen = __TS__CountVarargs(...) + local listLen = sparseArray.sparseLength + for i = 1, argsLen do + sparseArray[listLen + i] = args[i] + end + sparseArray.sparseLength = listLen + argsLen +end + +local function __TS__SparseArraySpread(sparseArray) + local _unpack = unpack or table.unpack + return _unpack(sparseArray, 1, sparseArray.sparseLength) +end + +local WeakMap +do + WeakMap = __TS__Class() + WeakMap.name = "WeakMap" + function WeakMap.prototype.____constructor(self, entries) + self[Symbol.toStringTag] = "WeakMap" + self.items = {} + setmetatable(self.items, {__mode = "k"}) + if entries == nil then + return + end + local iterable = entries + if iterable[Symbol.iterator] then + local iterator = iterable[Symbol.iterator](iterable) + while true do + local result = iterator:next() + if result.done then + break + end + local value = result.value + self.items[value[1]] = value[2] + end + else + for ____, kvp in ipairs(entries) do + self.items[kvp[1]] = kvp[2] + end + end + end + function WeakMap.prototype.delete(self, key) + local contains = self:has(key) + self.items[key] = nil + return contains + end + function WeakMap.prototype.get(self, key) + return self.items[key] + end + function WeakMap.prototype.has(self, key) + return self.items[key] ~= nil + end + function WeakMap.prototype.set(self, key, value) + self.items[key] = value + return self + end + WeakMap[Symbol.species] = WeakMap +end + +local WeakSet +do + WeakSet = __TS__Class() + WeakSet.name = "WeakSet" + function WeakSet.prototype.____constructor(self, values) + self[Symbol.toStringTag] = "WeakSet" + self.items = {} + setmetatable(self.items, {__mode = "k"}) + if values == nil then + return + end + local iterable = values + if iterable[Symbol.iterator] then + local iterator = iterable[Symbol.iterator](iterable) + while true do + local result = iterator:next() + if result.done then + break + end + self.items[result.value] = true + end + else + for ____, value in ipairs(values) do + self.items[value] = true + end + end + end + function WeakSet.prototype.add(self, value) + self.items[value] = true + return self + end + function WeakSet.prototype.delete(self, value) + local contains = self:has(value) + self.items[value] = nil + return contains + end + function WeakSet.prototype.has(self, value) + return self.items[value] == true + end + WeakSet[Symbol.species] = WeakSet +end + +local function __TS__SourceMapTraceBack(fileName, sourceMap) + _G.__TS__sourcemap = _G.__TS__sourcemap or ({}) + _G.__TS__sourcemap[fileName] = sourceMap + if _G.__TS__originalTraceback == nil then + local originalTraceback = debug.traceback + _G.__TS__originalTraceback = originalTraceback + debug.traceback = function(thread, message, level) + local trace + if thread == nil and message == nil and level == nil then + trace = originalTraceback() + elseif __TS__StringIncludes(_VERSION, "Lua 5.0") then + trace = originalTraceback((("[Level " .. tostring(level)) .. "] ") .. tostring(message)) + else + trace = originalTraceback(thread, message, level) + end + if type(trace) ~= "string" then + return trace + end + local function replacer(____, file, srcFile, line) + local fileSourceMap = _G.__TS__sourcemap[file] + if fileSourceMap ~= nil and fileSourceMap[line] ~= nil then + local data = fileSourceMap[line] + if type(data) == "number" then + return (srcFile .. ":") .. tostring(data) + end + return (data.file .. ":") .. tostring(data.line) + end + return (file .. ":") .. line + end + local result = string.gsub( + trace, + "(%S+)%.lua:(%d+)", + function(file, line) return replacer(nil, file .. ".lua", file .. ".ts", line) end + ) + local function stringReplacer(____, file, line) + local fileSourceMap = _G.__TS__sourcemap[file] + if fileSourceMap ~= nil and fileSourceMap[line] ~= nil then + local chunkName = (__TS__Match(file, "%[string \"([^\"]+)\"%]")) + local sourceName = string.gsub(chunkName, ".lua$", ".ts") + local data = fileSourceMap[line] + if type(data) == "number" then + return (sourceName .. ":") .. tostring(data) + end + return (data.file .. ":") .. tostring(data.line) + end + return (file .. ":") .. line + end + result = string.gsub( + result, + "(%[string \"[^\"]+\"%]):(%d+)", + function(file, line) return stringReplacer(nil, file, line) end + ) + return result + end + end +end + +local function __TS__Spread(iterable) + local arr = {} + if type(iterable) == "string" then + for i = 0, #iterable - 1 do + arr[i + 1] = __TS__StringAccess(iterable, i) + end + else + local len = 0 + for ____, item in __TS__Iterator(iterable) do + len = len + 1 + arr[len] = item + end + end + return __TS__Unpack(arr) +end + +local function __TS__StringCharAt(self, pos) + if pos ~= pos then + pos = 0 + end + if pos < 0 then + return "" + end + return string.sub(self, pos + 1, pos + 1) +end + +local function __TS__StringCharCodeAt(self, index) + if index ~= index then + index = 0 + end + if index < 0 then + return 0 / 0 + end + return string.byte(self, index + 1) or 0 / 0 +end + +local function __TS__StringEndsWith(self, searchString, endPosition) + if endPosition == nil or endPosition > #self then + endPosition = #self + end + return string.sub(self, endPosition - #searchString + 1, endPosition) == searchString +end + +local function __TS__StringPadEnd(self, maxLength, fillString) + if fillString == nil then + fillString = " " + end + if maxLength ~= maxLength then + maxLength = 0 + end + if maxLength == -math.huge or maxLength == math.huge then + error("Invalid string length", 0) + end + if #self >= maxLength or #fillString == 0 then + return self + end + maxLength = maxLength - #self + if maxLength > #fillString then + fillString = fillString .. string.rep( + fillString, + math.floor(maxLength / #fillString) + ) + end + return self .. string.sub( + fillString, + 1, + math.floor(maxLength) + ) +end + +local function __TS__StringPadStart(self, maxLength, fillString) + if fillString == nil then + fillString = " " + end + if maxLength ~= maxLength then + maxLength = 0 + end + if maxLength == -math.huge or maxLength == math.huge then + error("Invalid string length", 0) + end + if #self >= maxLength or #fillString == 0 then + return self + end + maxLength = maxLength - #self + if maxLength > #fillString then + fillString = fillString .. string.rep( + fillString, + math.floor(maxLength / #fillString) + ) + end + return string.sub( + fillString, + 1, + math.floor(maxLength) + ) .. self +end + +local __TS__StringReplace +do + local sub = string.sub + function __TS__StringReplace(source, searchValue, replaceValue) + local startPos, endPos = string.find(source, searchValue, nil, true) + if not startPos then + return source + end + local before = sub(source, 1, startPos - 1) + local replacement = type(replaceValue) == "string" and replaceValue or replaceValue(nil, searchValue, startPos - 1, source) + local after = sub(source, endPos + 1) + return (before .. replacement) .. after + end +end + +local __TS__StringSplit +do + local sub = string.sub + local find = string.find + function __TS__StringSplit(source, separator, limit) + if limit == nil then + limit = 4294967295 + end + if limit == 0 then + return {} + end + local result = {} + local resultIndex = 1 + if separator == nil or separator == "" then + for i = 1, #source do + result[resultIndex] = sub(source, i, i) + resultIndex = resultIndex + 1 + end + else + local currentPos = 1 + while resultIndex <= limit do + local startPos, endPos = find(source, separator, currentPos, true) + if not startPos then + break + end + result[resultIndex] = sub(source, currentPos, startPos - 1) + resultIndex = resultIndex + 1 + currentPos = endPos + 1 + end + if resultIndex <= limit then + result[resultIndex] = sub(source, currentPos) + end + end + return result + end +end + +local __TS__StringReplaceAll +do + local sub = string.sub + local find = string.find + function __TS__StringReplaceAll(source, searchValue, replaceValue) + if type(replaceValue) == "string" then + local concat = table.concat( + __TS__StringSplit(source, searchValue), + replaceValue + ) + if #searchValue == 0 then + return (replaceValue .. concat) .. replaceValue + end + return concat + end + local parts = {} + local partsIndex = 1 + if #searchValue == 0 then + parts[1] = replaceValue(nil, "", 0, source) + partsIndex = 2 + for i = 1, #source do + parts[partsIndex] = sub(source, i, i) + parts[partsIndex + 1] = replaceValue(nil, "", i, source) + partsIndex = partsIndex + 2 + end + else + local currentPos = 1 + while true do + local startPos, endPos = find(source, searchValue, currentPos, true) + if not startPos then + break + end + parts[partsIndex] = sub(source, currentPos, startPos - 1) + parts[partsIndex + 1] = replaceValue(nil, searchValue, startPos - 1, source) + partsIndex = partsIndex + 2 + currentPos = endPos + 1 + end + parts[partsIndex] = sub(source, currentPos) + end + return table.concat(parts) + end +end + +local function __TS__StringSlice(self, start, ____end) + if start == nil or start ~= start then + start = 0 + end + if ____end ~= ____end then + ____end = 0 + end + if start >= 0 then + start = start + 1 + end + if ____end ~= nil and ____end < 0 then + ____end = ____end - 1 + end + return string.sub(self, start, ____end) +end + +local function __TS__StringStartsWith(self, searchString, position) + if position == nil or position < 0 then + position = 0 + end + return string.sub(self, position + 1, #searchString + position) == searchString +end + +local function __TS__StringSubstr(self, from, length) + if from ~= from then + from = 0 + end + if length ~= nil then + if length ~= length or length <= 0 then + return "" + end + length = length + from + end + if from >= 0 then + from = from + 1 + end + return string.sub(self, from, length) +end + +local function __TS__StringTrim(self) + local result = string.gsub(self, "^[%s ]*(.-)[%s ]*$", "%1") + return result +end + +local function __TS__StringTrimEnd(self) + local result = string.gsub(self, "[%s ]*$", "") + return result +end + +local function __TS__StringTrimStart(self) + local result = string.gsub(self, "^[%s ]*", "") + return result +end + +local __TS__SymbolRegistryFor, __TS__SymbolRegistryKeyFor +do + local symbolRegistry = {} + function __TS__SymbolRegistryFor(key) + if not symbolRegistry[key] then + symbolRegistry[key] = __TS__Symbol(key) + end + return symbolRegistry[key] + end + function __TS__SymbolRegistryKeyFor(sym) + for key in pairs(symbolRegistry) do + if symbolRegistry[key] == sym then + return key + end + end + return nil + end +end + +local function __TS__TypeOf(value) + local luaType = type(value) + if luaType == "table" then + return "object" + elseif luaType == "nil" then + return "undefined" + else + return luaType + end +end + +local function __TS__Using(self, cb, ...) + local args = {...} + local thrownError + local ok, result = xpcall( + function() return cb( + nil, + __TS__Unpack(args) + ) end, + function(err) + thrownError = err + return thrownError + end + ) + local argArray = {__TS__Unpack(args)} + do + local i = #argArray - 1 + while i >= 0 do + local ____self_0 = argArray[i + 1] + ____self_0[Symbol.dispose](____self_0) + i = i - 1 + end + end + if not ok then + error(thrownError, 0) + end + return result +end + +local function __TS__UsingAsync(self, cb, ...) + local args = {...} + return __TS__AsyncAwaiter(function(____awaiter_resolve) + local thrownError + local ok, result = xpcall( + function() return cb( + nil, + __TS__Unpack(args) + ) end, + function(err) + thrownError = err + return thrownError + end + ) + local argArray = {__TS__Unpack(args)} + do + local i = #argArray - 1 + while i >= 0 do + if argArray[i + 1][Symbol.dispose] ~= nil then + local ____self_0 = argArray[i + 1] + ____self_0[Symbol.dispose](____self_0) + end + if argArray[i + 1][Symbol.asyncDispose] ~= nil then + local ____self_1 = argArray[i + 1] + __TS__Await(____self_1[Symbol.asyncDispose](____self_1)) + end + i = i - 1 + end + end + if not ok then + error(thrownError, 0) + end + return ____awaiter_resolve(nil, result) + end) +end + +return { + __TS__ArrayAt = __TS__ArrayAt, + __TS__ArrayConcat = __TS__ArrayConcat, + __TS__ArrayEntries = __TS__ArrayEntries, + __TS__ArrayEvery = __TS__ArrayEvery, + __TS__ArrayFill = __TS__ArrayFill, + __TS__ArrayFilter = __TS__ArrayFilter, + __TS__ArrayForEach = __TS__ArrayForEach, + __TS__ArrayFind = __TS__ArrayFind, + __TS__ArrayFindIndex = __TS__ArrayFindIndex, + __TS__ArrayFrom = __TS__ArrayFrom, + __TS__ArrayIncludes = __TS__ArrayIncludes, + __TS__ArrayIndexOf = __TS__ArrayIndexOf, + __TS__ArrayIsArray = __TS__ArrayIsArray, + __TS__ArrayJoin = __TS__ArrayJoin, + __TS__ArrayMap = __TS__ArrayMap, + __TS__ArrayPush = __TS__ArrayPush, + __TS__ArrayPushArray = __TS__ArrayPushArray, + __TS__ArrayReduce = __TS__ArrayReduce, + __TS__ArrayReduceRight = __TS__ArrayReduceRight, + __TS__ArrayReverse = __TS__ArrayReverse, + __TS__ArrayUnshift = __TS__ArrayUnshift, + __TS__ArraySort = __TS__ArraySort, + __TS__ArraySlice = __TS__ArraySlice, + __TS__ArraySome = __TS__ArraySome, + __TS__ArraySplice = __TS__ArraySplice, + __TS__ArrayToObject = __TS__ArrayToObject, + __TS__ArrayFlat = __TS__ArrayFlat, + __TS__ArrayFlatMap = __TS__ArrayFlatMap, + __TS__ArraySetLength = __TS__ArraySetLength, + __TS__ArrayToReversed = __TS__ArrayToReversed, + __TS__ArrayToSorted = __TS__ArrayToSorted, + __TS__ArrayToSpliced = __TS__ArrayToSpliced, + __TS__ArrayWith = __TS__ArrayWith, + __TS__AsyncAwaiter = __TS__AsyncAwaiter, + __TS__Await = __TS__Await, + __TS__Class = __TS__Class, + __TS__ClassExtends = __TS__ClassExtends, + __TS__CloneDescriptor = __TS__CloneDescriptor, + __TS__CountVarargs = __TS__CountVarargs, + __TS__Decorate = __TS__Decorate, + __TS__DecorateLegacy = __TS__DecorateLegacy, + __TS__DecorateParam = __TS__DecorateParam, + __TS__Delete = __TS__Delete, + __TS__DelegatedYield = __TS__DelegatedYield, + __TS__DescriptorGet = __TS__DescriptorGet, + __TS__DescriptorSet = __TS__DescriptorSet, + Error = Error, + RangeError = RangeError, + ReferenceError = ReferenceError, + SyntaxError = SyntaxError, + TypeError = TypeError, + URIError = URIError, + __TS__FunctionBind = __TS__FunctionBind, + __TS__Generator = __TS__Generator, + __TS__InstanceOf = __TS__InstanceOf, + __TS__InstanceOfObject = __TS__InstanceOfObject, + __TS__Iterator = __TS__Iterator, + __TS__LuaIteratorSpread = __TS__LuaIteratorSpread, + Map = Map, + __TS__Match = __TS__Match, + __TS__MathAtan2 = __TS__MathAtan2, + __TS__MathModf = __TS__MathModf, + __TS__MathSign = __TS__MathSign, + __TS__New = __TS__New, + __TS__Number = __TS__Number, + __TS__NumberIsFinite = __TS__NumberIsFinite, + __TS__NumberIsInteger = __TS__NumberIsInteger, + __TS__NumberIsNaN = __TS__NumberIsNaN, + __TS__ParseInt = __TS__ParseInt, + __TS__ParseFloat = __TS__ParseFloat, + __TS__NumberToString = __TS__NumberToString, + __TS__NumberToFixed = __TS__NumberToFixed, + __TS__ObjectAssign = __TS__ObjectAssign, + __TS__ObjectDefineProperty = __TS__ObjectDefineProperty, + __TS__ObjectEntries = __TS__ObjectEntries, + __TS__ObjectFromEntries = __TS__ObjectFromEntries, + __TS__ObjectGetOwnPropertyDescriptor = __TS__ObjectGetOwnPropertyDescriptor, + __TS__ObjectGetOwnPropertyDescriptors = __TS__ObjectGetOwnPropertyDescriptors, + __TS__ObjectKeys = __TS__ObjectKeys, + __TS__ObjectRest = __TS__ObjectRest, + __TS__ObjectValues = __TS__ObjectValues, + __TS__ParseFloat = __TS__ParseFloat, + __TS__ParseInt = __TS__ParseInt, + __TS__Promise = __TS__Promise, + __TS__PromiseAll = __TS__PromiseAll, + __TS__PromiseAllSettled = __TS__PromiseAllSettled, + __TS__PromiseAny = __TS__PromiseAny, + __TS__PromiseRace = __TS__PromiseRace, + Set = Set, + __TS__SetDescriptor = __TS__SetDescriptor, + __TS__SparseArrayNew = __TS__SparseArrayNew, + __TS__SparseArrayPush = __TS__SparseArrayPush, + __TS__SparseArraySpread = __TS__SparseArraySpread, + WeakMap = WeakMap, + WeakSet = WeakSet, + __TS__SourceMapTraceBack = __TS__SourceMapTraceBack, + __TS__Spread = __TS__Spread, + __TS__StringAccess = __TS__StringAccess, + __TS__StringCharAt = __TS__StringCharAt, + __TS__StringCharCodeAt = __TS__StringCharCodeAt, + __TS__StringEndsWith = __TS__StringEndsWith, + __TS__StringIncludes = __TS__StringIncludes, + __TS__StringPadEnd = __TS__StringPadEnd, + __TS__StringPadStart = __TS__StringPadStart, + __TS__StringReplace = __TS__StringReplace, + __TS__StringReplaceAll = __TS__StringReplaceAll, + __TS__StringSlice = __TS__StringSlice, + __TS__StringSplit = __TS__StringSplit, + __TS__StringStartsWith = __TS__StringStartsWith, + __TS__StringSubstr = __TS__StringSubstr, + __TS__StringSubstring = __TS__StringSubstring, + __TS__StringTrim = __TS__StringTrim, + __TS__StringTrimEnd = __TS__StringTrimEnd, + __TS__StringTrimStart = __TS__StringTrimStart, + __TS__Symbol = __TS__Symbol, + Symbol = Symbol, + __TS__SymbolRegistryFor = __TS__SymbolRegistryFor, + __TS__SymbolRegistryKeyFor = __TS__SymbolRegistryKeyFor, + __TS__TypeOf = __TS__TypeOf, + __TS__Unpack = __TS__Unpack, + __TS__Using = __TS__Using, + __TS__UsingAsync = __TS__UsingAsync +} diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..bcfa23d --- /dev/null +++ b/package-lock.json @@ -0,0 +1,198 @@ +{ + "name": "forem.nvim", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "forem.nvim", + "version": "1.0.0", + "license": "ISC", + "devDependencies": { + "lua-types": "^2.13.1", + "prettier": "^3.2.5", + "typescript": "^5.3.3", + "typescript-to-lua": "^1.24.1" + } + }, + "node_modules/@typescript-to-lua/language-extensions": { + "version": "1.19.0", + "resolved": "https://registry.npmjs.org/@typescript-to-lua/language-extensions/-/language-extensions-1.19.0.tgz", + "integrity": "sha512-Os5wOKwviTD4LeqI29N0btYOjokSJ97iCf45EOjIABlb5IwNQy7AE/AqZJobRw3ywHH8+KzJUMkEirWPzh2tUA==", + "dev": true + }, + "node_modules/enhanced-resolve": { + "version": "5.15.1", + "resolved": "https://registry.npmjs.org/enhanced-resolve/-/enhanced-resolve-5.15.1.tgz", + "integrity": "sha512-3d3JRbwsCLJsYgvb6NuWEG44jjPSOMuS73L/6+7BZuoKm3W+qXnSoIYVHi8dG7Qcg4inAY4jbzkZ7MnskePeDg==", + "dev": true, + "dependencies": { + "graceful-fs": "^4.2.4", + "tapable": "^2.2.0" + }, + "engines": { + "node": ">=10.13.0" + } + }, + "node_modules/function-bind": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", + "integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==", + "dev": true, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/graceful-fs": { + "version": "4.2.11", + "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.11.tgz", + "integrity": "sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==", + "dev": true + }, + "node_modules/hasown": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.1.tgz", + "integrity": "sha512-1/th4MHjnwncwXsIW6QMzlvYL9kG5e/CpVvLRZe4XPa8TOUNbCELqmvhDmnkNsAjwaG4+I8gJJL0JBvTTLO9qA==", + "dev": true, + "dependencies": { + "function-bind": "^1.1.2" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/is-core-module": { + "version": "2.13.1", + "resolved": "https://registry.npmjs.org/is-core-module/-/is-core-module-2.13.1.tgz", + "integrity": "sha512-hHrIjvZsftOsvKSn2TRYl63zvxsgE0K+0mYMoH6gD4omR5IWB2KynivBQczo3+wF1cCkjzvptnI9Q0sPU66ilw==", + "dev": true, + "dependencies": { + "hasown": "^2.0.0" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/lua-types": { + "version": "2.13.1", + "resolved": "https://registry.npmjs.org/lua-types/-/lua-types-2.13.1.tgz", + "integrity": "sha512-rRwtvX6kS+5MpuO3xpvKsnYjdSDDI064Qq1OqX8gY+r+0l7m3dFLiZPDFoHqH22jaBpEvcHcPs6+WD7qkdmFsA==", + "dev": true + }, + "node_modules/path-parse": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.7.tgz", + "integrity": "sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==", + "dev": true + }, + "node_modules/picomatch": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz", + "integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==", + "dev": true, + "engines": { + "node": ">=8.6" + }, + "funding": { + "url": "https://github.com/sponsors/jonschlinkert" + } + }, + "node_modules/prettier": { + "version": "3.2.5", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.2.5.tgz", + "integrity": "sha512-3/GWa9aOC0YeD7LUfvOG2NiDyhOWRvt1k+rcKhOuYnMY24iiCphgneUfJDyFXd6rZCAnuLBv6UeAULtrhT/F4A==", + "dev": true, + "bin": { + "prettier": "bin/prettier.cjs" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } + }, + "node_modules/resolve": { + "version": "1.22.8", + "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.22.8.tgz", + "integrity": "sha512-oKWePCxqpd6FlLvGV1VU0x7bkPmmCNolxzjMf4NczoDnQcIWrAF+cPtZn5i6n+RfD2d9i0tzpKnG6Yk168yIyw==", + "dev": true, + "dependencies": { + "is-core-module": "^2.13.0", + "path-parse": "^1.0.7", + "supports-preserve-symlinks-flag": "^1.0.0" + }, + "bin": { + "resolve": "bin/resolve" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/source-map": { + "version": "0.7.4", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.7.4.tgz", + "integrity": "sha512-l3BikUxvPOcn5E74dZiq5BGsTb5yEwhaTSzccU6t4sDOH8NWJCstKO5QT2CvtFoK6F0saL7p9xHAqHOlCPJygA==", + "dev": true, + "engines": { + "node": ">= 8" + } + }, + "node_modules/supports-preserve-symlinks-flag": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz", + "integrity": "sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==", + "dev": true, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/tapable": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/tapable/-/tapable-2.2.1.tgz", + "integrity": "sha512-GNzQvQTOIP6RyTfE2Qxb8ZVlNmw0n88vp1szwWRimP02mnTsx3Wtn5qRdqY9w2XduFNUgvOwhNnQsjwCp+kqaQ==", + "dev": true, + "engines": { + "node": ">=6" + } + }, + "node_modules/typescript": { + "version": "5.3.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.3.3.tgz", + "integrity": "sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw==", + "dev": true, + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + }, + "node_modules/typescript-to-lua": { + "version": "1.24.1", + "resolved": "https://registry.npmjs.org/typescript-to-lua/-/typescript-to-lua-1.24.1.tgz", + "integrity": "sha512-IqHKVMkgRk9cd8S8nhu5wKP+AiK9QeCJ9OUphKcNzQMNpEirrQKHvbQwoeVNWNsIaiCUvrdM1SuyxDX1B3TRZw==", + "dev": true, + "dependencies": { + "@typescript-to-lua/language-extensions": "1.19.0", + "enhanced-resolve": "^5.8.2", + "picomatch": "^2.3.1", + "resolve": "^1.15.1", + "source-map": "^0.7.3" + }, + "bin": { + "tstl": "dist/tstl.js" + }, + "engines": { + "node": ">=16.10.0" + }, + "peerDependencies": { + "typescript": "5.3.3" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..26b0ba7 --- /dev/null +++ b/package.json @@ -0,0 +1,22 @@ +{ + "name": "forem.nvim", + "version": "1.0.0", + "description": "This plugin integrates Neovim with Forem platforms (for example, dev.to)", + "main": "index.js", + "directories": { + "test": "tests" + }, + "scripts": { + "build": "make", + "dev": "tstl -p ./src/tsconfig.json --watch" + }, + "keywords": [], + "author": "", + "license": "ISC", + "devDependencies": { + "lua-types": "^2.13.1", + "prettier": "^3.2.5", + "typescript": "^5.3.3", + "typescript-to-lua": "^1.24.1" + } +} diff --git a/plugin/forem-nvim.fnl b/plugin/forem-nvim.fnl deleted file mode 100644 index 5b665a1..0000000 --- a/plugin/forem-nvim.fnl +++ /dev/null @@ -1,12 +0,0 @@ -(local forem (require :forem-nvim)) -(local notify (require :forem-nvim.notify)) - -(local commands [:feed :my_articles :new_article :open_by_url]) - -(vim.api.nvim_create_user_command :Forem - (fn [{: args}] - (if (vim.tbl_contains commands args) - ((. forem args)) - (notify.error (.. "Unknown command: " - args)))) - {:nargs 1 :complete (fn [] commands)}) diff --git a/plugin/forem-nvim.lua b/plugin/forem-nvim.lua index 887c931..500a489 100644 --- a/plugin/forem-nvim.lua +++ b/plugin/forem-nvim.lua @@ -1,16 +1,52 @@ +local ____lualib = require("lualib_bundle") +local __TS__ObjectValues = ____lualib.__TS__ObjectValues +local __TS__ArrayIncludes = ____lualib.__TS__ArrayIncludes +local ____exports = {} local forem = require("forem-nvim") local notify = require("forem-nvim.notify") -local commands = {"feed", "my_articles", "new_article", "open_by_url"} -local function _3_(_1_) - local _arg_2_ = _1_ - local args = _arg_2_["args"] - if vim.tbl_contains(commands, args) then - return forem[args]() - else - return notify.error(("Unknown command: " .. args)) - end +local Command = Command or ({}) +Command.Feed = "feed" +Command.MyArticles = "my_articles" +Command.NewArticle = "new_article" +Command.OpenUrl = "open_url" +local function isCommand(command) + return __TS__ArrayIncludes( + __TS__ObjectValues(Command), + command + ) end -local function _5_() - return commands -end -return vim.api.nvim_create_user_command("Forem", _3_, {nargs = 1, complete = _5_}) +vim.api.nvim_create_user_command( + "Forem", + function(____bindingPattern0) + local args + args = ____bindingPattern0.args + if not isCommand(args) then + notify.error("Unknown command: " .. args) + return + end + repeat + local ____switch5 = args + local ____cond5 = ____switch5 == Command.Feed + if ____cond5 then + return forem.feed() + end + ____cond5 = ____cond5 or ____switch5 == Command.MyArticles + if ____cond5 then + return forem.my_articles() + end + ____cond5 = ____cond5 or ____switch5 == Command.NewArticle + if ____cond5 then + return forem.new_article() + end + ____cond5 = ____cond5 or ____switch5 == Command.OpenUrl + if ____cond5 then + return forem.open_url() + end + until true + end, + { + nargs = 1, + complete = function() return __TS__ObjectValues(Command) end + } +) +return ____exports diff --git a/plugin/forem-nvim.ts b/plugin/forem-nvim.ts new file mode 100644 index 0000000..adc61ea --- /dev/null +++ b/plugin/forem-nvim.ts @@ -0,0 +1,34 @@ +import * as forem from "forem-nvim"; +import * as notify from "forem-nvim.notify"; + +enum Command { + Feed = "feed", + MyArticles = "my_articles", + NewArticle = "new_article", + OpenUrl = "open_url", +} + +const isCommand = (command: string): command is Command => + Object.values(Command).includes(command as Command); + +vim.api.nvim_create_user_command( + "Forem", + function ({ args }) { + if (!isCommand(args)) { + notify.error(`Unknown command: ${args}`); + return; + } + + switch (args) { + case Command.Feed: + return forem.feed(); + case Command.MyArticles: + return forem.my_articles(); + case Command.NewArticle: + return forem.new_article(); + case Command.OpenUrl: + return forem.open_url(); + } + }, + { nargs: 1, complete: () => Object.values(Command) }, +); diff --git a/plugin/lualib_bundle.lua b/plugin/lualib_bundle.lua new file mode 100644 index 0000000..100c890 --- /dev/null +++ b/plugin/lualib_bundle.lua @@ -0,0 +1,2629 @@ +local function __TS__ArrayAt(self, relativeIndex) + local absoluteIndex = relativeIndex < 0 and #self + relativeIndex or relativeIndex + if absoluteIndex >= 0 and absoluteIndex < #self then + return self[absoluteIndex + 1] + end + return nil +end + +local function __TS__ArrayIsArray(value) + return type(value) == "table" and (value[1] ~= nil or next(value) == nil) +end + +local function __TS__ArrayConcat(self, ...) + local items = {...} + local result = {} + local len = 0 + for i = 1, #self do + len = len + 1 + result[len] = self[i] + end + for i = 1, #items do + local item = items[i] + if __TS__ArrayIsArray(item) then + for j = 1, #item do + len = len + 1 + result[len] = item[j] + end + else + len = len + 1 + result[len] = item + end + end + return result +end + +local __TS__Symbol, Symbol +do + local symbolMetatable = {__tostring = function(self) + return ("Symbol(" .. (self.description or "")) .. ")" + end} + function __TS__Symbol(description) + return setmetatable({description = description}, symbolMetatable) + end + Symbol = { + asyncDispose = __TS__Symbol("Symbol.asyncDispose"), + dispose = __TS__Symbol("Symbol.dispose"), + iterator = __TS__Symbol("Symbol.iterator"), + hasInstance = __TS__Symbol("Symbol.hasInstance"), + species = __TS__Symbol("Symbol.species"), + toStringTag = __TS__Symbol("Symbol.toStringTag") + } +end + +local function __TS__ArrayEntries(array) + local key = 0 + return { + [Symbol.iterator] = function(self) + return self + end, + next = function(self) + local result = {done = array[key + 1] == nil, value = {key, array[key + 1]}} + key = key + 1 + return result + end + } +end + +local function __TS__ArrayEvery(self, callbackfn, thisArg) + for i = 1, #self do + if not callbackfn(thisArg, self[i], i - 1, self) then + return false + end + end + return true +end + +local function __TS__ArrayFill(self, value, start, ____end) + local relativeStart = start or 0 + local relativeEnd = ____end or #self + if relativeStart < 0 then + relativeStart = relativeStart + #self + end + if relativeEnd < 0 then + relativeEnd = relativeEnd + #self + end + do + local i = relativeStart + while i < relativeEnd do + self[i + 1] = value + i = i + 1 + end + end + return self +end + +local function __TS__ArrayFilter(self, callbackfn, thisArg) + local result = {} + local len = 0 + for i = 1, #self do + if callbackfn(thisArg, self[i], i - 1, self) then + len = len + 1 + result[len] = self[i] + end + end + return result +end + +local function __TS__ArrayForEach(self, callbackFn, thisArg) + for i = 1, #self do + callbackFn(thisArg, self[i], i - 1, self) + end +end + +local function __TS__ArrayFind(self, predicate, thisArg) + for i = 1, #self do + local elem = self[i] + if predicate(thisArg, elem, i - 1, self) then + return elem + end + end + return nil +end + +local function __TS__ArrayFindIndex(self, callbackFn, thisArg) + for i = 1, #self do + if callbackFn(thisArg, self[i], i - 1, self) then + return i - 1 + end + end + return -1 +end + +local __TS__Iterator +do + local function iteratorGeneratorStep(self) + local co = self.____coroutine + local status, value = coroutine.resume(co) + if not status then + error(value, 0) + end + if coroutine.status(co) == "dead" then + return + end + return true, value + end + local function iteratorIteratorStep(self) + local result = self:next() + if result.done then + return + end + return true, result.value + end + local function iteratorStringStep(self, index) + index = index + 1 + if index > #self then + return + end + return index, string.sub(self, index, index) + end + function __TS__Iterator(iterable) + if type(iterable) == "string" then + return iteratorStringStep, iterable, 0 + elseif iterable.____coroutine ~= nil then + return iteratorGeneratorStep, iterable + elseif iterable[Symbol.iterator] then + local iterator = iterable[Symbol.iterator](iterable) + return iteratorIteratorStep, iterator + else + return ipairs(iterable) + end + end +end + +local __TS__ArrayFrom +do + local function arrayLikeStep(self, index) + index = index + 1 + if index > self.length then + return + end + return index, self[index] + end + local function arrayLikeIterator(arr) + if type(arr.length) == "number" then + return arrayLikeStep, arr, 0 + end + return __TS__Iterator(arr) + end + function __TS__ArrayFrom(arrayLike, mapFn, thisArg) + local result = {} + if mapFn == nil then + for ____, v in arrayLikeIterator(arrayLike) do + result[#result + 1] = v + end + else + for i, v in arrayLikeIterator(arrayLike) do + result[#result + 1] = mapFn(thisArg, v, i - 1) + end + end + return result + end +end + +local function __TS__ArrayIncludes(self, searchElement, fromIndex) + if fromIndex == nil then + fromIndex = 0 + end + local len = #self + local k = fromIndex + if fromIndex < 0 then + k = len + fromIndex + end + if k < 0 then + k = 0 + end + for i = k + 1, len do + if self[i] == searchElement then + return true + end + end + return false +end + +local function __TS__ArrayIndexOf(self, searchElement, fromIndex) + if fromIndex == nil then + fromIndex = 0 + end + local len = #self + if len == 0 then + return -1 + end + if fromIndex >= len then + return -1 + end + if fromIndex < 0 then + fromIndex = len + fromIndex + if fromIndex < 0 then + fromIndex = 0 + end + end + for i = fromIndex + 1, len do + if self[i] == searchElement then + return i - 1 + end + end + return -1 +end + +local function __TS__ArrayJoin(self, separator) + if separator == nil then + separator = "," + end + local parts = {} + for i = 1, #self do + parts[i] = tostring(self[i]) + end + return table.concat(parts, separator) +end + +local function __TS__ArrayMap(self, callbackfn, thisArg) + local result = {} + for i = 1, #self do + result[i] = callbackfn(thisArg, self[i], i - 1, self) + end + return result +end + +local function __TS__ArrayPush(self, ...) + local items = {...} + local len = #self + for i = 1, #items do + len = len + 1 + self[len] = items[i] + end + return len +end + +local function __TS__ArrayPushArray(self, items) + local len = #self + for i = 1, #items do + len = len + 1 + self[len] = items[i] + end + return len +end + +local function __TS__CountVarargs(...) + return select("#", ...) +end + +local function __TS__ArrayReduce(self, callbackFn, ...) + local len = #self + local k = 0 + local accumulator = nil + if __TS__CountVarargs(...) ~= 0 then + accumulator = ... + elseif len > 0 then + accumulator = self[1] + k = 1 + else + error("Reduce of empty array with no initial value", 0) + end + for i = k + 1, len do + accumulator = callbackFn( + nil, + accumulator, + self[i], + i - 1, + self + ) + end + return accumulator +end + +local function __TS__ArrayReduceRight(self, callbackFn, ...) + local len = #self + local k = len - 1 + local accumulator = nil + if __TS__CountVarargs(...) ~= 0 then + accumulator = ... + elseif len > 0 then + accumulator = self[k + 1] + k = k - 1 + else + error("Reduce of empty array with no initial value", 0) + end + for i = k + 1, 1, -1 do + accumulator = callbackFn( + nil, + accumulator, + self[i], + i - 1, + self + ) + end + return accumulator +end + +local function __TS__ArrayReverse(self) + local i = 1 + local j = #self + while i < j do + local temp = self[j] + self[j] = self[i] + self[i] = temp + i = i + 1 + j = j - 1 + end + return self +end + +local function __TS__ArrayUnshift(self, ...) + local items = {...} + local numItemsToInsert = #items + if numItemsToInsert == 0 then + return #self + end + for i = #self, 1, -1 do + self[i + numItemsToInsert] = self[i] + end + for i = 1, numItemsToInsert do + self[i] = items[i] + end + return #self +end + +local function __TS__ArraySort(self, compareFn) + if compareFn ~= nil then + table.sort( + self, + function(a, b) return compareFn(nil, a, b) < 0 end + ) + else + table.sort(self) + end + return self +end + +local function __TS__ArraySlice(self, first, last) + local len = #self + first = first or 0 + if first < 0 then + first = len + first + if first < 0 then + first = 0 + end + else + if first > len then + first = len + end + end + last = last or len + if last < 0 then + last = len + last + if last < 0 then + last = 0 + end + else + if last > len then + last = len + end + end + local out = {} + first = first + 1 + last = last + 1 + local n = 1 + while first < last do + out[n] = self[first] + first = first + 1 + n = n + 1 + end + return out +end + +local function __TS__ArraySome(self, callbackfn, thisArg) + for i = 1, #self do + if callbackfn(thisArg, self[i], i - 1, self) then + return true + end + end + return false +end + +local function __TS__ArraySplice(self, ...) + local args = {...} + local len = #self + local actualArgumentCount = __TS__CountVarargs(...) + local start = args[1] + local deleteCount = args[2] + if start < 0 then + start = len + start + if start < 0 then + start = 0 + end + elseif start > len then + start = len + end + local itemCount = actualArgumentCount - 2 + if itemCount < 0 then + itemCount = 0 + end + local actualDeleteCount + if actualArgumentCount == 0 then + actualDeleteCount = 0 + elseif actualArgumentCount == 1 then + actualDeleteCount = len - start + else + actualDeleteCount = deleteCount or 0 + if actualDeleteCount < 0 then + actualDeleteCount = 0 + end + if actualDeleteCount > len - start then + actualDeleteCount = len - start + end + end + local out = {} + for k = 1, actualDeleteCount do + local from = start + k + if self[from] ~= nil then + out[k] = self[from] + end + end + if itemCount < actualDeleteCount then + for k = start + 1, len - actualDeleteCount do + local from = k + actualDeleteCount + local to = k + itemCount + if self[from] then + self[to] = self[from] + else + self[to] = nil + end + end + for k = len - actualDeleteCount + itemCount + 1, len do + self[k] = nil + end + elseif itemCount > actualDeleteCount then + for k = len - actualDeleteCount, start + 1, -1 do + local from = k + actualDeleteCount + local to = k + itemCount + if self[from] then + self[to] = self[from] + else + self[to] = nil + end + end + end + local j = start + 1 + for i = 3, actualArgumentCount do + self[j] = args[i] + j = j + 1 + end + for k = #self, len - actualDeleteCount + itemCount + 1, -1 do + self[k] = nil + end + return out +end + +local function __TS__ArrayToObject(self) + local object = {} + for i = 1, #self do + object[i - 1] = self[i] + end + return object +end + +local function __TS__ArrayFlat(self, depth) + if depth == nil then + depth = 1 + end + local result = {} + local len = 0 + for i = 1, #self do + local value = self[i] + if depth > 0 and __TS__ArrayIsArray(value) then + local toAdd + if depth == 1 then + toAdd = value + else + toAdd = __TS__ArrayFlat(value, depth - 1) + end + for j = 1, #toAdd do + local val = toAdd[j] + len = len + 1 + result[len] = val + end + else + len = len + 1 + result[len] = value + end + end + return result +end + +local function __TS__ArrayFlatMap(self, callback, thisArg) + local result = {} + local len = 0 + for i = 1, #self do + local value = callback(thisArg, self[i], i - 1, self) + if __TS__ArrayIsArray(value) then + for j = 1, #value do + len = len + 1 + result[len] = value[j] + end + else + len = len + 1 + result[len] = value + end + end + return result +end + +local function __TS__ArraySetLength(self, length) + if length < 0 or length ~= length or length == math.huge or math.floor(length) ~= length then + error( + "invalid array length: " .. tostring(length), + 0 + ) + end + for i = length + 1, #self do + self[i] = nil + end + return length +end + +local __TS__Unpack = table.unpack or unpack + +local function __TS__ArrayToReversed(self) + local copy = {__TS__Unpack(self)} + __TS__ArrayReverse(copy) + return copy +end + +local function __TS__ArrayToSorted(self, compareFn) + local copy = {__TS__Unpack(self)} + __TS__ArraySort(copy, compareFn) + return copy +end + +local function __TS__ArrayToSpliced(self, start, deleteCount, ...) + local copy = {__TS__Unpack(self)} + __TS__ArraySplice(copy, start, deleteCount, ...) + return copy +end + +local function __TS__ArrayWith(self, index, value) + local copy = {__TS__Unpack(self)} + copy[index + 1] = value + return copy +end + +local function __TS__New(target, ...) + local instance = setmetatable({}, target.prototype) + instance:____constructor(...) + return instance +end + +local function __TS__InstanceOf(obj, classTbl) + if type(classTbl) ~= "table" then + error("Right-hand side of 'instanceof' is not an object", 0) + end + if classTbl[Symbol.hasInstance] ~= nil then + return not not classTbl[Symbol.hasInstance](classTbl, obj) + end + if type(obj) == "table" then + local luaClass = obj.constructor + while luaClass ~= nil do + if luaClass == classTbl then + return true + end + luaClass = luaClass.____super + end + end + return false +end + +local function __TS__Class(self) + local c = {prototype = {}} + c.prototype.__index = c.prototype + c.prototype.constructor = c + return c +end + +local __TS__Promise +do + local function makeDeferredPromiseFactory() + local resolve + local reject + local function executor(____, res, rej) + resolve = res + reject = rej + end + return function() + local promise = __TS__New(__TS__Promise, executor) + return promise, resolve, reject + end + end + local makeDeferredPromise = makeDeferredPromiseFactory() + local function isPromiseLike(value) + return __TS__InstanceOf(value, __TS__Promise) + end + local function doNothing(self) + end + local ____pcall = _G.pcall + __TS__Promise = __TS__Class() + __TS__Promise.name = "__TS__Promise" + function __TS__Promise.prototype.____constructor(self, executor) + self.state = 0 + self.fulfilledCallbacks = {} + self.rejectedCallbacks = {} + self.finallyCallbacks = {} + local success, ____error = ____pcall( + executor, + nil, + function(____, v) return self:resolve(v) end, + function(____, err) return self:reject(err) end + ) + if not success then + self:reject(____error) + end + end + function __TS__Promise.resolve(value) + if __TS__InstanceOf(value, __TS__Promise) then + return value + end + local promise = __TS__New(__TS__Promise, doNothing) + promise.state = 1 + promise.value = value + return promise + end + function __TS__Promise.reject(reason) + local promise = __TS__New(__TS__Promise, doNothing) + promise.state = 2 + promise.rejectionReason = reason + return promise + end + __TS__Promise.prototype["then"] = function(self, onFulfilled, onRejected) + local promise, resolve, reject = makeDeferredPromise() + self:addCallbacks( + onFulfilled and self:createPromiseResolvingCallback(onFulfilled, resolve, reject) or resolve, + onRejected and self:createPromiseResolvingCallback(onRejected, resolve, reject) or reject + ) + return promise + end + function __TS__Promise.prototype.addCallbacks(self, fulfilledCallback, rejectedCallback) + if self.state == 1 then + return fulfilledCallback(nil, self.value) + end + if self.state == 2 then + return rejectedCallback(nil, self.rejectionReason) + end + local ____self_fulfilledCallbacks_0 = self.fulfilledCallbacks + ____self_fulfilledCallbacks_0[#____self_fulfilledCallbacks_0 + 1] = fulfilledCallback + local ____self_rejectedCallbacks_1 = self.rejectedCallbacks + ____self_rejectedCallbacks_1[#____self_rejectedCallbacks_1 + 1] = rejectedCallback + end + function __TS__Promise.prototype.catch(self, onRejected) + return self["then"](self, nil, onRejected) + end + function __TS__Promise.prototype.finally(self, onFinally) + if onFinally then + local ____self_finallyCallbacks_2 = self.finallyCallbacks + ____self_finallyCallbacks_2[#____self_finallyCallbacks_2 + 1] = onFinally + if self.state ~= 0 then + onFinally(nil) + end + end + return self + end + function __TS__Promise.prototype.resolve(self, value) + if isPromiseLike(value) then + return value:addCallbacks( + function(____, v) return self:resolve(v) end, + function(____, err) return self:reject(err) end + ) + end + if self.state == 0 then + self.state = 1 + self.value = value + return self:invokeCallbacks(self.fulfilledCallbacks, value) + end + end + function __TS__Promise.prototype.reject(self, reason) + if self.state == 0 then + self.state = 2 + self.rejectionReason = reason + return self:invokeCallbacks(self.rejectedCallbacks, reason) + end + end + function __TS__Promise.prototype.invokeCallbacks(self, callbacks, value) + local callbacksLength = #callbacks + local finallyCallbacks = self.finallyCallbacks + local finallyCallbacksLength = #finallyCallbacks + if callbacksLength ~= 0 then + for i = 1, callbacksLength - 1 do + callbacks[i](callbacks, value) + end + if finallyCallbacksLength == 0 then + return callbacks[callbacksLength](callbacks, value) + end + callbacks[callbacksLength](callbacks, value) + end + if finallyCallbacksLength ~= 0 then + for i = 1, finallyCallbacksLength - 1 do + finallyCallbacks[i](finallyCallbacks) + end + return finallyCallbacks[finallyCallbacksLength](finallyCallbacks) + end + end + function __TS__Promise.prototype.createPromiseResolvingCallback(self, f, resolve, reject) + return function(____, value) + local success, resultOrError = ____pcall(f, nil, value) + if not success then + return reject(nil, resultOrError) + end + return self:handleCallbackValue(resultOrError, resolve, reject) + end + end + function __TS__Promise.prototype.handleCallbackValue(self, value, resolve, reject) + if isPromiseLike(value) then + local nextpromise = value + if nextpromise.state == 1 then + return resolve(nil, nextpromise.value) + elseif nextpromise.state == 2 then + return reject(nil, nextpromise.rejectionReason) + else + return nextpromise:addCallbacks(resolve, reject) + end + else + return resolve(nil, value) + end + end +end + +local __TS__AsyncAwaiter, __TS__Await +do + local cocreate = coroutine.create + local coresume = coroutine.resume + local costatus = coroutine.status + local coyield = coroutine.yield + function __TS__AsyncAwaiter(generator) + return __TS__New( + __TS__Promise, + function(____, resolve, reject) + local fulfilled, step, resolved, asyncCoroutine + function fulfilled(self, value) + local success, resultOrError = coresume(asyncCoroutine, value) + if success then + return step(resultOrError) + end + return reject(nil, resultOrError) + end + function step(result) + if resolved then + return + end + if costatus(asyncCoroutine) == "dead" then + return resolve(nil, result) + end + return __TS__Promise.resolve(result):addCallbacks(fulfilled, reject) + end + resolved = false + asyncCoroutine = cocreate(generator) + local success, resultOrError = coresume( + asyncCoroutine, + function(____, v) + resolved = true + return __TS__Promise.resolve(v):addCallbacks(resolve, reject) + end + ) + if success then + return step(resultOrError) + else + return reject(nil, resultOrError) + end + end + ) + end + function __TS__Await(thing) + return coyield(thing) + end +end + +local function __TS__ClassExtends(target, base) + target.____super = base + local staticMetatable = setmetatable({__index = base}, base) + setmetatable(target, staticMetatable) + local baseMetatable = getmetatable(base) + if baseMetatable then + if type(baseMetatable.__index) == "function" then + staticMetatable.__index = baseMetatable.__index + end + if type(baseMetatable.__newindex) == "function" then + staticMetatable.__newindex = baseMetatable.__newindex + end + end + setmetatable(target.prototype, base.prototype) + if type(base.prototype.__index) == "function" then + target.prototype.__index = base.prototype.__index + end + if type(base.prototype.__newindex) == "function" then + target.prototype.__newindex = base.prototype.__newindex + end + if type(base.prototype.__tostring) == "function" then + target.prototype.__tostring = base.prototype.__tostring + end +end + +local function __TS__CloneDescriptor(____bindingPattern0) + local value + local writable + local set + local get + local configurable + local enumerable + enumerable = ____bindingPattern0.enumerable + configurable = ____bindingPattern0.configurable + get = ____bindingPattern0.get + set = ____bindingPattern0.set + writable = ____bindingPattern0.writable + value = ____bindingPattern0.value + local descriptor = {enumerable = enumerable == true, configurable = configurable == true} + local hasGetterOrSetter = get ~= nil or set ~= nil + local hasValueOrWritableAttribute = writable ~= nil or value ~= nil + if hasGetterOrSetter and hasValueOrWritableAttribute then + error("Invalid property descriptor. Cannot both specify accessors and a value or writable attribute.", 0) + end + if get or set then + descriptor.get = get + descriptor.set = set + else + descriptor.value = value + descriptor.writable = writable == true + end + return descriptor +end + +local function __TS__Decorate(self, originalValue, decorators, context) + local result = originalValue + do + local i = #decorators + while i >= 0 do + local decorator = decorators[i + 1] + if decorator ~= nil then + local ____decorator_result_0 = decorator(self, result, context) + if ____decorator_result_0 == nil then + ____decorator_result_0 = result + end + result = ____decorator_result_0 + end + i = i - 1 + end + end + return result +end + +local function __TS__ObjectAssign(target, ...) + local sources = {...} + for i = 1, #sources do + local source = sources[i] + for key in pairs(source) do + target[key] = source[key] + end + end + return target +end + +local function __TS__ObjectGetOwnPropertyDescriptor(object, key) + local metatable = getmetatable(object) + if not metatable then + return + end + if not rawget(metatable, "_descriptors") then + return + end + return rawget(metatable, "_descriptors")[key] +end + +local __TS__DescriptorGet +do + local getmetatable = _G.getmetatable + local ____rawget = _G.rawget + function __TS__DescriptorGet(self, metatable, key) + while metatable do + local rawResult = ____rawget(metatable, key) + if rawResult ~= nil then + return rawResult + end + local descriptors = ____rawget(metatable, "_descriptors") + if descriptors then + local descriptor = descriptors[key] + if descriptor ~= nil then + if descriptor.get then + return descriptor.get(self) + end + return descriptor.value + end + end + metatable = getmetatable(metatable) + end + end +end + +local __TS__DescriptorSet +do + local getmetatable = _G.getmetatable + local ____rawget = _G.rawget + local rawset = _G.rawset + function __TS__DescriptorSet(self, metatable, key, value) + while metatable do + local descriptors = ____rawget(metatable, "_descriptors") + if descriptors then + local descriptor = descriptors[key] + if descriptor ~= nil then + if descriptor.set then + descriptor.set(self, value) + else + if descriptor.writable == false then + error( + ((("Cannot assign to read only property '" .. key) .. "' of object '") .. tostring(self)) .. "'", + 0 + ) + end + descriptor.value = value + end + return + end + end + metatable = getmetatable(metatable) + end + rawset(self, key, value) + end +end + +local __TS__SetDescriptor +do + local getmetatable = _G.getmetatable + local function descriptorIndex(self, key) + return __TS__DescriptorGet( + self, + getmetatable(self), + key + ) + end + local function descriptorNewIndex(self, key, value) + return __TS__DescriptorSet( + self, + getmetatable(self), + key, + value + ) + end + function __TS__SetDescriptor(target, key, desc, isPrototype) + if isPrototype == nil then + isPrototype = false + end + local ____isPrototype_0 + if isPrototype then + ____isPrototype_0 = target + else + ____isPrototype_0 = getmetatable(target) + end + local metatable = ____isPrototype_0 + if not metatable then + metatable = {} + setmetatable(target, metatable) + end + local value = rawget(target, key) + if value ~= nil then + rawset(target, key, nil) + end + if not rawget(metatable, "_descriptors") then + metatable._descriptors = {} + end + metatable._descriptors[key] = __TS__CloneDescriptor(desc) + metatable.__index = descriptorIndex + metatable.__newindex = descriptorNewIndex + end +end + +local function __TS__DecorateLegacy(decorators, target, key, desc) + local result = target + do + local i = #decorators + while i >= 0 do + local decorator = decorators[i + 1] + if decorator ~= nil then + local oldResult = result + if key == nil then + result = decorator(nil, result) + elseif desc == true then + local value = rawget(target, key) + local descriptor = __TS__ObjectGetOwnPropertyDescriptor(target, key) or ({configurable = true, writable = true, value = value}) + local desc = decorator(nil, target, key, descriptor) or descriptor + local isSimpleValue = desc.configurable == true and desc.writable == true and not desc.get and not desc.set + if isSimpleValue then + rawset(target, key, desc.value) + else + __TS__SetDescriptor( + target, + key, + __TS__ObjectAssign({}, descriptor, desc) + ) + end + elseif desc == false then + result = decorator(nil, target, key, desc) + else + result = decorator(nil, target, key) + end + result = result or oldResult + end + i = i - 1 + end + end + return result +end + +local function __TS__DecorateParam(paramIndex, decorator) + return function(____, target, key) return decorator(nil, target, key, paramIndex) end +end + +local function __TS__StringIncludes(self, searchString, position) + if not position then + position = 1 + else + position = position + 1 + end + local index = string.find(self, searchString, position, true) + return index ~= nil +end + +local Error, RangeError, ReferenceError, SyntaxError, TypeError, URIError +do + local function getErrorStack(self, constructor) + if debug == nil then + return nil + end + local level = 1 + while true do + local info = debug.getinfo(level, "f") + level = level + 1 + if not info then + level = 1 + break + elseif info.func == constructor then + break + end + end + if __TS__StringIncludes(_VERSION, "Lua 5.0") then + return debug.traceback(("[Level " .. tostring(level)) .. "]") + else + return debug.traceback(nil, level) + end + end + local function wrapErrorToString(self, getDescription) + return function(self) + local description = getDescription(self) + local caller = debug.getinfo(3, "f") + local isClassicLua = __TS__StringIncludes(_VERSION, "Lua 5.0") or _VERSION == "Lua 5.1" + if isClassicLua or caller and caller.func ~= error then + return description + else + return (description .. "\n") .. tostring(self.stack) + end + end + end + local function initErrorClass(self, Type, name) + Type.name = name + return setmetatable( + Type, + {__call = function(____, _self, message) return __TS__New(Type, message) end} + ) + end + local ____initErrorClass_1 = initErrorClass + local ____class_0 = __TS__Class() + ____class_0.name = "" + function ____class_0.prototype.____constructor(self, message) + if message == nil then + message = "" + end + self.message = message + self.name = "Error" + self.stack = getErrorStack(nil, self.constructor.new) + local metatable = getmetatable(self) + if metatable and not metatable.__errorToStringPatched then + metatable.__errorToStringPatched = true + metatable.__tostring = wrapErrorToString(nil, metatable.__tostring) + end + end + function ____class_0.prototype.__tostring(self) + return self.message ~= "" and (self.name .. ": ") .. self.message or self.name + end + Error = ____initErrorClass_1(nil, ____class_0, "Error") + local function createErrorClass(self, name) + local ____initErrorClass_3 = initErrorClass + local ____class_2 = __TS__Class() + ____class_2.name = ____class_2.name + __TS__ClassExtends(____class_2, Error) + function ____class_2.prototype.____constructor(self, ...) + ____class_2.____super.prototype.____constructor(self, ...) + self.name = name + end + return ____initErrorClass_3(nil, ____class_2, name) + end + RangeError = createErrorClass(nil, "RangeError") + ReferenceError = createErrorClass(nil, "ReferenceError") + SyntaxError = createErrorClass(nil, "SyntaxError") + TypeError = createErrorClass(nil, "TypeError") + URIError = createErrorClass(nil, "URIError") +end + +local function __TS__ObjectGetOwnPropertyDescriptors(object) + local metatable = getmetatable(object) + if not metatable then + return {} + end + return rawget(metatable, "_descriptors") or ({}) +end + +local function __TS__Delete(target, key) + local descriptors = __TS__ObjectGetOwnPropertyDescriptors(target) + local descriptor = descriptors[key] + if descriptor then + if not descriptor.configurable then + error( + __TS__New( + TypeError, + ((("Cannot delete property " .. tostring(key)) .. " of ") .. tostring(target)) .. "." + ), + 0 + ) + end + descriptors[key] = nil + return true + end + target[key] = nil + return true +end + +local function __TS__StringAccess(self, index) + if index >= 0 and index < #self then + return string.sub(self, index + 1, index + 1) + end +end + +local function __TS__DelegatedYield(iterable) + if type(iterable) == "string" then + for index = 0, #iterable - 1 do + coroutine.yield(__TS__StringAccess(iterable, index)) + end + elseif iterable.____coroutine ~= nil then + local co = iterable.____coroutine + while true do + local status, value = coroutine.resume(co) + if not status then + error(value, 0) + end + if coroutine.status(co) == "dead" then + return value + else + coroutine.yield(value) + end + end + elseif iterable[Symbol.iterator] then + local iterator = iterable[Symbol.iterator](iterable) + while true do + local result = iterator:next() + if result.done then + return result.value + else + coroutine.yield(result.value) + end + end + else + for ____, value in ipairs(iterable) do + coroutine.yield(value) + end + end +end + +local function __TS__FunctionBind(fn, ...) + local boundArgs = {...} + return function(____, ...) + local args = {...} + __TS__ArrayUnshift( + args, + __TS__Unpack(boundArgs) + ) + return fn(__TS__Unpack(args)) + end +end + +local __TS__Generator +do + local function generatorIterator(self) + return self + end + local function generatorNext(self, ...) + local co = self.____coroutine + if coroutine.status(co) == "dead" then + return {done = true} + end + local status, value = coroutine.resume(co, ...) + if not status then + error(value, 0) + end + return { + value = value, + done = coroutine.status(co) == "dead" + } + end + function __TS__Generator(fn) + return function(...) + local args = {...} + local argsLength = __TS__CountVarargs(...) + return { + ____coroutine = coroutine.create(function() return fn(__TS__Unpack(args, 1, argsLength)) end), + [Symbol.iterator] = generatorIterator, + next = generatorNext + } + end + end +end + +local function __TS__InstanceOfObject(value) + local valueType = type(value) + return valueType == "table" or valueType == "function" +end + +local function __TS__LuaIteratorSpread(self, state, firstKey) + local results = {} + local key, value = self(state, firstKey) + while key do + results[#results + 1] = {key, value} + key, value = self(state, key) + end + return __TS__Unpack(results) +end + +local Map +do + Map = __TS__Class() + Map.name = "Map" + function Map.prototype.____constructor(self, entries) + self[Symbol.toStringTag] = "Map" + self.items = {} + self.size = 0 + self.nextKey = {} + self.previousKey = {} + if entries == nil then + return + end + local iterable = entries + if iterable[Symbol.iterator] then + local iterator = iterable[Symbol.iterator](iterable) + while true do + local result = iterator:next() + if result.done then + break + end + local value = result.value + self:set(value[1], value[2]) + end + else + local array = entries + for ____, kvp in ipairs(array) do + self:set(kvp[1], kvp[2]) + end + end + end + function Map.prototype.clear(self) + self.items = {} + self.nextKey = {} + self.previousKey = {} + self.firstKey = nil + self.lastKey = nil + self.size = 0 + end + function Map.prototype.delete(self, key) + local contains = self:has(key) + if contains then + self.size = self.size - 1 + local next = self.nextKey[key] + local previous = self.previousKey[key] + if next ~= nil and previous ~= nil then + self.nextKey[previous] = next + self.previousKey[next] = previous + elseif next ~= nil then + self.firstKey = next + self.previousKey[next] = nil + elseif previous ~= nil then + self.lastKey = previous + self.nextKey[previous] = nil + else + self.firstKey = nil + self.lastKey = nil + end + self.nextKey[key] = nil + self.previousKey[key] = nil + end + self.items[key] = nil + return contains + end + function Map.prototype.forEach(self, callback) + for ____, key in __TS__Iterator(self:keys()) do + callback(nil, self.items[key], key, self) + end + end + function Map.prototype.get(self, key) + return self.items[key] + end + function Map.prototype.has(self, key) + return self.nextKey[key] ~= nil or self.lastKey == key + end + function Map.prototype.set(self, key, value) + local isNewValue = not self:has(key) + if isNewValue then + self.size = self.size + 1 + end + self.items[key] = value + if self.firstKey == nil then + self.firstKey = key + self.lastKey = key + elseif isNewValue then + self.nextKey[self.lastKey] = key + self.previousKey[key] = self.lastKey + self.lastKey = key + end + return self + end + Map.prototype[Symbol.iterator] = function(self) + return self:entries() + end + function Map.prototype.entries(self) + local items = self.items + local nextKey = self.nextKey + local key = self.firstKey + return { + [Symbol.iterator] = function(self) + return self + end, + next = function(self) + local result = {done = not key, value = {key, items[key]}} + key = nextKey[key] + return result + end + } + end + function Map.prototype.keys(self) + local nextKey = self.nextKey + local key = self.firstKey + return { + [Symbol.iterator] = function(self) + return self + end, + next = function(self) + local result = {done = not key, value = key} + key = nextKey[key] + return result + end + } + end + function Map.prototype.values(self) + local items = self.items + local nextKey = self.nextKey + local key = self.firstKey + return { + [Symbol.iterator] = function(self) + return self + end, + next = function(self) + local result = {done = not key, value = items[key]} + key = nextKey[key] + return result + end + } + end + Map[Symbol.species] = Map +end + +local __TS__Match = string.match + +local __TS__MathAtan2 = math.atan2 or math.atan + +local __TS__MathModf = math.modf + +local function __TS__MathSign(val) + if val > 0 then + return 1 + elseif val < 0 then + return -1 + end + return 0 +end + +local function __TS__Number(value) + local valueType = type(value) + if valueType == "number" then + return value + elseif valueType == "string" then + local numberValue = tonumber(value) + if numberValue then + return numberValue + end + if value == "Infinity" then + return math.huge + end + if value == "-Infinity" then + return -math.huge + end + local stringWithoutSpaces = string.gsub(value, "%s", "") + if stringWithoutSpaces == "" then + return 0 + end + return 0 / 0 + elseif valueType == "boolean" then + return value and 1 or 0 + else + return 0 / 0 + end +end + +local function __TS__NumberIsFinite(value) + return type(value) == "number" and value == value and value ~= math.huge and value ~= -math.huge +end + +local function __TS__NumberIsInteger(value) + return __TS__NumberIsFinite(value) and math.floor(value) == value +end + +local function __TS__NumberIsNaN(value) + return value ~= value +end + +local function __TS__StringSubstring(self, start, ____end) + if ____end ~= ____end then + ____end = 0 + end + if ____end ~= nil and start > ____end then + start, ____end = ____end, start + end + if start >= 0 then + start = start + 1 + else + start = 1 + end + if ____end ~= nil and ____end < 0 then + ____end = 0 + end + return string.sub(self, start, ____end) +end + +local __TS__ParseInt +do + local parseIntBasePattern = "0123456789aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTvVwWxXyYzZ" + function __TS__ParseInt(numberString, base) + if base == nil then + base = 10 + local hexMatch = __TS__Match(numberString, "^%s*-?0[xX]") + if hexMatch ~= nil then + base = 16 + numberString = (__TS__Match(hexMatch, "-")) and "-" .. __TS__StringSubstring(numberString, #hexMatch) or __TS__StringSubstring(numberString, #hexMatch) + end + end + if base < 2 or base > 36 then + return 0 / 0 + end + local allowedDigits = base <= 10 and __TS__StringSubstring(parseIntBasePattern, 0, base) or __TS__StringSubstring(parseIntBasePattern, 0, 10 + 2 * (base - 10)) + local pattern = ("^%s*(-?[" .. allowedDigits) .. "]*)" + local number = tonumber((__TS__Match(numberString, pattern)), base) + if number == nil then + return 0 / 0 + end + if number >= 0 then + return math.floor(number) + else + return math.ceil(number) + end + end +end + +local function __TS__ParseFloat(numberString) + local infinityMatch = __TS__Match(numberString, "^%s*(-?Infinity)") + if infinityMatch ~= nil then + return __TS__StringAccess(infinityMatch, 0) == "-" and -math.huge or math.huge + end + local number = tonumber((__TS__Match(numberString, "^%s*(-?%d+%.?%d*)"))) + return number or 0 / 0 +end + +local __TS__NumberToString +do + local radixChars = "0123456789abcdefghijklmnopqrstuvwxyz" + function __TS__NumberToString(self, radix) + if radix == nil or radix == 10 or self == math.huge or self == -math.huge or self ~= self then + return tostring(self) + end + radix = math.floor(radix) + if radix < 2 or radix > 36 then + error("toString() radix argument must be between 2 and 36", 0) + end + local integer, fraction = __TS__MathModf(math.abs(self)) + local result = "" + if radix == 8 then + result = string.format("%o", integer) + elseif radix == 16 then + result = string.format("%x", integer) + else + repeat + do + result = __TS__StringAccess(radixChars, integer % radix) .. result + integer = math.floor(integer / radix) + end + until not (integer ~= 0) + end + if fraction ~= 0 then + result = result .. "." + local delta = 1e-16 + repeat + do + fraction = fraction * radix + delta = delta * radix + local digit = math.floor(fraction) + result = result .. __TS__StringAccess(radixChars, digit) + fraction = fraction - digit + end + until not (fraction >= delta) + end + if self < 0 then + result = "-" .. result + end + return result + end +end + +local function __TS__NumberToFixed(self, fractionDigits) + if math.abs(self) >= 1e+21 or self ~= self then + return tostring(self) + end + local f = math.floor(fractionDigits or 0) + if f < 0 or f > 99 then + error("toFixed() digits argument must be between 0 and 99", 0) + end + return string.format( + ("%." .. tostring(f)) .. "f", + self + ) +end + +local function __TS__ObjectDefineProperty(target, key, desc) + local luaKey = type(key) == "number" and key + 1 or key + local value = rawget(target, luaKey) + local hasGetterOrSetter = desc.get ~= nil or desc.set ~= nil + local descriptor + if hasGetterOrSetter then + if value ~= nil then + error( + "Cannot redefine property: " .. tostring(key), + 0 + ) + end + descriptor = desc + else + local valueExists = value ~= nil + local ____desc_set_4 = desc.set + local ____desc_get_5 = desc.get + local ____temp_0 + if desc.configurable ~= nil then + ____temp_0 = desc.configurable + else + ____temp_0 = valueExists + end + local ____temp_1 + if desc.enumerable ~= nil then + ____temp_1 = desc.enumerable + else + ____temp_1 = valueExists + end + local ____temp_2 + if desc.writable ~= nil then + ____temp_2 = desc.writable + else + ____temp_2 = valueExists + end + local ____temp_3 + if desc.value ~= nil then + ____temp_3 = desc.value + else + ____temp_3 = value + end + descriptor = { + set = ____desc_set_4, + get = ____desc_get_5, + configurable = ____temp_0, + enumerable = ____temp_1, + writable = ____temp_2, + value = ____temp_3 + } + end + __TS__SetDescriptor(target, luaKey, descriptor) + return target +end + +local function __TS__ObjectEntries(obj) + local result = {} + local len = 0 + for key in pairs(obj) do + len = len + 1 + result[len] = {key, obj[key]} + end + return result +end + +local function __TS__ObjectFromEntries(entries) + local obj = {} + local iterable = entries + if iterable[Symbol.iterator] then + local iterator = iterable[Symbol.iterator](iterable) + while true do + local result = iterator:next() + if result.done then + break + end + local value = result.value + obj[value[1]] = value[2] + end + else + for ____, entry in ipairs(entries) do + obj[entry[1]] = entry[2] + end + end + return obj +end + +local function __TS__ObjectKeys(obj) + local result = {} + local len = 0 + for key in pairs(obj) do + len = len + 1 + result[len] = key + end + return result +end + +local function __TS__ObjectRest(target, usedProperties) + local result = {} + for property in pairs(target) do + if not usedProperties[property] then + result[property] = target[property] + end + end + return result +end + +local function __TS__ObjectValues(obj) + local result = {} + local len = 0 + for key in pairs(obj) do + len = len + 1 + result[len] = obj[key] + end + return result +end + +local function __TS__PromiseAll(iterable) + local results = {} + local toResolve = {} + local numToResolve = 0 + local i = 0 + for ____, item in __TS__Iterator(iterable) do + if __TS__InstanceOf(item, __TS__Promise) then + if item.state == 1 then + results[i + 1] = item.value + elseif item.state == 2 then + return __TS__Promise.reject(item.rejectionReason) + else + numToResolve = numToResolve + 1 + toResolve[i] = item + end + else + results[i + 1] = item + end + i = i + 1 + end + if numToResolve == 0 then + return __TS__Promise.resolve(results) + end + return __TS__New( + __TS__Promise, + function(____, resolve, reject) + for index, promise in pairs(toResolve) do + promise["then"]( + promise, + function(____, data) + results[index + 1] = data + numToResolve = numToResolve - 1 + if numToResolve == 0 then + resolve(nil, results) + end + end, + function(____, reason) + reject(nil, reason) + end + ) + end + end + ) +end + +local function __TS__PromiseAllSettled(iterable) + local results = {} + local toResolve = {} + local numToResolve = 0 + local i = 0 + for ____, item in __TS__Iterator(iterable) do + if __TS__InstanceOf(item, __TS__Promise) then + if item.state == 1 then + results[i + 1] = {status = "fulfilled", value = item.value} + elseif item.state == 2 then + results[i + 1] = {status = "rejected", reason = item.rejectionReason} + else + numToResolve = numToResolve + 1 + toResolve[i] = item + end + else + results[i + 1] = {status = "fulfilled", value = item} + end + i = i + 1 + end + if numToResolve == 0 then + return __TS__Promise.resolve(results) + end + return __TS__New( + __TS__Promise, + function(____, resolve) + for index, promise in pairs(toResolve) do + promise["then"]( + promise, + function(____, data) + results[index + 1] = {status = "fulfilled", value = data} + numToResolve = numToResolve - 1 + if numToResolve == 0 then + resolve(nil, results) + end + end, + function(____, reason) + results[index + 1] = {status = "rejected", reason = reason} + numToResolve = numToResolve - 1 + if numToResolve == 0 then + resolve(nil, results) + end + end + ) + end + end + ) +end + +local function __TS__PromiseAny(iterable) + local rejections = {} + local pending = {} + for ____, item in __TS__Iterator(iterable) do + if __TS__InstanceOf(item, __TS__Promise) then + if item.state == 1 then + return __TS__Promise.resolve(item.value) + elseif item.state == 2 then + rejections[#rejections + 1] = item.rejectionReason + else + pending[#pending + 1] = item + end + else + return __TS__Promise.resolve(item) + end + end + if #pending == 0 then + return __TS__Promise.reject("No promises to resolve with .any()") + end + local numResolved = 0 + return __TS__New( + __TS__Promise, + function(____, resolve, reject) + for ____, promise in ipairs(pending) do + promise["then"]( + promise, + function(____, data) + resolve(nil, data) + end, + function(____, reason) + rejections[#rejections + 1] = reason + numResolved = numResolved + 1 + if numResolved == #pending then + reject(nil, {name = "AggregateError", message = "All Promises rejected", errors = rejections}) + end + end + ) + end + end + ) +end + +local function __TS__PromiseRace(iterable) + local pending = {} + for ____, item in __TS__Iterator(iterable) do + if __TS__InstanceOf(item, __TS__Promise) then + if item.state == 1 then + return __TS__Promise.resolve(item.value) + elseif item.state == 2 then + return __TS__Promise.reject(item.rejectionReason) + else + pending[#pending + 1] = item + end + else + return __TS__Promise.resolve(item) + end + end + return __TS__New( + __TS__Promise, + function(____, resolve, reject) + for ____, promise in ipairs(pending) do + promise["then"]( + promise, + function(____, value) return resolve(nil, value) end, + function(____, reason) return reject(nil, reason) end + ) + end + end + ) +end + +local Set +do + Set = __TS__Class() + Set.name = "Set" + function Set.prototype.____constructor(self, values) + self[Symbol.toStringTag] = "Set" + self.size = 0 + self.nextKey = {} + self.previousKey = {} + if values == nil then + return + end + local iterable = values + if iterable[Symbol.iterator] then + local iterator = iterable[Symbol.iterator](iterable) + while true do + local result = iterator:next() + if result.done then + break + end + self:add(result.value) + end + else + local array = values + for ____, value in ipairs(array) do + self:add(value) + end + end + end + function Set.prototype.add(self, value) + local isNewValue = not self:has(value) + if isNewValue then + self.size = self.size + 1 + end + if self.firstKey == nil then + self.firstKey = value + self.lastKey = value + elseif isNewValue then + self.nextKey[self.lastKey] = value + self.previousKey[value] = self.lastKey + self.lastKey = value + end + return self + end + function Set.prototype.clear(self) + self.nextKey = {} + self.previousKey = {} + self.firstKey = nil + self.lastKey = nil + self.size = 0 + end + function Set.prototype.delete(self, value) + local contains = self:has(value) + if contains then + self.size = self.size - 1 + local next = self.nextKey[value] + local previous = self.previousKey[value] + if next ~= nil and previous ~= nil then + self.nextKey[previous] = next + self.previousKey[next] = previous + elseif next ~= nil then + self.firstKey = next + self.previousKey[next] = nil + elseif previous ~= nil then + self.lastKey = previous + self.nextKey[previous] = nil + else + self.firstKey = nil + self.lastKey = nil + end + self.nextKey[value] = nil + self.previousKey[value] = nil + end + return contains + end + function Set.prototype.forEach(self, callback) + for ____, key in __TS__Iterator(self:keys()) do + callback(nil, key, key, self) + end + end + function Set.prototype.has(self, value) + return self.nextKey[value] ~= nil or self.lastKey == value + end + Set.prototype[Symbol.iterator] = function(self) + return self:values() + end + function Set.prototype.entries(self) + local nextKey = self.nextKey + local key = self.firstKey + return { + [Symbol.iterator] = function(self) + return self + end, + next = function(self) + local result = {done = not key, value = {key, key}} + key = nextKey[key] + return result + end + } + end + function Set.prototype.keys(self) + local nextKey = self.nextKey + local key = self.firstKey + return { + [Symbol.iterator] = function(self) + return self + end, + next = function(self) + local result = {done = not key, value = key} + key = nextKey[key] + return result + end + } + end + function Set.prototype.values(self) + local nextKey = self.nextKey + local key = self.firstKey + return { + [Symbol.iterator] = function(self) + return self + end, + next = function(self) + local result = {done = not key, value = key} + key = nextKey[key] + return result + end + } + end + Set[Symbol.species] = Set +end + +local function __TS__SparseArrayNew(...) + local sparseArray = {...} + sparseArray.sparseLength = __TS__CountVarargs(...) + return sparseArray +end + +local function __TS__SparseArrayPush(sparseArray, ...) + local args = {...} + local argsLen = __TS__CountVarargs(...) + local listLen = sparseArray.sparseLength + for i = 1, argsLen do + sparseArray[listLen + i] = args[i] + end + sparseArray.sparseLength = listLen + argsLen +end + +local function __TS__SparseArraySpread(sparseArray) + local _unpack = unpack or table.unpack + return _unpack(sparseArray, 1, sparseArray.sparseLength) +end + +local WeakMap +do + WeakMap = __TS__Class() + WeakMap.name = "WeakMap" + function WeakMap.prototype.____constructor(self, entries) + self[Symbol.toStringTag] = "WeakMap" + self.items = {} + setmetatable(self.items, {__mode = "k"}) + if entries == nil then + return + end + local iterable = entries + if iterable[Symbol.iterator] then + local iterator = iterable[Symbol.iterator](iterable) + while true do + local result = iterator:next() + if result.done then + break + end + local value = result.value + self.items[value[1]] = value[2] + end + else + for ____, kvp in ipairs(entries) do + self.items[kvp[1]] = kvp[2] + end + end + end + function WeakMap.prototype.delete(self, key) + local contains = self:has(key) + self.items[key] = nil + return contains + end + function WeakMap.prototype.get(self, key) + return self.items[key] + end + function WeakMap.prototype.has(self, key) + return self.items[key] ~= nil + end + function WeakMap.prototype.set(self, key, value) + self.items[key] = value + return self + end + WeakMap[Symbol.species] = WeakMap +end + +local WeakSet +do + WeakSet = __TS__Class() + WeakSet.name = "WeakSet" + function WeakSet.prototype.____constructor(self, values) + self[Symbol.toStringTag] = "WeakSet" + self.items = {} + setmetatable(self.items, {__mode = "k"}) + if values == nil then + return + end + local iterable = values + if iterable[Symbol.iterator] then + local iterator = iterable[Symbol.iterator](iterable) + while true do + local result = iterator:next() + if result.done then + break + end + self.items[result.value] = true + end + else + for ____, value in ipairs(values) do + self.items[value] = true + end + end + end + function WeakSet.prototype.add(self, value) + self.items[value] = true + return self + end + function WeakSet.prototype.delete(self, value) + local contains = self:has(value) + self.items[value] = nil + return contains + end + function WeakSet.prototype.has(self, value) + return self.items[value] == true + end + WeakSet[Symbol.species] = WeakSet +end + +local function __TS__SourceMapTraceBack(fileName, sourceMap) + _G.__TS__sourcemap = _G.__TS__sourcemap or ({}) + _G.__TS__sourcemap[fileName] = sourceMap + if _G.__TS__originalTraceback == nil then + local originalTraceback = debug.traceback + _G.__TS__originalTraceback = originalTraceback + debug.traceback = function(thread, message, level) + local trace + if thread == nil and message == nil and level == nil then + trace = originalTraceback() + elseif __TS__StringIncludes(_VERSION, "Lua 5.0") then + trace = originalTraceback((("[Level " .. tostring(level)) .. "] ") .. tostring(message)) + else + trace = originalTraceback(thread, message, level) + end + if type(trace) ~= "string" then + return trace + end + local function replacer(____, file, srcFile, line) + local fileSourceMap = _G.__TS__sourcemap[file] + if fileSourceMap ~= nil and fileSourceMap[line] ~= nil then + local data = fileSourceMap[line] + if type(data) == "number" then + return (srcFile .. ":") .. tostring(data) + end + return (data.file .. ":") .. tostring(data.line) + end + return (file .. ":") .. line + end + local result = string.gsub( + trace, + "(%S+)%.lua:(%d+)", + function(file, line) return replacer(nil, file .. ".lua", file .. ".ts", line) end + ) + local function stringReplacer(____, file, line) + local fileSourceMap = _G.__TS__sourcemap[file] + if fileSourceMap ~= nil and fileSourceMap[line] ~= nil then + local chunkName = (__TS__Match(file, "%[string \"([^\"]+)\"%]")) + local sourceName = string.gsub(chunkName, ".lua$", ".ts") + local data = fileSourceMap[line] + if type(data) == "number" then + return (sourceName .. ":") .. tostring(data) + end + return (data.file .. ":") .. tostring(data.line) + end + return (file .. ":") .. line + end + result = string.gsub( + result, + "(%[string \"[^\"]+\"%]):(%d+)", + function(file, line) return stringReplacer(nil, file, line) end + ) + return result + end + end +end + +local function __TS__Spread(iterable) + local arr = {} + if type(iterable) == "string" then + for i = 0, #iterable - 1 do + arr[i + 1] = __TS__StringAccess(iterable, i) + end + else + local len = 0 + for ____, item in __TS__Iterator(iterable) do + len = len + 1 + arr[len] = item + end + end + return __TS__Unpack(arr) +end + +local function __TS__StringCharAt(self, pos) + if pos ~= pos then + pos = 0 + end + if pos < 0 then + return "" + end + return string.sub(self, pos + 1, pos + 1) +end + +local function __TS__StringCharCodeAt(self, index) + if index ~= index then + index = 0 + end + if index < 0 then + return 0 / 0 + end + return string.byte(self, index + 1) or 0 / 0 +end + +local function __TS__StringEndsWith(self, searchString, endPosition) + if endPosition == nil or endPosition > #self then + endPosition = #self + end + return string.sub(self, endPosition - #searchString + 1, endPosition) == searchString +end + +local function __TS__StringPadEnd(self, maxLength, fillString) + if fillString == nil then + fillString = " " + end + if maxLength ~= maxLength then + maxLength = 0 + end + if maxLength == -math.huge or maxLength == math.huge then + error("Invalid string length", 0) + end + if #self >= maxLength or #fillString == 0 then + return self + end + maxLength = maxLength - #self + if maxLength > #fillString then + fillString = fillString .. string.rep( + fillString, + math.floor(maxLength / #fillString) + ) + end + return self .. string.sub( + fillString, + 1, + math.floor(maxLength) + ) +end + +local function __TS__StringPadStart(self, maxLength, fillString) + if fillString == nil then + fillString = " " + end + if maxLength ~= maxLength then + maxLength = 0 + end + if maxLength == -math.huge or maxLength == math.huge then + error("Invalid string length", 0) + end + if #self >= maxLength or #fillString == 0 then + return self + end + maxLength = maxLength - #self + if maxLength > #fillString then + fillString = fillString .. string.rep( + fillString, + math.floor(maxLength / #fillString) + ) + end + return string.sub( + fillString, + 1, + math.floor(maxLength) + ) .. self +end + +local __TS__StringReplace +do + local sub = string.sub + function __TS__StringReplace(source, searchValue, replaceValue) + local startPos, endPos = string.find(source, searchValue, nil, true) + if not startPos then + return source + end + local before = sub(source, 1, startPos - 1) + local replacement = type(replaceValue) == "string" and replaceValue or replaceValue(nil, searchValue, startPos - 1, source) + local after = sub(source, endPos + 1) + return (before .. replacement) .. after + end +end + +local __TS__StringSplit +do + local sub = string.sub + local find = string.find + function __TS__StringSplit(source, separator, limit) + if limit == nil then + limit = 4294967295 + end + if limit == 0 then + return {} + end + local result = {} + local resultIndex = 1 + if separator == nil or separator == "" then + for i = 1, #source do + result[resultIndex] = sub(source, i, i) + resultIndex = resultIndex + 1 + end + else + local currentPos = 1 + while resultIndex <= limit do + local startPos, endPos = find(source, separator, currentPos, true) + if not startPos then + break + end + result[resultIndex] = sub(source, currentPos, startPos - 1) + resultIndex = resultIndex + 1 + currentPos = endPos + 1 + end + if resultIndex <= limit then + result[resultIndex] = sub(source, currentPos) + end + end + return result + end +end + +local __TS__StringReplaceAll +do + local sub = string.sub + local find = string.find + function __TS__StringReplaceAll(source, searchValue, replaceValue) + if type(replaceValue) == "string" then + local concat = table.concat( + __TS__StringSplit(source, searchValue), + replaceValue + ) + if #searchValue == 0 then + return (replaceValue .. concat) .. replaceValue + end + return concat + end + local parts = {} + local partsIndex = 1 + if #searchValue == 0 then + parts[1] = replaceValue(nil, "", 0, source) + partsIndex = 2 + for i = 1, #source do + parts[partsIndex] = sub(source, i, i) + parts[partsIndex + 1] = replaceValue(nil, "", i, source) + partsIndex = partsIndex + 2 + end + else + local currentPos = 1 + while true do + local startPos, endPos = find(source, searchValue, currentPos, true) + if not startPos then + break + end + parts[partsIndex] = sub(source, currentPos, startPos - 1) + parts[partsIndex + 1] = replaceValue(nil, searchValue, startPos - 1, source) + partsIndex = partsIndex + 2 + currentPos = endPos + 1 + end + parts[partsIndex] = sub(source, currentPos) + end + return table.concat(parts) + end +end + +local function __TS__StringSlice(self, start, ____end) + if start == nil or start ~= start then + start = 0 + end + if ____end ~= ____end then + ____end = 0 + end + if start >= 0 then + start = start + 1 + end + if ____end ~= nil and ____end < 0 then + ____end = ____end - 1 + end + return string.sub(self, start, ____end) +end + +local function __TS__StringStartsWith(self, searchString, position) + if position == nil or position < 0 then + position = 0 + end + return string.sub(self, position + 1, #searchString + position) == searchString +end + +local function __TS__StringSubstr(self, from, length) + if from ~= from then + from = 0 + end + if length ~= nil then + if length ~= length or length <= 0 then + return "" + end + length = length + from + end + if from >= 0 then + from = from + 1 + end + return string.sub(self, from, length) +end + +local function __TS__StringTrim(self) + local result = string.gsub(self, "^[%s ]*(.-)[%s ]*$", "%1") + return result +end + +local function __TS__StringTrimEnd(self) + local result = string.gsub(self, "[%s ]*$", "") + return result +end + +local function __TS__StringTrimStart(self) + local result = string.gsub(self, "^[%s ]*", "") + return result +end + +local __TS__SymbolRegistryFor, __TS__SymbolRegistryKeyFor +do + local symbolRegistry = {} + function __TS__SymbolRegistryFor(key) + if not symbolRegistry[key] then + symbolRegistry[key] = __TS__Symbol(key) + end + return symbolRegistry[key] + end + function __TS__SymbolRegistryKeyFor(sym) + for key in pairs(symbolRegistry) do + if symbolRegistry[key] == sym then + return key + end + end + return nil + end +end + +local function __TS__TypeOf(value) + local luaType = type(value) + if luaType == "table" then + return "object" + elseif luaType == "nil" then + return "undefined" + else + return luaType + end +end + +local function __TS__Using(self, cb, ...) + local args = {...} + local thrownError + local ok, result = xpcall( + function() return cb( + nil, + __TS__Unpack(args) + ) end, + function(err) + thrownError = err + return thrownError + end + ) + local argArray = {__TS__Unpack(args)} + do + local i = #argArray - 1 + while i >= 0 do + local ____self_0 = argArray[i + 1] + ____self_0[Symbol.dispose](____self_0) + i = i - 1 + end + end + if not ok then + error(thrownError, 0) + end + return result +end + +local function __TS__UsingAsync(self, cb, ...) + local args = {...} + return __TS__AsyncAwaiter(function(____awaiter_resolve) + local thrownError + local ok, result = xpcall( + function() return cb( + nil, + __TS__Unpack(args) + ) end, + function(err) + thrownError = err + return thrownError + end + ) + local argArray = {__TS__Unpack(args)} + do + local i = #argArray - 1 + while i >= 0 do + if argArray[i + 1][Symbol.dispose] ~= nil then + local ____self_0 = argArray[i + 1] + ____self_0[Symbol.dispose](____self_0) + end + if argArray[i + 1][Symbol.asyncDispose] ~= nil then + local ____self_1 = argArray[i + 1] + __TS__Await(____self_1[Symbol.asyncDispose](____self_1)) + end + i = i - 1 + end + end + if not ok then + error(thrownError, 0) + end + return ____awaiter_resolve(nil, result) + end) +end + +return { + __TS__ArrayAt = __TS__ArrayAt, + __TS__ArrayConcat = __TS__ArrayConcat, + __TS__ArrayEntries = __TS__ArrayEntries, + __TS__ArrayEvery = __TS__ArrayEvery, + __TS__ArrayFill = __TS__ArrayFill, + __TS__ArrayFilter = __TS__ArrayFilter, + __TS__ArrayForEach = __TS__ArrayForEach, + __TS__ArrayFind = __TS__ArrayFind, + __TS__ArrayFindIndex = __TS__ArrayFindIndex, + __TS__ArrayFrom = __TS__ArrayFrom, + __TS__ArrayIncludes = __TS__ArrayIncludes, + __TS__ArrayIndexOf = __TS__ArrayIndexOf, + __TS__ArrayIsArray = __TS__ArrayIsArray, + __TS__ArrayJoin = __TS__ArrayJoin, + __TS__ArrayMap = __TS__ArrayMap, + __TS__ArrayPush = __TS__ArrayPush, + __TS__ArrayPushArray = __TS__ArrayPushArray, + __TS__ArrayReduce = __TS__ArrayReduce, + __TS__ArrayReduceRight = __TS__ArrayReduceRight, + __TS__ArrayReverse = __TS__ArrayReverse, + __TS__ArrayUnshift = __TS__ArrayUnshift, + __TS__ArraySort = __TS__ArraySort, + __TS__ArraySlice = __TS__ArraySlice, + __TS__ArraySome = __TS__ArraySome, + __TS__ArraySplice = __TS__ArraySplice, + __TS__ArrayToObject = __TS__ArrayToObject, + __TS__ArrayFlat = __TS__ArrayFlat, + __TS__ArrayFlatMap = __TS__ArrayFlatMap, + __TS__ArraySetLength = __TS__ArraySetLength, + __TS__ArrayToReversed = __TS__ArrayToReversed, + __TS__ArrayToSorted = __TS__ArrayToSorted, + __TS__ArrayToSpliced = __TS__ArrayToSpliced, + __TS__ArrayWith = __TS__ArrayWith, + __TS__AsyncAwaiter = __TS__AsyncAwaiter, + __TS__Await = __TS__Await, + __TS__Class = __TS__Class, + __TS__ClassExtends = __TS__ClassExtends, + __TS__CloneDescriptor = __TS__CloneDescriptor, + __TS__CountVarargs = __TS__CountVarargs, + __TS__Decorate = __TS__Decorate, + __TS__DecorateLegacy = __TS__DecorateLegacy, + __TS__DecorateParam = __TS__DecorateParam, + __TS__Delete = __TS__Delete, + __TS__DelegatedYield = __TS__DelegatedYield, + __TS__DescriptorGet = __TS__DescriptorGet, + __TS__DescriptorSet = __TS__DescriptorSet, + Error = Error, + RangeError = RangeError, + ReferenceError = ReferenceError, + SyntaxError = SyntaxError, + TypeError = TypeError, + URIError = URIError, + __TS__FunctionBind = __TS__FunctionBind, + __TS__Generator = __TS__Generator, + __TS__InstanceOf = __TS__InstanceOf, + __TS__InstanceOfObject = __TS__InstanceOfObject, + __TS__Iterator = __TS__Iterator, + __TS__LuaIteratorSpread = __TS__LuaIteratorSpread, + Map = Map, + __TS__Match = __TS__Match, + __TS__MathAtan2 = __TS__MathAtan2, + __TS__MathModf = __TS__MathModf, + __TS__MathSign = __TS__MathSign, + __TS__New = __TS__New, + __TS__Number = __TS__Number, + __TS__NumberIsFinite = __TS__NumberIsFinite, + __TS__NumberIsInteger = __TS__NumberIsInteger, + __TS__NumberIsNaN = __TS__NumberIsNaN, + __TS__ParseInt = __TS__ParseInt, + __TS__ParseFloat = __TS__ParseFloat, + __TS__NumberToString = __TS__NumberToString, + __TS__NumberToFixed = __TS__NumberToFixed, + __TS__ObjectAssign = __TS__ObjectAssign, + __TS__ObjectDefineProperty = __TS__ObjectDefineProperty, + __TS__ObjectEntries = __TS__ObjectEntries, + __TS__ObjectFromEntries = __TS__ObjectFromEntries, + __TS__ObjectGetOwnPropertyDescriptor = __TS__ObjectGetOwnPropertyDescriptor, + __TS__ObjectGetOwnPropertyDescriptors = __TS__ObjectGetOwnPropertyDescriptors, + __TS__ObjectKeys = __TS__ObjectKeys, + __TS__ObjectRest = __TS__ObjectRest, + __TS__ObjectValues = __TS__ObjectValues, + __TS__ParseFloat = __TS__ParseFloat, + __TS__ParseInt = __TS__ParseInt, + __TS__Promise = __TS__Promise, + __TS__PromiseAll = __TS__PromiseAll, + __TS__PromiseAllSettled = __TS__PromiseAllSettled, + __TS__PromiseAny = __TS__PromiseAny, + __TS__PromiseRace = __TS__PromiseRace, + Set = Set, + __TS__SetDescriptor = __TS__SetDescriptor, + __TS__SparseArrayNew = __TS__SparseArrayNew, + __TS__SparseArrayPush = __TS__SparseArrayPush, + __TS__SparseArraySpread = __TS__SparseArraySpread, + WeakMap = WeakMap, + WeakSet = WeakSet, + __TS__SourceMapTraceBack = __TS__SourceMapTraceBack, + __TS__Spread = __TS__Spread, + __TS__StringAccess = __TS__StringAccess, + __TS__StringCharAt = __TS__StringCharAt, + __TS__StringCharCodeAt = __TS__StringCharCodeAt, + __TS__StringEndsWith = __TS__StringEndsWith, + __TS__StringIncludes = __TS__StringIncludes, + __TS__StringPadEnd = __TS__StringPadEnd, + __TS__StringPadStart = __TS__StringPadStart, + __TS__StringReplace = __TS__StringReplace, + __TS__StringReplaceAll = __TS__StringReplaceAll, + __TS__StringSlice = __TS__StringSlice, + __TS__StringSplit = __TS__StringSplit, + __TS__StringStartsWith = __TS__StringStartsWith, + __TS__StringSubstr = __TS__StringSubstr, + __TS__StringSubstring = __TS__StringSubstring, + __TS__StringTrim = __TS__StringTrim, + __TS__StringTrimEnd = __TS__StringTrimEnd, + __TS__StringTrimStart = __TS__StringTrimStart, + __TS__Symbol = __TS__Symbol, + Symbol = Symbol, + __TS__SymbolRegistryFor = __TS__SymbolRegistryFor, + __TS__SymbolRegistryKeyFor = __TS__SymbolRegistryKeyFor, + __TS__TypeOf = __TS__TypeOf, + __TS__Unpack = __TS__Unpack, + __TS__Using = __TS__Using, + __TS__UsingAsync = __TS__UsingAsync +} diff --git a/plugin/tsconfig.json b/plugin/tsconfig.json new file mode 100644 index 0000000..9089ccf --- /dev/null +++ b/plugin/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "rootDir": ".", + "outDir": "." + }, + "include": [".", "../src/types"], + "exclude": [] +} diff --git a/scripts/fennel b/scripts/fennel deleted file mode 100755 index ac51c2b..0000000 --- a/scripts/fennel +++ /dev/null @@ -1,6000 +0,0 @@ -#!/usr/bin/env lua -package.preload["fennel.binary"] = package.preload["fennel.binary"] or function(...) - local fennel = require("fennel") - local _local_712_ = require("fennel.utils") - local warn = _local_712_["warn"] - local copy = _local_712_["copy"] - local function shellout(command) - local f = io.popen(command) - local stdout = f:read("*all") - return (f:close() and stdout) - end - local function execute(cmd) - local _713_ = os.execute(cmd) - if (_713_ == 0) then - return true - elseif (_713_ == true) then - return true - else - return nil - end - end - local function string__3ec_hex_literal(characters) - local hex = {} - for character in characters:gmatch(".") do - table.insert(hex, ("0x%02x"):format(string.byte(character))) - end - return table.concat(hex, ", ") - end - local c_shim = "#ifdef __cplusplus\nextern \"C\" {\n#endif\n#include \n#include \n#include \n#ifdef __cplusplus\n}\n#endif\n#include \n#include \n#include \n#include \n\n#if LUA_VERSION_NUM == 501\n #define LUA_OK 0\n#endif\n\n/* Copied from lua.c */\n\nstatic lua_State *globalL = NULL;\n\nstatic void lstop (lua_State *L, lua_Debug *ar) {\n (void)ar; /* unused arg. */\n lua_sethook(L, NULL, 0, 0); /* reset hook */\n luaL_error(L, \"interrupted!\");\n}\n\nstatic void laction (int i) {\n signal(i, SIG_DFL); /* if another SIGINT happens, terminate process */\n lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);\n}\n\nstatic void createargtable (lua_State *L, char **argv, int argc, int script) {\n int i, narg;\n if (script == argc) script = 0; /* no script name? */\n narg = argc - (script + 1); /* number of positive indices */\n lua_createtable(L, narg, script + 1);\n for (i = 0; i < argc; i++) {\n lua_pushstring(L, argv[i]);\n lua_rawseti(L, -2, i - script);\n }\n lua_setglobal(L, \"arg\");\n}\n\nstatic int msghandler (lua_State *L) {\n const char *msg = lua_tostring(L, 1);\n if (msg == NULL) { /* is error object not a string? */\n if (luaL_callmeta(L, 1, \"__tostring\") && /* does it have a metamethod */\n lua_type(L, -1) == LUA_TSTRING) /* that produces a string? */\n return 1; /* that is the message */\n else\n msg = lua_pushfstring(L, \"(error object is a %%s value)\",\n luaL_typename(L, 1));\n }\n /* Call debug.traceback() instead of luaL_traceback() for Lua 5.1 compat. */\n lua_getglobal(L, \"debug\");\n lua_getfield(L, -1, \"traceback\");\n /* debug */\n lua_remove(L, -2);\n lua_pushstring(L, msg);\n /* original msg */\n lua_remove(L, -3);\n lua_pushinteger(L, 2); /* skip this function and traceback */\n lua_call(L, 2, 1); /* call debug.traceback */\n return 1; /* return the traceback */\n}\n\nstatic int docall (lua_State *L, int narg, int nres) {\n int status;\n int base = lua_gettop(L) - narg; /* function index */\n lua_pushcfunction(L, msghandler); /* push message handler */\n lua_insert(L, base); /* put it under function and args */\n globalL = L; /* to be available to 'laction' */\n signal(SIGINT, laction); /* set C-signal handler */\n status = lua_pcall(L, narg, nres, base);\n signal(SIGINT, SIG_DFL); /* reset C-signal handler */\n lua_remove(L, base); /* remove message handler from the stack */\n return status;\n}\n\nint main(int argc, char *argv[]) {\n lua_State *L = luaL_newstate();\n luaL_openlibs(L);\n createargtable(L, argv, argc, 0);\n\n static const unsigned char lua_loader_program[] = {\n%s\n};\n if(luaL_loadbuffer(L, (const char*)lua_loader_program,\n sizeof(lua_loader_program), \"%s\") != LUA_OK) {\n fprintf(stderr, \"luaL_loadbuffer: %%s\\n\", lua_tostring(L, -1));\n lua_close(L);\n return 1;\n }\n\n /* lua_bundle */\n lua_newtable(L);\n static const unsigned char lua_require_1[] = {\n %s\n };\n lua_pushlstring(L, (const char*)lua_require_1, sizeof(lua_require_1));\n lua_setfield(L, -2, \"%s\");\n\n%s\n\n if (docall(L, 1, LUA_MULTRET)) {\n const char *errmsg = lua_tostring(L, 1);\n if (errmsg) {\n fprintf(stderr, \"%%s\\n\", errmsg);\n }\n lua_close(L);\n return 1;\n }\n lua_close(L);\n return 0;\n}" - local function compile_fennel(filename, options) - local f - if (filename == "-") then - f = io.stdin - else - f = assert(io.open(filename, "rb")) - end - local lua_code = fennel["compile-string"](f:read("*a"), options) - f:close() - return lua_code - end - local function native_loader(native, _3foptions) - local opts = (_3foptions or {["rename-modules"] = {}}) - local rename = (opts["rename-modules"] or {}) - local used_renames = {} - local nm = (os.getenv("NM") or "nm") - local out = {" /* native libraries */"} - for _, path in ipairs(native) do - local opens = {} - for open in shellout((nm .. " " .. path)):gmatch("[^dDt] _?luaopen_([%a%p%d]+)") do - table.insert(opens, open) - end - if (0 == #opens) then - warn((("Native module %s did not contain any luaopen_* symbols. " .. "Did you mean to use --native-library instead of --native-module?")):format(path)) - else - end - for _0, open in ipairs(opens) do - local require_name - do - local _717_ = rename[open] - if (nil ~= _717_) then - local renamed = _717_ - used_renames[open] = true - require_name = renamed - elseif true then - local _1 = _717_ - require_name = open - else - require_name = nil - end - end - table.insert(out, (" int luaopen_%s(lua_State *L);"):format(open)) - table.insert(out, (" lua_pushcfunction(L, luaopen_%s);"):format(open)) - table.insert(out, (" lua_setfield(L, -2, \"%s\");\n"):format(require_name)) - end - end - for key, val in pairs(rename) do - if not used_renames[key] then - warn((("unused --rename-native-module %s %s argument. " .. "Did you mean to include a native module?")):format(key, val)) - else - end - end - return table.concat(out, "\n") - end - local function fennel__3ec(filename, native, options) - local basename = filename:gsub("(.*[\\/])(.*)", "%2") - local basename_noextension = (basename:match("(.+)%.") or basename) - local dotpath = filename:gsub("^%.%/", ""):gsub("[\\/]", ".") - local dotpath_noextension = (dotpath:match("(.+)%.") or dotpath) - local fennel_loader - local _720_ - do - _720_ = "(do (local bundle_2_auto ...) (fn loader_3_auto [name_4_auto] (match (or (. bundle_2_auto name_4_auto) (. bundle_2_auto (.. name_4_auto \".init\"))) (mod_5_auto ? (= \"function\" (type mod_5_auto))) mod_5_auto (mod_5_auto ? (= \"string\" (type mod_5_auto))) (assert (if (= _VERSION \"Lua 5.1\") (loadstring mod_5_auto name_4_auto) (load mod_5_auto name_4_auto))) nil (values nil (: \"\n\\tmodule '%%s' not found in fennel bundle\" \"format\" name_4_auto)))) (table.insert (or package.loaders package.searchers) 2 loader_3_auto) ((assert (loader_3_auto \"%s\")) ((or unpack table.unpack) arg)))" - end - fennel_loader = _720_:format(dotpath_noextension) - local lua_loader = fennel["compile-string"](fennel_loader) - local _let_721_ = options - local rename_modules = _let_721_["rename-modules"] - return c_shim:format(string__3ec_hex_literal(lua_loader), basename_noextension, string__3ec_hex_literal(compile_fennel(filename, options)), dotpath_noextension, native_loader(native, {["rename-modules"] = rename_modules})) - end - local function write_c(filename, native, options) - local out_filename = (filename .. "_binary.c") - local f = assert(io.open(out_filename, "w+")) - f:write(fennel__3ec(filename, native, options)) - f:close() - return out_filename - end - local function compile_binary(lua_c_path, executable_name, static_lua, lua_include_dir, native) - local cc = (os.getenv("CC") or "cc") - local rdynamic, bin_extension, ldl_3f = nil, nil, nil - local _723_ - do - local _722_ = shellout((cc .. " -dumpmachine")) - if (nil ~= _722_) then - _723_ = _722_:match("mingw") - else - _723_ = _722_ - end - end - if _723_ then - rdynamic, bin_extension, ldl_3f = "", ".exe", false - else - rdynamic, bin_extension, ldl_3f = "-rdynamic", "", true - end - local compile_command - local _726_ - if ldl_3f then - _726_ = "-ldl" - else - _726_ = "" - end - compile_command = {cc, "-Os", lua_c_path, table.concat(native, " "), static_lua, rdynamic, "-lm", _726_, "-o", (executable_name .. bin_extension), "-I", lua_include_dir, os.getenv("CC_OPTS")} - if os.getenv("FENNEL_DEBUG") then - print("Compiling with", table.concat(compile_command, " ")) - else - end - if not execute(table.concat(compile_command, " ")) then - print("failed:", table.concat(compile_command, " ")) - os.exit(1) - else - end - if not os.getenv("FENNEL_DEBUG") then - os.remove(lua_c_path) - else - end - return os.exit(0) - end - local function native_path_3f(path) - local extension, version_extension = path:match("%.(%a+)(%.?%d*)$") - if (version_extension and not (version_extension == "") and not version_extension:match("%.%d+")) then - return false - else - local _731_ = extension - if (_731_ == "a") then - return path - elseif (_731_ == "o") then - return path - elseif (_731_ == "so") then - return path - elseif (_731_ == "dylib") then - return path - elseif true then - local _ = _731_ - return false - else - return nil - end - end - end - local function extract_native_args(args) - local native = {modules = {}, libraries = {}, ["rename-modules"] = {}} - for i = #args, 1, -1 do - if ("--native-module" == args[i]) then - local path = assert(native_path_3f(table.remove(args, (i + 1)))) - table.insert(native.modules, 1, path) - table.insert(native.libraries, 1, path) - table.remove(args, i) - else - end - if ("--native-library" == args[i]) then - table.insert(native.libraries, 1, assert(native_path_3f(table.remove(args, (i + 1))))) - table.remove(args, i) - else - end - if ("--rename-native-module" == args[i]) then - local original = table.remove(args, (i + 1)) - local new = table.remove(args, (i + 1)) - do end (native["rename-modules"])[original] = new - table.remove(args, i) - else - end - end - if (0 < #args) then - print(table.concat(args, " ")) - error(("Unknown args: " .. table.concat(args, " "))) - else - end - return native - end - local function compile(filename, executable_name, static_lua, lua_include_dir, options, args) - local _let_738_ = extract_native_args(args) - local modules = _let_738_["modules"] - local libraries = _let_738_["libraries"] - local rename_modules = _let_738_["rename-modules"] - local opts = {["rename-modules"] = rename_modules} - copy(options, opts) - return compile_binary(write_c(filename, modules, opts), executable_name, static_lua, lua_include_dir, libraries) - end - local help = ("\nUsage: %s --compile-binary FILE OUT STATIC_LUA_LIB LUA_INCLUDE_DIR\n\nCompile a binary from your Fennel program.\n\nRequires a C compiler, a copy of liblua, and Lua's dev headers. Implies\nthe --require-as-include option.\n\n FILE: the Fennel source being compiled.\n OUT: the name of the executable to generate\n STATIC_LUA_LIB: the path to the Lua library to use in the executable\n LUA_INCLUDE_DIR: the path to the directory of Lua C header files\n\nFor example, on a Debian system, to compile a file called program.fnl using\nLua 5.3, you would use this:\n\n $ %s --compile-binary program.fnl program \\\n /usr/lib/x86_64-linux-gnu/liblua5.3.a /usr/include/lua5.3\n\nThe program will be compiled to Lua, then compiled to C, then compiled to\nmachine code. You can set the CC environment variable to change the compiler\nused (default: cc) or set CC_OPTS to pass in compiler options. For example\nset CC_OPTS=-static to generate a binary with static linking.\n\nThis method is currently limited to programs do not transitively require Lua\nmodules. Requiring a Lua module directly will work, but requiring a Lua module\nwhich requires another will fail.\n\nTo include C libraries that contain Lua modules, add --native-module path/to.so,\nand to include C libraries without modules, use --native-library path/to.so.\nThese options are unstable, barely tested, and even more likely to break.\n\nIf you need to change the require name that a given native module is referenced\nas, you can use the --rename-native-module ORIGINAL NEW. ORIGINAL should be the\nsuffix of the luaopen_* symbol in the native module. NEW should be the string\nyou wish to pass to require to require the given native module. This can be used\nto handle cases where the name of an object file does not match the name of the\nluaopen_* symbol(s) within it. For example, the Lua readline bindings include a\nreadline.lua file which is usually required as \"readline\", and a C-readline.so\nfile which is required in the Lua half of the bindings like so:\n\n require 'C-readline'\n\nHowever, the symbol within the C-readline.so file is named luaopen_readline, so\nby default --compile-binary will make it so you can require it as \"readline\",\nwhich collides with the name of the readline.lua file and doesn't match the\nrequire call within readline.lua. In order to include the module within your\ncompiled binary and have it get picked up by readline.lua correctly, you can\nspecify the name used to refer to it in a require call by compiling it like\nso (this is assuming that program.fnl requires the Lua bindings):\n\n $ %s --compile-binary program.fnl program \\\n /usr/lib/x86_64-linux-gnu/liblua5.3.a /usr/include/lua5.3 \\\n --native-module C-readline.so \\\n --rename-native-module readline C-readline\n"):format(arg[0], arg[0], arg[0]) - return {compile = compile, help = help} -end -local fennel -package.preload["fennel.repl"] = package.preload["fennel.repl"] or function(...) - local utils = require("fennel.utils") - local parser = require("fennel.parser") - local compiler = require("fennel.compiler") - local specials = require("fennel.specials") - local view = require("fennel.view") - local unpack = (table.unpack or _G.unpack) - local function default_read_chunk(parser_state) - local function _565_() - if (0 < parser_state["stack-size"]) then - return ".." - else - return ">> " - end - end - io.write(_565_()) - io.flush() - local input = io.read() - return (input and (input .. "\n")) - end - local function default_on_values(xs) - io.write(table.concat(xs, "\9")) - return io.write("\n") - end - local function default_on_error(errtype, err, lua_source) - local function _567_() - local _566_ = errtype - if (_566_ == "Lua Compile") then - return ("Bad code generated - likely a bug with the compiler:\n" .. "--- Generated Lua Start ---\n" .. lua_source .. "--- Generated Lua End ---\n") - elseif (_566_ == "Runtime") then - return (compiler.traceback(tostring(err), 4) .. "\n") - elseif true then - local _ = _566_ - return ("%s error: %s\n"):format(errtype, tostring(err)) - else - return nil - end - end - return io.write(_567_()) - end - local save_source = table.concat({"local ___i___ = 1", "while true do", " local name, value = debug.getlocal(1, ___i___)", " if(name and name ~= \"___i___\") then", " ___replLocals___[name] = value", " ___i___ = ___i___ + 1", " else break end end"}, "\n") - local function splice_save_locals(env, lua_source) - local spliced_source = {} - local bind = "local %s = ___replLocals___['%s']" - for line in lua_source:gmatch("([^\n]+)\n?") do - table.insert(spliced_source, line) - end - for name in pairs(env.___replLocals___) do - table.insert(spliced_source, 1, bind:format(name, name)) - end - if ((1 < #spliced_source) and (spliced_source[#spliced_source]):match("^ *return .*$")) then - table.insert(spliced_source, #spliced_source, save_source) - else - end - return table.concat(spliced_source, "\n") - end - local function completer(env, scope, text) - local max_items = 2000 - local seen = {} - local matches = {} - local input_fragment = text:gsub(".*[%s)(]+", "") - local stop_looking_3f = false - local function add_partials(input, tbl, prefix) - local scope_first_3f = ((tbl == env) or (tbl == env.___replLocals___)) - local tbl_14_auto = matches - local i_15_auto = #tbl_14_auto - local function _570_() - if scope_first_3f then - return scope.manglings - else - return tbl - end - end - for k, is_mangled in utils.allpairs(_570_()) do - if (max_items <= #matches) then break end - local val_16_auto - do - local lookup_k - if scope_first_3f then - lookup_k = is_mangled - else - lookup_k = k - end - if ((type(k) == "string") and (input == k:sub(0, #input)) and not seen[k] and ((":" ~= prefix:sub(-1)) or ("function" == type(tbl[lookup_k])))) then - seen[k] = true - val_16_auto = (prefix .. k) - else - val_16_auto = nil - end - end - if (nil ~= val_16_auto) then - i_15_auto = (i_15_auto + 1) - do end (tbl_14_auto)[i_15_auto] = val_16_auto - else - end - end - return tbl_14_auto - end - local function descend(input, tbl, prefix, add_matches, method_3f) - local splitter - if method_3f then - splitter = "^([^:]+):(.*)" - else - splitter = "^([^.]+)%.(.*)" - end - local head, tail = input:match(splitter) - local raw_head = (scope.manglings[head] or head) - if (type(tbl[raw_head]) == "table") then - stop_looking_3f = true - if method_3f then - return add_partials(tail, tbl[raw_head], (prefix .. head .. ":")) - else - return add_matches(tail, tbl[raw_head], (prefix .. head)) - end - else - return nil - end - end - local function add_matches(input, tbl, prefix) - local prefix0 - if prefix then - prefix0 = (prefix .. ".") - else - prefix0 = "" - end - if (not input:find("%.") and input:find(":")) then - return descend(input, tbl, prefix0, add_matches, true) - elseif not input:find("%.") then - return add_partials(input, tbl, prefix0) - else - return descend(input, tbl, prefix0, add_matches, false) - end - end - for _, source in ipairs({scope.specials, scope.macros, (env.___replLocals___ or {}), env, env._G}) do - if stop_looking_3f then break end - add_matches(input_fragment, source) - end - return matches - end - local commands = {} - local function command_3f(input) - return input:match("^%s*,") - end - local function command_docs() - local _579_ - do - local tbl_14_auto = {} - local i_15_auto = #tbl_14_auto - for name, f in pairs(commands) do - local val_16_auto = (" ,%s - %s"):format(name, ((compiler.metadata):get(f, "fnl/docstring") or "undocumented")) - if (nil ~= val_16_auto) then - i_15_auto = (i_15_auto + 1) - do end (tbl_14_auto)[i_15_auto] = val_16_auto - else - end - end - _579_ = tbl_14_auto - end - return table.concat(_579_, "\n") - end - commands.help = function(_, _0, on_values) - return on_values({("Welcome to Fennel.\nThis is the REPL where you can enter code to be evaluated.\nYou can also run these repl commands:\n\n" .. command_docs() .. "\n ,exit - Leave the repl.\n\nUse ,doc something to see descriptions for individual macros and special forms.\n\nFor more information about the language, see https://fennel-lang.org/reference")}) - end - do end (compiler.metadata):set(commands.help, "fnl/docstring", "Show this message.") - local function reload(module_name, env, on_values, on_error) - local _581_, _582_ = pcall(specials["load-code"]("return require(...)", env), module_name) - if ((_581_ == true) and (nil ~= _582_)) then - local old = _582_ - local _ - package.loaded[module_name] = nil - _ = nil - local ok, new = pcall(require, module_name) - local new0 - if not ok then - on_values({new}) - new0 = old - else - new0 = new - end - specials["macro-loaded"][module_name] = nil - if ((type(old) == "table") and (type(new0) == "table")) then - for k, v in pairs(new0) do - old[k] = v - end - for k in pairs(old) do - if (nil == (new0)[k]) then - old[k] = nil - else - end - end - package.loaded[module_name] = old - else - end - return on_values({"ok"}) - elseif ((_581_ == false) and (nil ~= _582_)) then - local msg = _582_ - if (specials["macro-loaded"])[module_name] then - specials["macro-loaded"][module_name] = nil - return nil - else - local function _587_() - local _586_ = msg:gsub("\n.*", "") - return _586_ - end - return on_error("Runtime", _587_()) - end - else - return nil - end - end - local function run_command(read, on_error, f) - local _590_, _591_, _592_ = pcall(read) - if ((_590_ == true) and (_591_ == true) and (nil ~= _592_)) then - local val = _592_ - return f(val) - elseif (_590_ == false) then - return on_error("Parse", "Couldn't parse input.") - else - return nil - end - end - commands.reload = function(env, read, on_values, on_error) - local function _594_(_241) - return reload(tostring(_241), env, on_values, on_error) - end - return run_command(read, on_error, _594_) - end - do end (compiler.metadata):set(commands.reload, "fnl/docstring", "Reload the specified module.") - commands.reset = function(env, _, on_values) - env.___replLocals___ = {} - return on_values({"ok"}) - end - do end (compiler.metadata):set(commands.reset, "fnl/docstring", "Erase all repl-local scope.") - commands.complete = function(env, read, on_values, on_error, scope, chars) - local function _595_() - return on_values(completer(env, scope, string.char(unpack(chars)):gsub(",complete +", ""):sub(1, -2))) - end - return run_command(read, on_error, _595_) - end - do end (compiler.metadata):set(commands.complete, "fnl/docstring", "Print all possible completions for a given input symbol.") - local function apropos_2a(pattern, tbl, prefix, seen, names) - for name, subtbl in pairs(tbl) do - if (("string" == type(name)) and (package ~= subtbl)) then - local _596_ = type(subtbl) - if (_596_ == "function") then - if ((prefix .. name)):match(pattern) then - table.insert(names, (prefix .. name)) - else - end - elseif (_596_ == "table") then - if not seen[subtbl] then - local _599_ - do - local _598_ = seen - _598_[subtbl] = true - _599_ = _598_ - end - apropos_2a(pattern, subtbl, (prefix .. name:gsub("%.", "/") .. "."), _599_, names) - else - end - else - end - else - end - end - return names - end - local function apropos(pattern) - local names = apropos_2a(pattern, package.loaded, "", {}, {}) - local tbl_14_auto = {} - local i_15_auto = #tbl_14_auto - for _, name in ipairs(names) do - local val_16_auto = name:gsub("^_G%.", "") - if (nil ~= val_16_auto) then - i_15_auto = (i_15_auto + 1) - do end (tbl_14_auto)[i_15_auto] = val_16_auto - else - end - end - return tbl_14_auto - end - commands.apropos = function(_env, read, on_values, on_error, _scope) - local function _604_(_241) - return on_values(apropos(tostring(_241))) - end - return run_command(read, on_error, _604_) - end - do end (compiler.metadata):set(commands.apropos, "fnl/docstring", "Print all functions matching a pattern in all loaded modules.") - local function apropos_follow_path(path) - local paths - do - local tbl_14_auto = {} - local i_15_auto = #tbl_14_auto - for p in path:gmatch("[^%.]+") do - local val_16_auto = p - if (nil ~= val_16_auto) then - i_15_auto = (i_15_auto + 1) - do end (tbl_14_auto)[i_15_auto] = val_16_auto - else - end - end - paths = tbl_14_auto - end - local tgt = package.loaded - for _, path0 in ipairs(paths) do - if (nil == tgt) then break end - local _607_ - do - local _606_ = path0:gsub("%/", ".") - _607_ = _606_ - end - tgt = tgt[_607_] - end - return tgt - end - local function apropos_doc(pattern) - local names = {} - for _, path in ipairs(apropos(".*")) do - local tgt = apropos_follow_path(path) - if ("function" == type(tgt)) then - local _608_ = (compiler.metadata):get(tgt, "fnl/docstring") - if (nil ~= _608_) then - local docstr = _608_ - if docstr:match(pattern) then - table.insert(names, path) - else - end - else - end - else - end - end - return names - end - commands["apropos-doc"] = function(_env, read, on_values, on_error, _scope) - local function _612_(_241) - return on_values(apropos_doc(tostring(_241))) - end - return run_command(read, on_error, _612_) - end - do end (compiler.metadata):set(commands["apropos-doc"], "fnl/docstring", "Print all functions that match the pattern in their docs") - local function apropos_show_docs(on_values, pattern) - for _, path in ipairs(apropos(pattern)) do - local tgt = apropos_follow_path(path) - if (("function" == type(tgt)) and (compiler.metadata):get(tgt, "fnl/docstring")) then - on_values(specials.doc(tgt, path)) - on_values() - else - end - end - return nil - end - commands["apropos-show-docs"] = function(_env, read, on_values, on_error) - local function _614_(_241) - return apropos_show_docs(on_values, tostring(_241)) - end - return run_command(read, on_error, _614_) - end - do end (compiler.metadata):set(commands["apropos-show-docs"], "fnl/docstring", "Print all documentations matching a pattern in function name") - local function resolve(identifier, _615_, scope) - local _arg_616_ = _615_ - local ___replLocals___ = _arg_616_["___replLocals___"] - local env = _arg_616_ - local e - local function _617_(_241, _242) - return (___replLocals___[_242] or env[_242]) - end - e = setmetatable({}, {__index = _617_}) - local _618_, _619_ = pcall(compiler["compile-string"], tostring(identifier), {scope = scope}) - if ((_618_ == true) and (nil ~= _619_)) then - local code = _619_ - local _620_ = specials["load-code"](code, e)() - local function _621_() - local x = _620_ - return (type(x) == "function") - end - if ((nil ~= _620_) and _621_()) then - local x = _620_ - return x - else - return nil - end - else - return nil - end - end - commands.find = function(env, read, on_values, on_error, scope) - local function _624_(_241) - local _625_ - do - local _626_ = utils["sym?"](_241) - if (nil ~= _626_) then - local _627_ = resolve(_626_, env, scope) - if (nil ~= _627_) then - _625_ = debug.getinfo(_627_) - else - _625_ = _627_ - end - else - _625_ = _626_ - end - end - if ((_G.type(_625_) == "table") and ((_625_).what == "Lua") and (nil ~= (_625_).source) and (nil ~= (_625_).short_src) and (nil ~= (_625_).linedefined)) then - local source = (_625_).source - local src = (_625_).short_src - local line = (_625_).linedefined - local fnlsrc - do - local t_630_ = compiler.sourcemap - if (nil ~= t_630_) then - t_630_ = (t_630_)[source] - else - end - if (nil ~= t_630_) then - t_630_ = (t_630_)[line] - else - end - if (nil ~= t_630_) then - t_630_ = (t_630_)[2] - else - end - fnlsrc = t_630_ - end - return on_values({string.format("%s:%s", src, (fnlsrc or line))}) - elseif (_625_ == nil) then - return on_error("Repl", "Unknown value") - elseif true then - local _ = _625_ - return on_error("Repl", "No source info") - else - return nil - end - end - return run_command(read, on_error, _624_) - end - do end (compiler.metadata):set(commands.find, "fnl/docstring", "Print the filename and line number for a given function") - commands.doc = function(env, read, on_values, on_error, scope) - local function _635_(_241) - local name = tostring(_241) - local is_ok, target = nil, nil - local function _636_() - return (scope.specials[name] or scope.macros[name] or resolve(name, env, scope)) - end - is_ok, target = pcall(_636_) - if is_ok then - return on_values({specials.doc(target, name)}) - else - return on_error("Repl", "Could not resolve value for docstring lookup") - end - end - return run_command(read, on_error, _635_) - end - do end (compiler.metadata):set(commands.doc, "fnl/docstring", "Print the docstring and arglist for a function, macro, or special form.") - local function load_plugin_commands(plugins) - for _, plugin in ipairs((plugins or {})) do - for name, f in pairs(plugin) do - local _638_ = name:match("^repl%-command%-(.*)") - if (nil ~= _638_) then - local cmd_name = _638_ - commands[cmd_name] = (commands[cmd_name] or f) - else - end - end - end - return nil - end - local function run_command_loop(input, read, loop, env, on_values, on_error, scope, chars) - local command_name = input:match(",([^%s/]+)") - do - local _640_ = commands[command_name] - if (nil ~= _640_) then - local command = _640_ - command(env, read, on_values, on_error, scope, chars) - elseif true then - local _ = _640_ - if ("exit" ~= command_name) then - on_values({"Unknown command", command_name}) - else - end - else - end - end - if ("exit" ~= command_name) then - return loop() - else - return nil - end - end - local function try_readline_21(opts, ok, readline) - if ok then - if readline.set_readline_name then - readline.set_readline_name("fennel") - else - end - readline.set_options({keeplines = 1000, histfile = ""}) - opts.readChunk = function(parser_state) - local prompt - if (0 < parser_state["stack-size"]) then - prompt = ".. " - else - prompt = ">> " - end - local str = readline.readline(prompt) - if str then - return (str .. "\n") - else - return nil - end - end - local completer0 = nil - opts.registerCompleter = function(repl_completer) - completer0 = repl_completer - return nil - end - local function repl_completer(text, from, to) - if completer0 then - readline.set_completion_append_character("") - return completer0(text:sub(from, to)) - else - return {} - end - end - readline.set_complete_function(repl_completer) - return readline - else - return nil - end - end - local function should_use_readline_3f(opts) - return (("dumb" ~= os.getenv("TERM")) and not opts.readChunk and not opts.registerCompleter) - end - local function repl(_3foptions) - local old_root_options = utils.root.options - local opts = ((_3foptions and utils.copy(_3foptions)) or {}) - local readline = (should_use_readline_3f(opts) and try_readline_21(opts, pcall(require, "readline"))) - local env = specials["wrap-env"]((opts.env or rawget(_G, "_ENV") or _G)) - local save_locals_3f = ((opts.saveLocals ~= false) and env.debug and env.debug.getlocal) - local read_chunk = (opts.readChunk or default_read_chunk) - local on_values = (opts.onValues or default_on_values) - local on_error = (opts.onError or default_on_error) - local pp = (opts.pp or view) - local byte_stream, clear_stream = parser.granulate(read_chunk) - local chars = {} - local read, reset = nil, nil - local function _649_(parser_state) - local c = byte_stream(parser_state) - table.insert(chars, c) - return c - end - read, reset = parser.parser(_649_) - opts.env, opts.scope = env, compiler["make-scope"]() - opts.useMetadata = (opts.useMetadata ~= false) - if (opts.allowedGlobals == nil) then - opts.allowedGlobals = specials["current-global-names"](env) - else - end - if opts.registerCompleter then - local function _653_() - local _651_ = env - local _652_ = opts.scope - local function _654_(...) - return completer(_651_, _652_, ...) - end - return _654_ - end - opts.registerCompleter(_653_()) - else - end - load_plugin_commands(opts.plugins) - if save_locals_3f then - local function newindex(t, k, v) - if opts.scope.unmanglings[k] then - return rawset(t, k, v) - else - return nil - end - end - env.___replLocals___ = setmetatable({}, {__newindex = newindex}) - else - end - local function print_values(...) - local vals = {...} - local out = {} - env._, env.__ = vals[1], vals - for i = 1, select("#", ...) do - table.insert(out, pp(vals[i])) - end - return on_values(out) - end - local function loop() - for k in pairs(chars) do - chars[k] = nil - end - reset() - local ok, not_eof_3f, x = pcall(read) - local src_string = string.char(unpack(chars)) - if not ok then - on_error("Parse", __fnl_global__parse_2dok_3f) - clear_stream() - return loop() - elseif command_3f(src_string) then - return run_command_loop(src_string, read, loop, env, on_values, on_error, opts.scope, chars) - else - if not_eof_3f then - do - local _658_, _659_ = nil, nil - local function _661_() - local _660_ = opts - _660_["source"] = src_string - return _660_ - end - _658_, _659_ = pcall(compiler.compile, x, _661_()) - if ((_658_ == false) and (nil ~= _659_)) then - local msg = _659_ - clear_stream() - on_error("Compile", msg) - elseif ((_658_ == true) and (nil ~= _659_)) then - local src = _659_ - local src0 - if save_locals_3f then - src0 = splice_save_locals(env, src, opts.scope) - else - src0 = src - end - local _663_, _664_ = pcall(specials["load-code"], src0, env) - if ((_663_ == false) and (nil ~= _664_)) then - local msg = _664_ - clear_stream() - on_error("Lua Compile", msg, src0) - elseif (true and (nil ~= _664_)) then - local _ = _663_ - local chunk = _664_ - local function _665_() - return print_values(chunk()) - end - local function _666_() - local function _667_(...) - return on_error("Runtime", ...) - end - return _667_ - end - xpcall(_665_, _666_()) - else - end - else - end - end - utils.root.options = old_root_options - return loop() - else - return nil - end - end - end - loop() - if readline then - return readline.save_history() - else - return nil - end - end - return repl -end -package.preload["fennel.specials"] = package.preload["fennel.specials"] or function(...) - local utils = require("fennel.utils") - local view = require("fennel.view") - local parser = require("fennel.parser") - local compiler = require("fennel.compiler") - local unpack = (table.unpack or _G.unpack) - local SPECIALS = compiler.scopes.global.specials - local function wrap_env(env) - local function _364_(_, key) - if utils["string?"](key) then - return env[compiler["global-unmangling"](key)] - else - return env[key] - end - end - local function _366_(_, key, value) - if utils["string?"](key) then - env[compiler["global-unmangling"](key)] = value - return nil - else - env[key] = value - return nil - end - end - local function _368_() - local function putenv(k, v) - local _369_ - if utils["string?"](k) then - _369_ = compiler["global-unmangling"](k) - else - _369_ = k - end - return _369_, v - end - return next, utils.kvmap(env, putenv), nil - end - return setmetatable({}, {__index = _364_, __newindex = _366_, __pairs = _368_}) - end - local function current_global_names(_3fenv) - local mt - do - local _371_ = getmetatable(_3fenv) - if ((_G.type(_371_) == "table") and (nil ~= (_371_).__pairs)) then - local mtpairs = (_371_).__pairs - local tbl_11_auto = {} - for k, v in mtpairs(_3fenv) do - local _372_, _373_ = k, v - if ((nil ~= _372_) and (nil ~= _373_)) then - local k_12_auto = _372_ - local v_13_auto = _373_ - tbl_11_auto[k_12_auto] = v_13_auto - else - end - end - mt = tbl_11_auto - elseif (_371_ == nil) then - mt = (_3fenv or _G) - else - mt = nil - end - end - return (mt and utils.kvmap(mt, compiler["global-unmangling"])) - end - local function load_code(code, _3fenv, _3ffilename) - local env = (_3fenv or rawget(_G, "_ENV") or _G) - if (rawget(_G, "setfenv") and rawget(_G, "loadstring")) then - local f = assert(_G.loadstring(code, _3ffilename)) - local _376_ = f - setfenv(_376_, env) - return _376_ - else - return assert(load(code, _3ffilename, "t", env)) - end - end - local function doc_2a(tgt, name) - if not tgt then - return (name .. " not found") - else - local docstring = (((compiler.metadata):get(tgt, "fnl/docstring") or "#")):gsub("\n$", ""):gsub("\n", "\n ") - local mt = getmetatable(tgt) - if ((type(tgt) == "function") or ((type(mt) == "table") and (type(mt.__call) == "function"))) then - local arglist = table.concat(((compiler.metadata):get(tgt, "fnl/arglist") or {"#"}), " ") - local _378_ - if (#arglist > 0) then - _378_ = " " - else - _378_ = "" - end - return string.format("(%s%s%s)\n %s", name, _378_, arglist, docstring) - else - return string.format("%s\n %s", name, docstring) - end - end - end - local function doc_special(name, arglist, docstring, body_form_3f) - compiler.metadata[SPECIALS[name]] = {["fnl/arglist"] = arglist, ["fnl/docstring"] = docstring, ["fnl/body-form?"] = body_form_3f} - return nil - end - local function compile_do(ast, scope, parent, _3fstart) - local start = (_3fstart or 2) - local len = #ast - local sub_scope = compiler["make-scope"](scope) - for i = start, len do - compiler.compile1(ast[i], sub_scope, parent, {nval = 0}) - end - return nil - end - SPECIALS["do"] = function(ast, scope, parent, opts, _3fstart, _3fchunk, _3fsub_scope, _3fpre_syms) - local start = (_3fstart or 2) - local sub_scope = (_3fsub_scope or compiler["make-scope"](scope)) - local chunk = (_3fchunk or {}) - local len = #ast - local retexprs = {returned = true} - local function compile_body(outer_target, outer_tail, outer_retexprs) - if (len < start) then - compiler.compile1(nil, sub_scope, chunk, {tail = outer_tail, target = outer_target}) - else - for i = start, len do - local subopts = {nval = (((i ~= len) and 0) or opts.nval), tail = (((i == len) and outer_tail) or nil), target = (((i == len) and outer_target) or nil)} - local _ = utils["propagate-options"](opts, subopts) - local subexprs = compiler.compile1(ast[i], sub_scope, chunk, subopts) - if (i ~= len) then - compiler["keep-side-effects"](subexprs, parent, nil, ast[i]) - else - end - end - end - compiler.emit(parent, chunk, ast) - compiler.emit(parent, "end", ast) - utils.hook("do", ast, sub_scope) - return (outer_retexprs or retexprs) - end - if (opts.target or (opts.nval == 0) or opts.tail) then - compiler.emit(parent, "do", ast) - return compile_body(opts.target, opts.tail) - elseif opts.nval then - local syms = {} - for i = 1, opts.nval do - local s = ((_3fpre_syms and (_3fpre_syms)[i]) or compiler.gensym(scope)) - do end (syms)[i] = s - retexprs[i] = utils.expr(s, "sym") - end - local outer_target = table.concat(syms, ", ") - compiler.emit(parent, string.format("local %s", outer_target), ast) - compiler.emit(parent, "do", ast) - return compile_body(outer_target, opts.tail) - else - local fname = compiler.gensym(scope) - local fargs - if scope.vararg then - fargs = "..." - else - fargs = "" - end - compiler.emit(parent, string.format("local function %s(%s)", fname, fargs), ast) - return compile_body(nil, true, utils.expr((fname .. "(" .. fargs .. ")"), "statement")) - end - end - doc_special("do", {"..."}, "Evaluate multiple forms; return last value.", true) - SPECIALS.values = function(ast, scope, parent) - local len = #ast - local exprs = {} - for i = 2, len do - local subexprs = compiler.compile1(ast[i], scope, parent, {nval = ((i ~= len) and 1)}) - table.insert(exprs, subexprs[1]) - if (i == len) then - for j = 2, #subexprs do - table.insert(exprs, subexprs[j]) - end - else - end - end - return exprs - end - doc_special("values", {"..."}, "Return multiple values from a function. Must be in tail position.") - local function deep_tostring(x, key_3f) - if utils["sequence?"](x) then - local _387_ - do - local tbl_14_auto = {} - local i_15_auto = #tbl_14_auto - for _, v in ipairs(x) do - local val_16_auto = deep_tostring(v) - if (nil ~= val_16_auto) then - i_15_auto = (i_15_auto + 1) - do end (tbl_14_auto)[i_15_auto] = val_16_auto - else - end - end - _387_ = tbl_14_auto - end - return ("[" .. table.concat(_387_, " ") .. "]") - elseif utils["table?"](x) then - local _389_ - do - local tbl_14_auto = {} - local i_15_auto = #tbl_14_auto - for k, v in pairs(x) do - local val_16_auto = (deep_tostring(k, true) .. " " .. deep_tostring(v)) - if (nil ~= val_16_auto) then - i_15_auto = (i_15_auto + 1) - do end (tbl_14_auto)[i_15_auto] = val_16_auto - else - end - end - _389_ = tbl_14_auto - end - return ("{" .. table.concat(_389_, " ") .. "}") - elseif (key_3f and utils["string?"](x) and x:find("^[-%w?\\^_!$%&*+./@:|<=>]+$")) then - return (":" .. x) - elseif utils["string?"](x) then - return string.format("%q", x):gsub("\\\"", "\\\\\""):gsub("\"", "\\\"") - else - return tostring(x) - end - end - local function set_fn_metadata(arg_list, docstring, parent, fn_name) - if utils.root.options.useMetadata then - local args - local function _392_(_241) - return ("\"%s\""):format(deep_tostring(_241)) - end - args = utils.map(arg_list, _392_) - local meta_fields = {"\"fnl/arglist\"", ("{" .. table.concat(args, ", ") .. "}")} - if docstring then - table.insert(meta_fields, "\"fnl/docstring\"") - table.insert(meta_fields, ("\"" .. docstring:gsub("%s+$", ""):gsub("\\", "\\\\"):gsub("\n", "\\n"):gsub("\"", "\\\"") .. "\"")) - else - end - local meta_str = ("require(\"%s\").metadata"):format((utils.root.options.moduleName or "fennel")) - return compiler.emit(parent, ("pcall(function() %s:setall(%s, %s) end)"):format(meta_str, fn_name, table.concat(meta_fields, ", "))) - else - return nil - end - end - local function get_fn_name(ast, scope, fn_name, multi) - if (fn_name and (fn_name[1] ~= "nil")) then - local _395_ - if not multi then - _395_ = compiler["declare-local"](fn_name, {}, scope, ast) - else - _395_ = (compiler["symbol-to-expression"](fn_name, scope))[1] - end - return _395_, not multi, 3 - else - return nil, true, 2 - end - end - local function compile_named_fn(ast, f_scope, f_chunk, parent, index, fn_name, local_3f, arg_name_list, f_metadata) - for i = (index + 1), #ast do - compiler.compile1(ast[i], f_scope, f_chunk, {nval = (((i ~= #ast) and 0) or nil), tail = (i == #ast)}) - end - local _398_ - if local_3f then - _398_ = "local function %s(%s)" - else - _398_ = "%s = function(%s)" - end - compiler.emit(parent, string.format(_398_, fn_name, table.concat(arg_name_list, ", ")), ast) - compiler.emit(parent, f_chunk, ast) - compiler.emit(parent, "end", ast) - set_fn_metadata(f_metadata["fnl/arglist"], f_metadata["fnl/docstring"], parent, fn_name) - utils.hook("fn", ast, f_scope) - return utils.expr(fn_name, "sym") - end - local function compile_anonymous_fn(ast, f_scope, f_chunk, parent, index, arg_name_list, f_metadata, scope) - local fn_name = compiler.gensym(scope) - return compile_named_fn(ast, f_scope, f_chunk, parent, index, fn_name, true, arg_name_list, f_metadata) - end - local function get_function_metadata(ast, arg_list, index) - local f_metadata = {["fnl/arglist"] = arg_list} - local index_2a = (index + 1) - local expr = ast[index_2a] - if (utils["string?"](expr) and (index_2a < #ast)) then - local _401_ - do - local _400_ = f_metadata - _400_["fnl/docstring"] = expr - _401_ = _400_ - end - return _401_, index_2a - elseif (utils["table?"](expr) and (index_2a < #ast)) then - local _402_ - do - local tbl_11_auto = f_metadata - for k, v in pairs(expr) do - local _403_, _404_ = k, v - if ((nil ~= _403_) and (nil ~= _404_)) then - local k_12_auto = _403_ - local v_13_auto = _404_ - tbl_11_auto[k_12_auto] = v_13_auto - else - end - end - _402_ = tbl_11_auto - end - return _402_, index_2a - else - return f_metadata, index - end - end - SPECIALS.fn = function(ast, scope, parent) - local f_scope - do - local _407_ = compiler["make-scope"](scope) - do end (_407_)["vararg"] = false - f_scope = _407_ - end - local f_chunk = {} - local fn_sym = utils["sym?"](ast[2]) - local multi = (fn_sym and utils["multi-sym?"](fn_sym[1])) - local fn_name, local_3f, index = get_fn_name(ast, scope, fn_sym, multi) - local arg_list = compiler.assert(utils["table?"](ast[index]), "expected parameters table", ast) - compiler.assert((not multi or not multi["multi-sym-method-call"]), ("unexpected multi symbol " .. tostring(fn_name)), fn_sym) - local function get_arg_name(arg) - if utils["varg?"](arg) then - compiler.assert((arg == arg_list[#arg_list]), "expected vararg as last parameter", ast) - f_scope.vararg = true - return "..." - elseif (utils["sym?"](arg) and (tostring(arg) ~= "nil") and not utils["multi-sym?"](tostring(arg))) then - return compiler["declare-local"](arg, {}, f_scope, ast) - elseif utils["table?"](arg) then - local raw = utils.sym(compiler.gensym(scope)) - local declared = compiler["declare-local"](raw, {}, f_scope, ast) - compiler.destructure(arg, raw, ast, f_scope, f_chunk, {declaration = true, nomulti = true, symtype = "arg"}) - return declared - else - return compiler.assert(false, ("expected symbol for function parameter: %s"):format(tostring(arg)), ast[index]) - end - end - local arg_name_list = utils.map(arg_list, get_arg_name) - local f_metadata, index0 = get_function_metadata(ast, arg_list, index) - if fn_name then - return compile_named_fn(ast, f_scope, f_chunk, parent, index0, fn_name, local_3f, arg_name_list, f_metadata) - else - return compile_anonymous_fn(ast, f_scope, f_chunk, parent, index0, arg_name_list, f_metadata, scope) - end - end - doc_special("fn", {"name?", "args", "docstring?", "..."}, "Function syntax. May optionally include a name and docstring or a metadata table.\nIf a name is provided, the function will be bound in the current scope.\nWhen called with the wrong number of args, excess args will be discarded\nand lacking args will be nil, use lambda for arity-checked functions.", true) - SPECIALS.lua = function(ast, _, parent) - compiler.assert(((#ast == 2) or (#ast == 3)), "expected 1 or 2 arguments", ast) - local _411_ - do - local _410_ = utils["sym?"](ast[2]) - if (nil ~= _410_) then - _411_ = tostring(_410_) - else - _411_ = _410_ - end - end - if ("nil" ~= _411_) then - table.insert(parent, {ast = ast, leaf = tostring(ast[2])}) - else - end - local _415_ - do - local _414_ = utils["sym?"](ast[3]) - if (nil ~= _414_) then - _415_ = tostring(_414_) - else - _415_ = _414_ - end - end - if ("nil" ~= _415_) then - return tostring(ast[3]) - else - return nil - end - end - local function dot(ast, scope, parent) - compiler.assert((1 < #ast), "expected table argument", ast) - local len = #ast - local _let_418_ = compiler.compile1(ast[2], scope, parent, {nval = 1}) - local lhs = _let_418_[1] - if (len == 2) then - return tostring(lhs) - else - local indices = {} - for i = 3, len do - local index = ast[i] - if (utils["string?"](index) and utils["valid-lua-identifier?"](index)) then - table.insert(indices, ("." .. index)) - else - local _let_419_ = compiler.compile1(index, scope, parent, {nval = 1}) - local index0 = _let_419_[1] - table.insert(indices, ("[" .. tostring(index0) .. "]")) - end - end - if (tostring(lhs):find("[{\"0-9]") or ("nil" == tostring(lhs))) then - return ("(" .. tostring(lhs) .. ")" .. table.concat(indices)) - else - return (tostring(lhs) .. table.concat(indices)) - end - end - end - SPECIALS["."] = dot - doc_special(".", {"tbl", "key1", "..."}, "Look up key1 in tbl table. If more args are provided, do a nested lookup.") - SPECIALS.global = function(ast, scope, parent) - compiler.assert((#ast == 3), "expected name and value", ast) - compiler.destructure(ast[2], ast[3], ast, scope, parent, {forceglobal = true, nomulti = true, symtype = "global"}) - return nil - end - doc_special("global", {"name", "val"}, "Set name as a global with val.") - SPECIALS.set = function(ast, scope, parent) - compiler.assert((#ast == 3), "expected name and value", ast) - compiler.destructure(ast[2], ast[3], ast, scope, parent, {noundef = true, symtype = "set"}) - return nil - end - doc_special("set", {"name", "val"}, "Set a local variable to a new value. Only works on locals using var.") - local function set_forcibly_21_2a(ast, scope, parent) - compiler.assert((#ast == 3), "expected name and value", ast) - compiler.destructure(ast[2], ast[3], ast, scope, parent, {forceset = true, symtype = "set"}) - return nil - end - SPECIALS["set-forcibly!"] = set_forcibly_21_2a - local function local_2a(ast, scope, parent) - compiler.assert((#ast == 3), "expected name and value", ast) - compiler.destructure(ast[2], ast[3], ast, scope, parent, {declaration = true, nomulti = true, symtype = "local"}) - return nil - end - SPECIALS["local"] = local_2a - doc_special("local", {"name", "val"}, "Introduce new top-level immutable local.") - SPECIALS.var = function(ast, scope, parent) - compiler.assert((#ast == 3), "expected name and value", ast) - compiler.destructure(ast[2], ast[3], ast, scope, parent, {declaration = true, isvar = true, nomulti = true, symtype = "var"}) - return nil - end - doc_special("var", {"name", "val"}, "Introduce new mutable local.") - local function kv_3f(t) - local _423_ - do - local tbl_14_auto = {} - local i_15_auto = #tbl_14_auto - for k in pairs(t) do - local val_16_auto - if not ("number" == type(k)) then - val_16_auto = k - else - val_16_auto = nil - end - if (nil ~= val_16_auto) then - i_15_auto = (i_15_auto + 1) - do end (tbl_14_auto)[i_15_auto] = val_16_auto - else - end - end - _423_ = tbl_14_auto - end - return (_423_)[1] - end - SPECIALS.let = function(ast, scope, parent, opts) - local bindings = ast[2] - local pre_syms = {} - compiler.assert((utils["table?"](bindings) and not kv_3f(bindings)), "expected binding sequence", bindings) - compiler.assert(((#bindings % 2) == 0), "expected even number of name/value bindings", ast[2]) - compiler.assert((#ast >= 3), "expected body expression", ast[1]) - for _ = 1, (opts.nval or 0) do - table.insert(pre_syms, compiler.gensym(scope)) - end - local sub_scope = compiler["make-scope"](scope) - local sub_chunk = {} - for i = 1, #bindings, 2 do - compiler.destructure(bindings[i], bindings[(i + 1)], ast, sub_scope, sub_chunk, {declaration = true, nomulti = true, symtype = "let"}) - end - return SPECIALS["do"](ast, scope, parent, opts, 3, sub_chunk, sub_scope, pre_syms) - end - doc_special("let", {"[name1 val1 ... nameN valN]", "..."}, "Introduces a new scope in which a given set of local bindings are used.", true) - local function get_prev_line(parent) - if ("table" == type(parent)) then - return get_prev_line((parent.leaf or parent[#parent])) - else - return (parent or "") - end - end - local function disambiguate_3f(rootstr, parent) - local function _428_() - local _427_ = get_prev_line(parent) - if (nil ~= _427_) then - local prev_line = _427_ - return prev_line:match("%)$") - else - return nil - end - end - return (rootstr:match("^{") or _428_()) - end - SPECIALS.tset = function(ast, scope, parent) - compiler.assert((#ast > 3), "expected table, key, and value arguments", ast) - local root = (compiler.compile1(ast[2], scope, parent, {nval = 1}))[1] - local keys = {} - for i = 3, (#ast - 1) do - local _let_430_ = compiler.compile1(ast[i], scope, parent, {nval = 1}) - local key = _let_430_[1] - table.insert(keys, tostring(key)) - end - local value = (compiler.compile1(ast[#ast], scope, parent, {nval = 1}))[1] - local rootstr = tostring(root) - local fmtstr - if disambiguate_3f(rootstr, parent) then - fmtstr = "do end (%s)[%s] = %s" - else - fmtstr = "%s[%s] = %s" - end - return compiler.emit(parent, fmtstr:format(rootstr, table.concat(keys, "]["), tostring(value)), ast) - end - doc_special("tset", {"tbl", "key1", "...", "keyN", "val"}, "Set the value of a table field. Can take additional keys to set\nnested values, but all parents must contain an existing table.") - local function calculate_target(scope, opts) - if not (opts.tail or opts.target or opts.nval) then - return "iife", true, nil - elseif (opts.nval and (opts.nval ~= 0) and not opts.target) then - local accum = {} - local target_exprs = {} - for i = 1, opts.nval do - local s = compiler.gensym(scope) - do end (accum)[i] = s - target_exprs[i] = utils.expr(s, "sym") - end - return "target", opts.tail, table.concat(accum, ", "), target_exprs - else - return "none", opts.tail, opts.target - end - end - local function if_2a(ast, scope, parent, opts) - compiler.assert((2 < #ast), "expected condition and body", ast) - local do_scope = compiler["make-scope"](scope) - local branches = {} - local wrapper, inner_tail, inner_target, target_exprs = calculate_target(scope, opts) - local body_opts = {nval = opts.nval, tail = inner_tail, target = inner_target} - local function compile_body(i) - local chunk = {} - local cscope = compiler["make-scope"](do_scope) - compiler["keep-side-effects"](compiler.compile1(ast[i], cscope, chunk, body_opts), chunk, nil, ast[i]) - return {chunk = chunk, scope = cscope} - end - if (1 == (#ast % 2)) then - table.insert(ast, utils.sym("nil")) - else - end - for i = 2, (#ast - 1), 2 do - local condchunk = {} - local res = compiler.compile1(ast[i], do_scope, condchunk, {nval = 1}) - local cond = res[1] - local branch = compile_body((i + 1)) - branch.cond = cond - branch.condchunk = condchunk - branch.nested = ((i ~= 2) and (next(condchunk, nil) == nil)) - table.insert(branches, branch) - end - local else_branch = compile_body(#ast) - local s = compiler.gensym(scope) - local buffer = {} - local last_buffer = buffer - for i = 1, #branches do - local branch = branches[i] - local fstr - if not branch.nested then - fstr = "if %s then" - else - fstr = "elseif %s then" - end - local cond = tostring(branch.cond) - local cond_line = fstr:format(cond) - if branch.nested then - compiler.emit(last_buffer, branch.condchunk, ast) - else - for _, v in ipairs(branch.condchunk) do - compiler.emit(last_buffer, v, ast) - end - end - compiler.emit(last_buffer, cond_line, ast) - compiler.emit(last_buffer, branch.chunk, ast) - if (i == #branches) then - compiler.emit(last_buffer, "else", ast) - compiler.emit(last_buffer, else_branch.chunk, ast) - compiler.emit(last_buffer, "end", ast) - elseif not (branches[(i + 1)]).nested then - local next_buffer = {} - compiler.emit(last_buffer, "else", ast) - compiler.emit(last_buffer, next_buffer, ast) - compiler.emit(last_buffer, "end", ast) - last_buffer = next_buffer - else - end - end - if (wrapper == "iife") then - local iifeargs = ((scope.vararg and "...") or "") - compiler.emit(parent, ("local function %s(%s)"):format(tostring(s), iifeargs), ast) - compiler.emit(parent, buffer, ast) - compiler.emit(parent, "end", ast) - return utils.expr(("%s(%s)"):format(tostring(s), iifeargs), "statement") - elseif (wrapper == "none") then - for i = 1, #buffer do - compiler.emit(parent, buffer[i], ast) - end - return {returned = true} - else - compiler.emit(parent, ("local %s"):format(inner_target), ast) - for i = 1, #buffer do - compiler.emit(parent, buffer[i], ast) - end - return target_exprs - end - end - SPECIALS["if"] = if_2a - doc_special("if", {"cond1", "body1", "...", "condN", "bodyN"}, "Conditional form.\nTakes any number of condition/body pairs and evaluates the first body where\nthe condition evaluates to truthy. Similar to cond in other lisps.") - local function remove_until_condition(bindings) - if ("until" == bindings[(#bindings - 1)]) then - table.remove(bindings, (#bindings - 1)) - return table.remove(bindings) - else - return nil - end - end - local function compile_until(condition, scope, chunk) - if condition then - local _let_439_ = compiler.compile1(condition, scope, chunk, {nval = 1}) - local condition_lua = _let_439_[1] - return compiler.emit(chunk, ("if %s then break end"):format(tostring(condition_lua)), utils.expr(condition, "expression")) - else - return nil - end - end - SPECIALS.each = function(ast, scope, parent) - compiler.assert((#ast >= 3), "expected body expression", ast[1]) - local binding = compiler.assert(utils["table?"](ast[2]), "expected binding table", ast) - local _ = compiler.assert((2 <= #binding), "expected binding and iterator", binding) - local until_condition = remove_until_condition(binding) - local iter = table.remove(binding, #binding) - local destructures = {} - local new_manglings = {} - local sub_scope = compiler["make-scope"](scope) - local function destructure_binding(v) - compiler.assert(not utils["string?"](v), ("unexpected iterator clause " .. tostring(v)), binding) - if utils["sym?"](v) then - return compiler["declare-local"](v, {}, sub_scope, ast, new_manglings) - else - local raw = utils.sym(compiler.gensym(sub_scope)) - do end (destructures)[raw] = v - return compiler["declare-local"](raw, {}, sub_scope, ast) - end - end - local bind_vars = utils.map(binding, destructure_binding) - local vals = compiler.compile1(iter, scope, parent) - local val_names = utils.map(vals, tostring) - local chunk = {} - compiler.emit(parent, ("for %s in %s do"):format(table.concat(bind_vars, ", "), table.concat(val_names, ", ")), ast) - for raw, args in utils.stablepairs(destructures) do - compiler.destructure(args, raw, ast, sub_scope, chunk, {declaration = true, nomulti = true, symtype = "each"}) - end - compiler["apply-manglings"](sub_scope, new_manglings, ast) - compile_until(until_condition, sub_scope, chunk) - compile_do(ast, sub_scope, chunk, 3) - compiler.emit(parent, chunk, ast) - return compiler.emit(parent, "end", ast) - end - doc_special("each", {"[key value (iterator)]", "..."}, "Runs the body once for each set of values provided by the given iterator.\nMost commonly used with ipairs for sequential tables or pairs for undefined\norder, but can be used with any iterator.", true) - local function while_2a(ast, scope, parent) - local len1 = #parent - local condition = (compiler.compile1(ast[2], scope, parent, {nval = 1}))[1] - local len2 = #parent - local sub_chunk = {} - if (len1 ~= len2) then - for i = (len1 + 1), len2 do - table.insert(sub_chunk, parent[i]) - do end (parent)[i] = nil - end - compiler.emit(parent, "while true do", ast) - compiler.emit(sub_chunk, ("if not %s then break end"):format(condition[1]), ast) - else - compiler.emit(parent, ("while " .. tostring(condition) .. " do"), ast) - end - compile_do(ast, compiler["make-scope"](scope), sub_chunk, 3) - compiler.emit(parent, sub_chunk, ast) - return compiler.emit(parent, "end", ast) - end - SPECIALS["while"] = while_2a - doc_special("while", {"condition", "..."}, "The classic while loop. Evaluates body until a condition is non-truthy.", true) - local function for_2a(ast, scope, parent) - local ranges = compiler.assert(utils["table?"](ast[2]), "expected binding table", ast) - local until_condition = remove_until_condition(ast[2]) - local binding_sym = table.remove(ast[2], 1) - local sub_scope = compiler["make-scope"](scope) - local range_args = {} - local chunk = {} - compiler.assert(utils["sym?"](binding_sym), ("unable to bind %s %s"):format(type(binding_sym), tostring(binding_sym)), ast[2]) - compiler.assert((#ast >= 3), "expected body expression", ast[1]) - compiler.assert((#ranges <= 3), "unexpected arguments", ranges[4]) - for i = 1, math.min(#ranges, 3) do - range_args[i] = tostring((compiler.compile1(ranges[i], scope, parent, {nval = 1}))[1]) - end - compiler.emit(parent, ("for %s = %s do"):format(compiler["declare-local"](binding_sym, {}, sub_scope, ast), table.concat(range_args, ", ")), ast) - compile_until(until_condition, sub_scope, chunk) - compile_do(ast, sub_scope, chunk, 3) - compiler.emit(parent, chunk, ast) - return compiler.emit(parent, "end", ast) - end - SPECIALS["for"] = for_2a - doc_special("for", {"[index start stop step?]", "..."}, "Numeric loop construct.\nEvaluates body once for each value between start and stop (inclusive).", true) - local function native_method_call(ast, _scope, _parent, target, args) - local _let_443_ = ast - local _ = _let_443_[1] - local _0 = _let_443_[2] - local method_string = _let_443_[3] - local call_string - if ((target.type == "literal") or (target.type == "varg") or (target.type == "expression")) then - call_string = "(%s):%s(%s)" - else - call_string = "%s:%s(%s)" - end - return utils.expr(string.format(call_string, tostring(target), method_string, table.concat(args, ", ")), "statement") - end - local function nonnative_method_call(ast, scope, parent, target, args) - local method_string = tostring((compiler.compile1(ast[3], scope, parent, {nval = 1}))[1]) - local args0 = {tostring(target), unpack(args)} - return utils.expr(string.format("%s[%s](%s)", tostring(target), method_string, table.concat(args0, ", ")), "statement") - end - local function double_eval_protected_method_call(ast, scope, parent, target, args) - local method_string = tostring((compiler.compile1(ast[3], scope, parent, {nval = 1}))[1]) - local call = "(function(tgt, m, ...) return tgt[m](tgt, ...) end)(%s, %s)" - table.insert(args, 1, method_string) - return utils.expr(string.format(call, tostring(target), table.concat(args, ", ")), "statement") - end - local function method_call(ast, scope, parent) - compiler.assert((2 < #ast), "expected at least 2 arguments", ast) - local _let_445_ = compiler.compile1(ast[2], scope, parent, {nval = 1}) - local target = _let_445_[1] - local args = {} - for i = 4, #ast do - local subexprs - local _446_ - if (i ~= #ast) then - _446_ = 1 - else - _446_ = nil - end - subexprs = compiler.compile1(ast[i], scope, parent, {nval = _446_}) - utils.map(subexprs, tostring, args) - end - if (utils["string?"](ast[3]) and utils["valid-lua-identifier?"](ast[3])) then - return native_method_call(ast, scope, parent, target, args) - elseif (target.type == "sym") then - return nonnative_method_call(ast, scope, parent, target, args) - else - return double_eval_protected_method_call(ast, scope, parent, target, args) - end - end - SPECIALS[":"] = method_call - doc_special(":", {"tbl", "method-name", "..."}, "Call the named method on tbl with the provided args.\nMethod name doesn't have to be known at compile-time; if it is, use\n(tbl:method-name ...) instead.") - SPECIALS.comment = function(ast, _, parent) - local els = {} - for i = 2, #ast do - table.insert(els, view(ast[i], {["one-line?"] = true})) - end - return compiler.emit(parent, ("--[[ " .. table.concat(els, " ") .. " ]]--"), ast) - end - doc_special("comment", {"..."}, "Comment which will be emitted in Lua output.", true) - local function hashfn_max_used(f_scope, i, max) - local max0 - if f_scope.symmeta[("$" .. i)].used then - max0 = i - else - max0 = max - end - if (i < 9) then - return hashfn_max_used(f_scope, (i + 1), max0) - else - return max0 - end - end - SPECIALS.hashfn = function(ast, scope, parent) - compiler.assert((#ast == 2), "expected one argument", ast) - local f_scope - do - local _451_ = compiler["make-scope"](scope) - do end (_451_)["vararg"] = false - _451_["hashfn"] = true - f_scope = _451_ - end - local f_chunk = {} - local name = compiler.gensym(scope) - local symbol = utils.sym(name) - local args = {} - compiler["declare-local"](symbol, {}, scope, ast) - for i = 1, 9 do - args[i] = compiler["declare-local"](utils.sym(("$" .. i)), {}, f_scope, ast) - end - local function walker(idx, node, parent_node) - if (utils["sym?"](node) and (tostring(node) == "$...")) then - parent_node[idx] = utils.varg() - f_scope.vararg = true - return nil - else - return (utils["list?"](node) or utils["table?"](node)) - end - end - utils["walk-tree"](ast[2], walker) - compiler.compile1(ast[2], f_scope, f_chunk, {tail = true}) - local max_used = hashfn_max_used(f_scope, 1, 0) - if f_scope.vararg then - compiler.assert((max_used == 0), "$ and $... in hashfn are mutually exclusive", ast) - else - end - local arg_str - if f_scope.vararg then - arg_str = tostring(utils.varg()) - else - arg_str = table.concat(args, ", ", 1, max_used) - end - compiler.emit(parent, string.format("local function %s(%s)", name, arg_str), ast) - compiler.emit(parent, f_chunk, ast) - compiler.emit(parent, "end", ast) - return utils.expr(name, "sym") - end - doc_special("hashfn", {"..."}, "Function literal shorthand; args are either $... OR $1, $2, etc.") - local function maybe_short_circuit_protect(ast, i, name, _455_) - local _arg_456_ = _455_ - local mac = _arg_456_["macros"] - local call = (utils["list?"](ast) and tostring(ast[1])) - if ((("or" == name) or ("and" == name)) and (1 < i) and (mac[call] or ("set" == call) or ("tset" == call) or ("global" == call))) then - return utils.list(utils.sym("do"), ast) - else - return ast - end - end - local function arithmetic_special(name, zero_arity, unary_prefix, ast, scope, parent) - local len = #ast - local operands = {} - local padded_op = (" " .. name .. " ") - for i = 2, len do - local subast = maybe_short_circuit_protect(ast[i], i, name, scope) - local subexprs = compiler.compile1(subast, scope, parent) - if (i == len) then - utils.map(subexprs, tostring, operands) - else - table.insert(operands, tostring(subexprs[1])) - end - end - local _459_ = #operands - if (_459_ == 0) then - local _461_ - do - local _460_ = zero_arity - compiler.assert(_460_, "Expected more than 0 arguments", ast) - _461_ = _460_ - end - return utils.expr(_461_, "literal") - elseif (_459_ == 1) then - if unary_prefix then - return ("(" .. unary_prefix .. padded_op .. operands[1] .. ")") - else - return operands[1] - end - elseif true then - local _ = _459_ - return ("(" .. table.concat(operands, padded_op) .. ")") - else - return nil - end - end - local function define_arithmetic_special(name, zero_arity, unary_prefix, _3flua_name) - local _467_ - do - local _464_ = (_3flua_name or name) - local _465_ = zero_arity - local _466_ = unary_prefix - local function _468_(...) - return arithmetic_special(_464_, _465_, _466_, ...) - end - _467_ = _468_ - end - SPECIALS[name] = _467_ - return doc_special(name, {"a", "b", "..."}, "Arithmetic operator; works the same as Lua but accepts more arguments.") - end - define_arithmetic_special("+", "0") - define_arithmetic_special("..", "''") - define_arithmetic_special("^") - define_arithmetic_special("-", nil, "") - define_arithmetic_special("*", "1") - define_arithmetic_special("%") - define_arithmetic_special("/", nil, "1") - define_arithmetic_special("//", nil, "1") - SPECIALS["or"] = function(ast, scope, parent) - return arithmetic_special("or", "false", nil, ast, scope, parent) - end - SPECIALS["and"] = function(ast, scope, parent) - return arithmetic_special("and", "true", nil, ast, scope, parent) - end - doc_special("and", {"a", "b", "..."}, "Boolean operator; works the same as Lua but accepts more arguments.") - doc_special("or", {"a", "b", "..."}, "Boolean operator; works the same as Lua but accepts more arguments.") - local function bitop_special(native_name, lib_name, zero_arity, unary_prefix, ast, scope, parent) - if (#ast == 1) then - return compiler.assert(zero_arity, "Expected more than 0 arguments.", ast) - else - local len = #ast - local operands = {} - local padded_native_name = (" " .. native_name .. " ") - local prefixed_lib_name = ("bit." .. lib_name) - for i = 2, len do - local subexprs - local _469_ - if (i ~= len) then - _469_ = 1 - else - _469_ = nil - end - subexprs = compiler.compile1(ast[i], scope, parent, {nval = _469_}) - utils.map(subexprs, tostring, operands) - end - if (#operands == 1) then - if utils.root.options.useBitLib then - return (prefixed_lib_name .. "(" .. unary_prefix .. ", " .. operands[1] .. ")") - else - return ("(" .. unary_prefix .. padded_native_name .. operands[1] .. ")") - end - else - if utils.root.options.useBitLib then - return (prefixed_lib_name .. "(" .. table.concat(operands, ", ") .. ")") - else - return ("(" .. table.concat(operands, padded_native_name) .. ")") - end - end - end - end - local function define_bitop_special(name, zero_arity, unary_prefix, native) - local _479_ - do - local _475_ = native - local _476_ = name - local _477_ = zero_arity - local _478_ = unary_prefix - local function _480_(...) - return bitop_special(_475_, _476_, _477_, _478_, ...) - end - _479_ = _480_ - end - SPECIALS[name] = _479_ - return nil - end - define_bitop_special("lshift", nil, "1", "<<") - define_bitop_special("rshift", nil, "1", ">>") - define_bitop_special("band", "0", "0", "&") - define_bitop_special("bor", "0", "0", "|") - define_bitop_special("bxor", "0", "0", "~") - doc_special("lshift", {"x", "n"}, "Bitwise logical left shift of x by n bits.\nOnly works in Lua 5.3+ or LuaJIT with the --use-bit-lib flag.") - doc_special("rshift", {"x", "n"}, "Bitwise logical right shift of x by n bits.\nOnly works in Lua 5.3+ or LuaJIT with the --use-bit-lib flag.") - doc_special("band", {"x1", "x2", "..."}, "Bitwise AND of any number of arguments.\nOnly works in Lua 5.3+ or LuaJIT with the --use-bit-lib flag.") - doc_special("bor", {"x1", "x2", "..."}, "Bitwise OR of any number of arguments.\nOnly works in Lua 5.3+ or LuaJIT with the --use-bit-lib flag.") - doc_special("bxor", {"x1", "x2", "..."}, "Bitwise XOR of any number of arguments.\nOnly works in Lua 5.3+ or LuaJIT with the --use-bit-lib flag.") - doc_special("..", {"a", "b", "..."}, "String concatenation operator; works the same as Lua but accepts more arguments.") - local function native_comparator(op, _481_, scope, parent) - local _arg_482_ = _481_ - local _ = _arg_482_[1] - local lhs_ast = _arg_482_[2] - local rhs_ast = _arg_482_[3] - local _let_483_ = compiler.compile1(lhs_ast, scope, parent, {nval = 1}) - local lhs = _let_483_[1] - local _let_484_ = compiler.compile1(rhs_ast, scope, parent, {nval = 1}) - local rhs = _let_484_[1] - return string.format("(%s %s %s)", tostring(lhs), op, tostring(rhs)) - end - local function double_eval_protected_comparator(op, chain_op, ast, scope, parent) - local arglist = {} - local comparisons = {} - local vals = {} - local chain = string.format(" %s ", (chain_op or "and")) - for i = 2, #ast do - table.insert(arglist, tostring(compiler.gensym(scope))) - table.insert(vals, tostring((compiler.compile1(ast[i], scope, parent, {nval = 1}))[1])) - end - for i = 1, (#arglist - 1) do - table.insert(comparisons, string.format("(%s %s %s)", arglist[i], op, arglist[(i + 1)])) - end - return string.format("(function(%s) return %s end)(%s)", table.concat(arglist, ","), table.concat(comparisons, chain), table.concat(vals, ",")) - end - local function define_comparator_special(name, _3flua_op, _3fchain_op) - do - local op = (_3flua_op or name) - local function opfn(ast, scope, parent) - compiler.assert((2 < #ast), "expected at least two arguments", ast) - if (3 == #ast) then - return native_comparator(op, ast, scope, parent) - else - return double_eval_protected_comparator(op, _3fchain_op, ast, scope, parent) - end - end - SPECIALS[name] = opfn - end - return doc_special(name, {"a", "b", "..."}, "Comparison operator; works the same as Lua but accepts more arguments.") - end - define_comparator_special(">") - define_comparator_special("<") - define_comparator_special(">=") - define_comparator_special("<=") - define_comparator_special("=", "==") - define_comparator_special("not=", "~=", "or") - local function define_unary_special(op, _3frealop) - local function opfn(ast, scope, parent) - compiler.assert((#ast == 2), "expected one argument", ast) - local tail = compiler.compile1(ast[2], scope, parent, {nval = 1}) - return ((_3frealop or op) .. tostring(tail[1])) - end - SPECIALS[op] = opfn - return nil - end - define_unary_special("not", "not ") - doc_special("not", {"x"}, "Logical operator; works the same as Lua.") - define_unary_special("bnot", "~") - doc_special("bnot", {"x"}, "Bitwise negation; only works in Lua 5.3+ or LuaJIT with the --use-bit-lib flag.") - define_unary_special("length", "#") - doc_special("length", {"x"}, "Returns the length of a table or string.") - do end (SPECIALS)["~="] = SPECIALS["not="] - SPECIALS["#"] = SPECIALS.length - SPECIALS.quote = function(ast, scope, parent) - compiler.assert((#ast == 2), "expected one argument", ast) - local runtime, this_scope = true, scope - while this_scope do - this_scope = this_scope.parent - if (this_scope == compiler.scopes.compiler) then - runtime = false - else - end - end - return compiler["do-quote"](ast[2], scope, parent, runtime) - end - doc_special("quote", {"x"}, "Quasiquote the following form. Only works in macro/compiler scope.") - local macro_loaded = {} - local function safe_getmetatable(tbl) - local mt = getmetatable(tbl) - assert((mt ~= getmetatable("")), "Illegal metatable access!") - return mt - end - local safe_require = nil - local function safe_compiler_env() - local _488_ - do - local _487_ = rawget(_G, "utf8") - if (nil ~= _487_) then - _488_ = utils.copy(_487_) - else - _488_ = _487_ - end - end - return {table = utils.copy(table), math = utils.copy(math), string = utils.copy(string), pairs = pairs, ipairs = ipairs, select = select, tostring = tostring, tonumber = tonumber, bit = rawget(_G, "bit"), pcall = pcall, xpcall = xpcall, next = next, print = print, type = type, assert = assert, error = error, setmetatable = setmetatable, getmetatable = safe_getmetatable, require = safe_require, rawlen = rawget(_G, "rawlen"), rawget = rawget, rawset = rawset, rawequal = rawequal, _VERSION = _VERSION, utf8 = _488_} - end - local function combined_mt_pairs(env) - local combined = {} - local _let_490_ = getmetatable(env) - local __index = _let_490_["__index"] - if ("table" == type(__index)) then - for k, v in pairs(__index) do - combined[k] = v - end - else - end - for k, v in next, env, nil do - combined[k] = v - end - return next, combined, nil - end - local function make_compiler_env(ast, scope, parent, _3fopts) - local provided - do - local _492_ = (_3fopts or utils.root.options) - if ((_G.type(_492_) == "table") and ((_492_)["compiler-env"] == "strict")) then - provided = safe_compiler_env() - elseif ((_G.type(_492_) == "table") and (nil ~= (_492_).compilerEnv)) then - local compilerEnv = (_492_).compilerEnv - provided = compilerEnv - elseif ((_G.type(_492_) == "table") and (nil ~= (_492_)["compiler-env"])) then - local compiler_env = (_492_)["compiler-env"] - provided = compiler_env - elseif true then - local _ = _492_ - provided = safe_compiler_env(false) - else - provided = nil - end - end - local env - local function _494_(base) - return utils.sym(compiler.gensym((compiler.scopes.macro or scope), base)) - end - local function _495_() - return compiler.scopes.macro - end - local function _496_(symbol) - compiler.assert(compiler.scopes.macro, "must call from macro", ast) - return compiler.scopes.macro.manglings[tostring(symbol)] - end - local function _497_(form) - compiler.assert(compiler.scopes.macro, "must call from macro", ast) - return compiler.macroexpand(form, compiler.scopes.macro) - end - env = {_AST = ast, _CHUNK = parent, _IS_COMPILER = true, _SCOPE = scope, _SPECIALS = compiler.scopes.global.specials, _VARARG = utils.varg(), ["macro-loaded"] = macro_loaded, unpack = unpack, ["assert-compile"] = compiler.assert, view = view, version = utils.version, metadata = compiler.metadata, list = utils.list, ["list?"] = utils["list?"], ["table?"] = utils["table?"], sequence = utils.sequence, ["sequence?"] = utils["sequence?"], sym = utils.sym, ["sym?"] = utils["sym?"], ["multi-sym?"] = utils["multi-sym?"], comment = utils.comment, ["comment?"] = utils["comment?"], ["varg?"] = utils["varg?"], gensym = _494_, ["get-scope"] = _495_, ["in-scope?"] = _496_, macroexpand = _497_} - env._G = env - return setmetatable(env, {__index = provided, __newindex = provided, __pairs = combined_mt_pairs}) - end - local function _499_(...) - local tbl_14_auto = {} - local i_15_auto = #tbl_14_auto - for c in string.gmatch((package.config or ""), "([^\n]+)") do - local val_16_auto = c - if (nil ~= val_16_auto) then - i_15_auto = (i_15_auto + 1) - do end (tbl_14_auto)[i_15_auto] = val_16_auto - else - end - end - return tbl_14_auto - end - local _local_498_ = _499_(...) - local dirsep = _local_498_[1] - local pathsep = _local_498_[2] - local pathmark = _local_498_[3] - local pkg_config = {dirsep = (dirsep or "/"), pathmark = (pathmark or ";"), pathsep = (pathsep or "?")} - local function escapepat(str) - return string.gsub(str, "[^%w]", "%%%1") - end - local function search_module(modulename, _3fpathstring) - local pathsepesc = escapepat(pkg_config.pathsep) - local pattern = ("([^%s]*)%s"):format(pathsepesc, pathsepesc) - local no_dot_module = modulename:gsub("%.", pkg_config.dirsep) - local fullpath = ((_3fpathstring or utils["fennel-module"].path) .. pkg_config.pathsep) - local function try_path(path) - local filename = path:gsub(escapepat(pkg_config.pathmark), no_dot_module) - local filename2 = path:gsub(escapepat(pkg_config.pathmark), modulename) - local _501_ = (io.open(filename) or io.open(filename2)) - if (nil ~= _501_) then - local file = _501_ - file:close() - return filename - elseif true then - local _ = _501_ - return nil, ("no file '" .. filename .. "'") - else - return nil - end - end - local function find_in_path(start, _3ftried_paths) - local _503_ = fullpath:match(pattern, start) - if (nil ~= _503_) then - local path = _503_ - local _504_, _505_ = try_path(path) - if (nil ~= _504_) then - local filename = _504_ - return filename - elseif ((_504_ == nil) and (nil ~= _505_)) then - local error = _505_ - local function _507_() - local _506_ = (_3ftried_paths or {}) - table.insert(_506_, error) - return _506_ - end - return find_in_path((start + #path + 1), _507_()) - else - return nil - end - elseif true then - local _ = _503_ - local function _509_() - local tried_paths = table.concat((_3ftried_paths or {}), "\n\9") - if (_VERSION < "Lua 5.4") then - return ("\n\9" .. tried_paths) - else - return tried_paths - end - end - return nil, _509_() - else - return nil - end - end - return find_in_path(1) - end - local function make_searcher(_3foptions) - local function _512_(module_name) - local opts = utils.copy(utils.root.options) - for k, v in pairs((_3foptions or {})) do - opts[k] = v - end - opts["module-name"] = module_name - local _513_, _514_ = search_module(module_name) - if (nil ~= _513_) then - local filename = _513_ - local _517_ - do - local _515_ = filename - local _516_ = opts - local function _518_(...) - return utils["fennel-module"].dofile(_515_, _516_, ...) - end - _517_ = _518_ - end - return _517_, filename - elseif ((_513_ == nil) and (nil ~= _514_)) then - local error = _514_ - return error - else - return nil - end - end - return _512_ - end - local function dofile_with_searcher(fennel_macro_searcher, filename, opts, ...) - local searchers = (package.loaders or package.searchers or {}) - local _ = table.insert(searchers, 1, fennel_macro_searcher) - local m = utils["fennel-module"].dofile(filename, opts, ...) - table.remove(searchers, 1) - return m - end - local function fennel_macro_searcher(module_name) - local opts - do - local _520_ = utils.copy(utils.root.options) - do end (_520_)["env"] = "_COMPILER" - _520_["requireAsInclude"] = false - _520_["allowedGlobals"] = nil - opts = _520_ - end - local _521_ = search_module(module_name, utils["fennel-module"]["macro-path"]) - if (nil ~= _521_) then - local filename = _521_ - local _522_ - if (opts["compiler-env"] == _G) then - local _523_ = fennel_macro_searcher - local _524_ = filename - local _525_ = opts - local function _527_(...) - return dofile_with_searcher(_523_, _524_, _525_, ...) - end - _522_ = _527_ - else - local _528_ = filename - local _529_ = opts - local function _531_(...) - return utils["fennel-module"].dofile(_528_, _529_, ...) - end - _522_ = _531_ - end - return _522_, filename - else - return nil - end - end - local function lua_macro_searcher(module_name) - local _534_ = search_module(module_name, package.path) - if (nil ~= _534_) then - local filename = _534_ - local code - do - local f = io.open(filename) - local function close_handlers_8_auto(ok_9_auto, ...) - f:close() - if ok_9_auto then - return ... - else - return error(..., 0) - end - end - local function _536_() - return assert(f:read("*a")) - end - code = close_handlers_8_auto(_G.xpcall(_536_, (package.loaded.fennel or debug).traceback)) - end - local chunk = load_code(code, make_compiler_env(), filename) - return chunk, filename - else - return nil - end - end - local macro_searchers = {fennel_macro_searcher, lua_macro_searcher} - local function search_macro_module(modname, n) - local _538_ = macro_searchers[n] - if (nil ~= _538_) then - local f = _538_ - local _539_, _540_ = f(modname) - if ((nil ~= _539_) and true) then - local loader = _539_ - local _3ffilename = _540_ - return loader, _3ffilename - elseif true then - local _ = _539_ - return search_macro_module(modname, (n + 1)) - else - return nil - end - else - return nil - end - end - local function metadata_only_fennel(modname) - if ((modname == "fennel.macros") or (package and package.loaded and ("table" == type(package.loaded[modname])) and (package.loaded[modname].metadata == compiler.metadata))) then - return {metadata = compiler.metadata} - else - return nil - end - end - local function _544_(modname) - local function _545_() - local loader, filename = search_macro_module(modname, 1) - compiler.assert(loader, (modname .. " module not found.")) - do end (macro_loaded)[modname] = loader(modname, filename) - return macro_loaded[modname] - end - return (macro_loaded[modname] or metadata_only_fennel(modname) or _545_()) - end - safe_require = _544_ - local function add_macros(macros_2a, ast, scope) - compiler.assert(utils["table?"](macros_2a), "expected macros to be table", ast) - for k, v in pairs(macros_2a) do - compiler.assert((type(v) == "function"), "expected each macro to be function", ast) - do end (scope.macros)[k] = v - end - return nil - end - local function resolve_module_name(_546_, _scope, _parent, opts) - local _arg_547_ = _546_ - local filename = _arg_547_["filename"] - local second = _arg_547_[2] - local filename0 = (filename or (utils["table?"](second) and second.filename)) - local module_name = utils.root.options["module-name"] - local modexpr = compiler.compile(second, opts) - local modname_chunk = load_code(modexpr) - return modname_chunk(module_name, filename0) - end - SPECIALS["require-macros"] = function(ast, scope, parent, _3freal_ast) - compiler.assert((#ast == 2), "Expected one module name argument", (_3freal_ast or ast)) - local modname = resolve_module_name(ast, scope, parent, {}) - compiler.assert(utils["string?"](modname), "module name must compile to string", (_3freal_ast or ast)) - if not macro_loaded[modname] then - local loader, filename = search_macro_module(modname, 1) - compiler.assert(loader, (modname .. " module not found."), ast) - do end (macro_loaded)[modname] = loader(modname, filename) - else - end - if ("import-macros" == tostring(ast[1])) then - return macro_loaded[modname] - else - return add_macros(macro_loaded[modname], ast, scope, parent) - end - end - doc_special("require-macros", {"macro-module-name"}, "Load given module and use its contents as macro definitions in current scope.\nMacro module should return a table of macro functions with string keys.\nConsider using import-macros instead as it is more flexible.") - local function emit_included_fennel(src, path, opts, sub_chunk) - local subscope = compiler["make-scope"](utils.root.scope.parent) - local forms = {} - if utils.root.options.requireAsInclude then - subscope.specials.require = compiler["require-include"] - else - end - for _, val in parser.parser(parser["string-stream"](src), path) do - table.insert(forms, val) - end - for i = 1, #forms do - local subopts - if (i == #forms) then - subopts = {tail = true} - else - subopts = {nval = 0} - end - utils["propagate-options"](opts, subopts) - compiler.compile1(forms[i], subscope, sub_chunk, subopts) - end - return nil - end - local function include_path(ast, opts, path, mod, fennel_3f) - utils.root.scope.includes[mod] = "fnl/loading" - local src - do - local f = assert(io.open(path)) - local function close_handlers_8_auto(ok_9_auto, ...) - f:close() - if ok_9_auto then - return ... - else - return error(..., 0) - end - end - local function _553_() - return assert(f:read("*all")):gsub("[\13\n]*$", "") - end - src = close_handlers_8_auto(_G.xpcall(_553_, (package.loaded.fennel or debug).traceback)) - end - local ret = utils.expr(("require(\"" .. mod .. "\")"), "statement") - local target = ("package.preload[%q]"):format(mod) - local preload_str = (target .. " = " .. target .. " or function(...)") - local temp_chunk, sub_chunk = {}, {} - compiler.emit(temp_chunk, preload_str, ast) - compiler.emit(temp_chunk, sub_chunk) - compiler.emit(temp_chunk, "end", ast) - for i, v in ipairs(temp_chunk) do - table.insert(utils.root.chunk, i, v) - end - if fennel_3f then - emit_included_fennel(src, path, opts, sub_chunk) - else - compiler.emit(sub_chunk, src, ast) - end - utils.root.scope.includes[mod] = ret - return ret - end - local function include_circular_fallback(mod, modexpr, fallback, ast) - if (utils.root.scope.includes[mod] == "fnl/loading") then - compiler.assert(fallback, "circular include detected", ast) - return fallback(modexpr) - else - return nil - end - end - SPECIALS.include = function(ast, scope, parent, opts) - compiler.assert((#ast == 2), "expected one argument", ast) - local modexpr - do - local _556_, _557_ = pcall(resolve_module_name, ast, scope, parent, opts) - if ((_556_ == true) and (nil ~= _557_)) then - local modname = _557_ - modexpr = utils.expr(string.format("%q", modname), "literal") - elseif true then - local _ = _556_ - modexpr = (compiler.compile1(ast[2], scope, parent, {nval = 1}))[1] - else - modexpr = nil - end - end - if ((modexpr.type ~= "literal") or ((modexpr[1]):byte() ~= 34)) then - if opts.fallback then - return opts.fallback(modexpr) - else - return compiler.assert(false, "module name must be string literal", ast) - end - else - local mod = load_code(("return " .. modexpr[1]))() - local oldmod = utils.root.options["module-name"] - local _ - utils.root.options["module-name"] = mod - _ = nil - local res - local function _561_() - local _560_ = search_module(mod) - if (nil ~= _560_) then - local fennel_path = _560_ - return include_path(ast, opts, fennel_path, mod, true) - elseif true then - local _0 = _560_ - local lua_path = search_module(mod, package.path) - if lua_path then - return include_path(ast, opts, lua_path, mod, false) - elseif opts.fallback then - return opts.fallback(modexpr) - else - return compiler.assert(false, ("module not found " .. mod), ast) - end - else - return nil - end - end - res = ((utils["member?"](mod, (utils.root.options.skipInclude or {})) and opts.fallback(modexpr, true)) or include_circular_fallback(mod, modexpr, opts.fallback, ast) or utils.root.scope.includes[mod] or _561_()) - utils.root.options["module-name"] = oldmod - return res - end - end - doc_special("include", {"module-name-literal"}, "Like require but load the target module during compilation and embed it in the\nLua output. The module must be a string literal and resolvable at compile time.") - local function eval_compiler_2a(ast, scope, parent) - local env = make_compiler_env(ast, scope, parent) - local opts = utils.copy(utils.root.options) - opts.scope = compiler["make-scope"](compiler.scopes.compiler) - opts.allowedGlobals = current_global_names(env) - return load_code(compiler.compile(ast, opts), wrap_env(env))(opts["module-name"], ast.filename) - end - SPECIALS.macros = function(ast, scope, parent) - compiler.assert((#ast == 2), "Expected one table argument", ast) - return add_macros(eval_compiler_2a(ast[2], scope, parent), ast, scope, parent) - end - doc_special("macros", {"{:macro-name-1 (fn [...] ...) ... :macro-name-N macro-body-N}"}, "Define all functions in the given table as macros local to the current scope.") - SPECIALS["eval-compiler"] = function(ast, scope, parent) - local old_first = ast[1] - ast[1] = utils.sym("do") - local val = eval_compiler_2a(ast, scope, parent) - do end (ast)[1] = old_first - return val - end - doc_special("eval-compiler", {"..."}, "Evaluate the body at compile-time. Use the macro system instead if possible.", true) - return {doc = doc_2a, ["current-global-names"] = current_global_names, ["load-code"] = load_code, ["macro-loaded"] = macro_loaded, ["macro-searchers"] = macro_searchers, ["make-compiler-env"] = make_compiler_env, ["search-module"] = search_module, ["make-searcher"] = make_searcher, ["wrap-env"] = wrap_env} -end -package.preload["fennel.compiler"] = package.preload["fennel.compiler"] or function(...) - local utils = require("fennel.utils") - local parser = require("fennel.parser") - local friend = require("fennel.friend") - local unpack = (table.unpack or _G.unpack) - local scopes = {} - local function make_scope(_3fparent) - local parent = (_3fparent or scopes.global) - local _219_ - if parent then - _219_ = ((parent.depth or 0) + 1) - else - _219_ = 0 - end - return {includes = setmetatable({}, {__index = (parent and parent.includes)}), macros = setmetatable({}, {__index = (parent and parent.macros)}), manglings = setmetatable({}, {__index = (parent and parent.manglings)}), specials = setmetatable({}, {__index = (parent and parent.specials)}), symmeta = setmetatable({}, {__index = (parent and parent.symmeta)}), unmanglings = setmetatable({}, {__index = (parent and parent.unmanglings)}), gensyms = setmetatable({}, {__index = (parent and parent.gensyms)}), autogensyms = setmetatable({}, {__index = (parent and parent.autogensyms)}), vararg = (parent and parent.vararg), depth = _219_, hashfn = (parent and parent.hashfn), refedglobals = {}, parent = parent} - end - local function assert_msg(ast, msg) - local ast_tbl - if ("table" == type(ast)) then - ast_tbl = ast - else - ast_tbl = {} - end - local m = getmetatable(ast) - local filename = ((m and m.filename) or ast_tbl.filename or "unknown") - local line = ((m and m.line) or ast_tbl.line or "?") - local target = tostring((utils["sym?"](ast_tbl[1]) or ast_tbl[1] or "()")) - return string.format("%s:%s: Compile error in '%s': %s", filename, line, target, msg) - end - local function assert_compile(condition, msg, ast) - if not condition then - local _let_222_ = (utils.root.options or {}) - local source = _let_222_["source"] - local unfriendly = _let_222_["unfriendly"] - if (nil == utils.hook("assert-compile", condition, msg, ast, utils.root.reset)) then - utils.root.reset() - if (unfriendly or not friend or not _G.io or not _G.io.read) then - error(assert_msg(ast, msg), 0) - else - friend["assert-compile"](condition, msg, ast, source) - end - else - end - else - end - return condition - end - scopes.global = make_scope() - scopes.global.vararg = true - scopes.compiler = make_scope(scopes.global) - scopes.macro = scopes.global - local serialize_subst = {["\7"] = "\\a", ["\8"] = "\\b", ["\9"] = "\\t", ["\n"] = "n", ["\11"] = "\\v", ["\12"] = "\\f"} - local function serialize_string(str) - local function _226_(_241) - return ("\\" .. _241:byte()) - end - return string.gsub(string.gsub(string.format("%q", str), ".", serialize_subst), "[\128-\255]", _226_) - end - local function global_mangling(str) - if utils["valid-lua-identifier?"](str) then - return str - else - local function _227_(_241) - return string.format("_%02x", _241:byte()) - end - return ("__fnl_global__" .. str:gsub("[^%w]", _227_)) - end - end - local function global_unmangling(identifier) - local _229_ = string.match(identifier, "^__fnl_global__(.*)$") - if (nil ~= _229_) then - local rest = _229_ - local _230_ - local function _231_(_241) - return string.char(tonumber(_241:sub(2), 16)) - end - _230_ = string.gsub(rest, "_[%da-f][%da-f]", _231_) - return _230_ - elseif true then - local _ = _229_ - return identifier - else - return nil - end - end - local allowed_globals = nil - local function global_allowed_3f(name) - return (not allowed_globals or utils["member?"](name, allowed_globals)) - end - local function unique_mangling(original, mangling, scope, append) - if (scope.unmanglings[mangling] and not scope.gensyms[mangling]) then - return unique_mangling(original, (original .. append), scope, (append + 1)) - else - return mangling - end - end - local function local_mangling(str, scope, ast, _3ftemp_manglings) - assert_compile(not utils["multi-sym?"](str), ("unexpected multi symbol " .. str), ast) - local raw - if ((utils["lua-keywords"])[str] or str:match("^%d")) then - raw = ("_" .. str) - else - raw = str - end - local mangling - local function _235_(_241) - return string.format("_%02x", _241:byte()) - end - mangling = string.gsub(string.gsub(raw, "-", "_"), "[^%w_]", _235_) - local unique = unique_mangling(mangling, mangling, scope, 0) - do end (scope.unmanglings)[unique] = str - do - local manglings = (_3ftemp_manglings or scope.manglings) - do end (manglings)[str] = unique - end - return unique - end - local function apply_manglings(scope, new_manglings, ast) - for raw, mangled in pairs(new_manglings) do - assert_compile(not scope.refedglobals[mangled], ("use of global " .. raw .. " is aliased by a local"), ast) - do end (scope.manglings)[raw] = mangled - end - return nil - end - local function combine_parts(parts, scope) - local ret = (scope.manglings[parts[1]] or global_mangling(parts[1])) - for i = 2, #parts do - if utils["valid-lua-identifier?"](parts[i]) then - if (parts["multi-sym-method-call"] and (i == #parts)) then - ret = (ret .. ":" .. parts[i]) - else - ret = (ret .. "." .. parts[i]) - end - else - ret = (ret .. "[" .. serialize_string(parts[i]) .. "]") - end - end - return ret - end - local function next_append() - utils.root.scope["gensym-append"] = ((utils.root.scope["gensym-append"] or 0) + 1) - return ("_" .. utils.root.scope["gensym-append"] .. "_") - end - local function gensym(scope, _3fbase, _3fsuffix) - local mangling = ((_3fbase or "") .. next_append() .. (_3fsuffix or "")) - while scope.unmanglings[mangling] do - mangling = ((_3fbase or "") .. next_append() .. (_3fsuffix or "")) - end - scope.unmanglings[mangling] = (_3fbase or true) - do end (scope.gensyms)[mangling] = true - return mangling - end - local function autogensym(base, scope) - local _238_ = utils["multi-sym?"](base) - if (nil ~= _238_) then - local parts = _238_ - parts[1] = autogensym(parts[1], scope) - return table.concat(parts, ((parts["multi-sym-method-call"] and ":") or ".")) - elseif true then - local _ = _238_ - local function _239_() - local mangling = gensym(scope, base:sub(1, ( - 2)), "auto") - do end (scope.autogensyms)[base] = mangling - return mangling - end - return (scope.autogensyms[base] or _239_()) - else - return nil - end - end - local function check_binding_valid(symbol, scope, ast) - local name = tostring(symbol) - assert_compile(not name:find("&"), "invalid character: &") - assert_compile(not name:find("^%."), "invalid character: .") - assert_compile(not (scope.specials[name] or scope.macros[name]), ("local %s was overshadowed by a special form or macro"):format(name), ast) - return assert_compile(not utils["quoted?"](symbol), string.format("macro tried to bind %s without gensym", name), symbol) - end - local function declare_local(symbol, meta, scope, ast, _3ftemp_manglings) - check_binding_valid(symbol, scope, ast) - local name = tostring(symbol) - assert_compile(not utils["multi-sym?"](name), ("unexpected multi symbol " .. name), ast) - do end (scope.symmeta)[name] = meta - return local_mangling(name, scope, ast, _3ftemp_manglings) - end - local function hashfn_arg_name(name, multi_sym_parts, scope) - if not scope.hashfn then - return nil - elseif (name == "$") then - return "$1" - elseif multi_sym_parts then - if (multi_sym_parts and (multi_sym_parts[1] == "$")) then - multi_sym_parts[1] = "$1" - else - end - return table.concat(multi_sym_parts, ".") - else - return nil - end - end - local function symbol_to_expression(symbol, scope, _3freference_3f) - utils.hook("symbol-to-expression", symbol, scope, _3freference_3f) - local name = symbol[1] - local multi_sym_parts = utils["multi-sym?"](name) - local name0 = (hashfn_arg_name(name, multi_sym_parts, scope) or name) - local parts = (multi_sym_parts or {name0}) - local etype = (((#parts > 1) and "expression") or "sym") - local local_3f = scope.manglings[parts[1]] - if (local_3f and scope.symmeta[parts[1]]) then - scope.symmeta[parts[1]]["used"] = true - else - end - assert_compile(not scope.macros[parts[1]], "tried to reference a macro at runtime", symbol) - assert_compile((not scope.specials[parts[1]] or ("require" == parts[1])), "tried to reference a special form at runtime", symbol) - assert_compile((not _3freference_3f or local_3f or ("_ENV" == parts[1]) or global_allowed_3f(parts[1])), ("unknown identifier in strict mode: " .. tostring(parts[1])), symbol) - if (allowed_globals and not local_3f and scope.parent) then - scope.parent.refedglobals[parts[1]] = true - else - end - return utils.expr(combine_parts(parts, scope), etype) - end - local function emit(chunk, out, _3fast) - if (type(out) == "table") then - return table.insert(chunk, out) - else - return table.insert(chunk, {ast = _3fast, leaf = out}) - end - end - local function peephole(chunk) - if chunk.leaf then - return chunk - elseif ((#chunk >= 3) and ((chunk[(#chunk - 2)]).leaf == "do") and not (chunk[(#chunk - 1)]).leaf and (chunk[#chunk].leaf == "end")) then - local kid = peephole(chunk[(#chunk - 1)]) - local new_chunk = {ast = chunk.ast} - for i = 1, (#chunk - 3) do - table.insert(new_chunk, peephole(chunk[i])) - end - for i = 1, #kid do - table.insert(new_chunk, kid[i]) - end - return new_chunk - else - return utils.map(chunk, peephole) - end - end - local function flatten_chunk_correlated(main_chunk, options) - local function flatten(chunk, out, last_line, file) - local last_line0 = last_line - if chunk.leaf then - out[last_line0] = ((out[last_line0] or "") .. " " .. chunk.leaf) - else - for _, subchunk in ipairs(chunk) do - if (subchunk.leaf or (#subchunk > 0)) then - local source = utils["ast-source"](subchunk.ast) - if (file == source.filename) then - last_line0 = math.max(last_line0, (source.line or 0)) - else - end - last_line0 = flatten(subchunk, out, last_line0, file) - else - end - end - end - return last_line0 - end - local out = {} - local last = flatten(main_chunk, out, 1, options.filename) - for i = 1, last do - if (out[i] == nil) then - out[i] = "" - else - end - end - return table.concat(out, "\n") - end - local function flatten_chunk(sm, chunk, tab, depth) - if chunk.leaf then - local code = chunk.leaf - local info = chunk.ast - if sm then - table.insert(sm, {(info and info.filename), (info and info.line)}) - else - end - return code - else - local tab0 - do - local _252_ = tab - if (_252_ == true) then - tab0 = " " - elseif (_252_ == false) then - tab0 = "" - elseif (_252_ == tab) then - tab0 = tab - elseif (_252_ == nil) then - tab0 = "" - else - tab0 = nil - end - end - local function parter(c) - if (c.leaf or (#c > 0)) then - local sub = flatten_chunk(sm, c, tab0, (depth + 1)) - if (depth > 0) then - return (tab0 .. sub:gsub("\n", ("\n" .. tab0))) - else - return sub - end - else - return nil - end - end - return table.concat(utils.map(chunk, parter), "\n") - end - end - local sourcemap = {} - local function make_short_src(source) - local source0 = source:gsub("\n", " ") - if (#source0 <= 49) then - return ("[fennel \"" .. source0 .. "\"]") - else - return ("[fennel \"" .. source0:sub(1, 46) .. "...\"]") - end - end - local function flatten(chunk, options) - local chunk0 = peephole(chunk) - if options.correlate then - return flatten_chunk_correlated(chunk0, options), {} - else - local sm = {} - local ret = flatten_chunk(sm, chunk0, options.indent, 0) - if sm then - sm.short_src = (options.filename or make_short_src((options.source or ret))) - if options.filename then - sm.key = ("@" .. options.filename) - else - sm.key = ret - end - sourcemap[sm.key] = sm - else - end - return ret, sm - end - end - local function make_metadata() - local function _261_(self, tgt, key) - if self[tgt] then - return self[tgt][key] - else - return nil - end - end - local function _263_(self, tgt, key, value) - self[tgt] = (self[tgt] or {}) - do end (self[tgt])[key] = value - return tgt - end - local function _264_(self, tgt, ...) - local kv_len = select("#", ...) - local kvs = {...} - if ((kv_len % 2) ~= 0) then - error("metadata:setall() expected even number of k/v pairs") - else - end - self[tgt] = (self[tgt] or {}) - for i = 1, kv_len, 2 do - self[tgt][kvs[i]] = kvs[(i + 1)] - end - return tgt - end - return setmetatable({}, {__index = {get = _261_, set = _263_, setall = _264_}, __mode = "k"}) - end - local function exprs1(exprs) - return table.concat(utils.map(exprs, tostring), ", ") - end - local function keep_side_effects(exprs, chunk, start, ast) - local start0 = (start or 1) - for j = start0, #exprs do - local se = exprs[j] - if ((se.type == "expression") and (se[1] ~= "nil")) then - emit(chunk, string.format("do local _ = %s end", tostring(se)), ast) - elseif (se.type == "statement") then - local code = tostring(se) - local disambiguated - if (code:byte() == 40) then - disambiguated = ("do end " .. code) - else - disambiguated = code - end - emit(chunk, disambiguated, ast) - else - end - end - return nil - end - local function handle_compile_opts(exprs, parent, opts, ast) - if opts.nval then - local n = opts.nval - local len = #exprs - if (n ~= len) then - if (len > n) then - keep_side_effects(exprs, parent, (n + 1), ast) - for i = (n + 1), len do - exprs[i] = nil - end - else - for i = (#exprs + 1), n do - exprs[i] = utils.expr("nil", "literal") - end - end - else - end - else - end - if opts.tail then - emit(parent, string.format("return %s", exprs1(exprs)), ast) - else - end - if opts.target then - local result = exprs1(exprs) - local function _272_() - if (result == "") then - return "nil" - else - return result - end - end - emit(parent, string.format("%s = %s", opts.target, _272_()), ast) - else - end - if (opts.tail or opts.target) then - return {returned = true} - else - local _274_ = exprs - _274_["returned"] = true - return _274_ - end - end - local function find_macro(ast, scope, multi_sym_parts) - local function find_in_table(t, i) - if (i <= #multi_sym_parts) then - return find_in_table((utils["table?"](t) and t[multi_sym_parts[i]]), (i + 1)) - else - return t - end - end - local macro_2a = (utils["sym?"](ast[1]) and scope.macros[tostring(ast[1])]) - if (not macro_2a and multi_sym_parts) then - local nested_macro = find_in_table(scope.macros, 1) - assert_compile((not scope.macros[multi_sym_parts[1]] or (type(nested_macro) == "function")), "macro not found in imported macro module", ast) - return nested_macro - else - return macro_2a - end - end - local function propagate_trace_info(_278_, _index, node) - local _arg_279_ = _278_ - local filename = _arg_279_["filename"] - local line = _arg_279_["line"] - local bytestart = _arg_279_["bytestart"] - local byteend = _arg_279_["byteend"] - if (("table" == type(node)) and (filename ~= node.filename)) then - local src = utils["ast-source"](node) - src.filename, src.line = filename, line - src.bytestart, src.byteend = bytestart, byteend - else - end - return ("table" == type(node)) - end - local function macroexpand_2a(ast, scope, _3fonce) - local _281_ - if utils["list?"](ast) then - _281_ = find_macro(ast, scope, utils["multi-sym?"](ast[1])) - else - _281_ = nil - end - if (_281_ == false) then - return ast - elseif (nil ~= _281_) then - local macro_2a = _281_ - local old_scope = scopes.macro - local _ - scopes.macro = scope - _ = nil - local ok, transformed = nil, nil - local function _283_() - return macro_2a(unpack(ast, 2)) - end - ok, transformed = xpcall(_283_, debug.traceback) - local function _285_() - local _284_ = ast - local function _286_(...) - return propagate_trace_info(_284_, ...) - end - return _286_ - end - utils["walk-tree"](transformed, _285_()) - scopes.macro = old_scope - assert_compile(ok, transformed, ast) - if (_3fonce or not transformed) then - return transformed - else - return macroexpand_2a(transformed, scope) - end - elseif true then - local _ = _281_ - return ast - else - return nil - end - end - local function compile_special(ast, scope, parent, opts, special) - local exprs = (special(ast, scope, parent, opts) or utils.expr("nil", "literal")) - local exprs0 - if ("table" ~= type(exprs)) then - exprs0 = utils.expr(exprs, "expression") - else - exprs0 = exprs - end - local exprs2 - if utils["expr?"](exprs0) then - exprs2 = {exprs0} - else - exprs2 = exprs0 - end - if not exprs2.returned then - return handle_compile_opts(exprs2, parent, opts, ast) - elseif (opts.tail or opts.target) then - return {returned = true} - else - return exprs2 - end - end - local function compile_function_call(ast, scope, parent, opts, compile1, len) - local fargs = {} - local fcallee = (compile1(ast[1], scope, parent, {nval = 1}))[1] - assert_compile((("string" == type(ast[1])) or (fcallee.type ~= "literal")), ("cannot call literal value " .. tostring(ast[1])), ast) - for i = 2, len do - local subexprs - local _292_ - if (i ~= len) then - _292_ = 1 - else - _292_ = nil - end - subexprs = compile1(ast[i], scope, parent, {nval = _292_}) - table.insert(fargs, (subexprs[1] or utils.expr("nil", "literal"))) - if (i == len) then - for j = 2, #subexprs do - table.insert(fargs, subexprs[j]) - end - else - keep_side_effects(subexprs, parent, 2, ast[i]) - end - end - local pat - if ("string" == type(ast[1])) then - pat = "(%s)(%s)" - else - pat = "%s(%s)" - end - local call = string.format(pat, tostring(fcallee), exprs1(fargs)) - return handle_compile_opts({utils.expr(call, "statement")}, parent, opts, ast) - end - local function compile_call(ast, scope, parent, opts, compile1) - utils.hook("call", ast, scope) - local len = #ast - local first = ast[1] - local multi_sym_parts = utils["multi-sym?"](first) - local special = (utils["sym?"](first) and scope.specials[tostring(first)]) - assert_compile((len > 0), "expected a function, macro, or special to call", ast) - if special then - return compile_special(ast, scope, parent, opts, special) - elseif (multi_sym_parts and multi_sym_parts["multi-sym-method-call"]) then - local table_with_method = table.concat({unpack(multi_sym_parts, 1, (#multi_sym_parts - 1))}, ".") - local method_to_call = multi_sym_parts[#multi_sym_parts] - local new_ast = utils.list(utils.sym(":", ast), utils.sym(table_with_method, ast), method_to_call, select(2, unpack(ast))) - return compile1(new_ast, scope, parent, opts) - else - return compile_function_call(ast, scope, parent, opts, compile1, len) - end - end - local function compile_varg(ast, scope, parent, opts) - local _297_ - if scope.hashfn then - _297_ = "use $... in hashfn" - else - _297_ = "unexpected vararg" - end - assert_compile(scope.vararg, _297_, ast) - return handle_compile_opts({utils.expr("...", "varg")}, parent, opts, ast) - end - local function compile_sym(ast, scope, parent, opts) - local multi_sym_parts = utils["multi-sym?"](ast) - assert_compile(not (multi_sym_parts and multi_sym_parts["multi-sym-method-call"]), "multisym method calls may only be in call position", ast) - local e - if (ast[1] == "nil") then - e = utils.expr("nil", "literal") - else - e = symbol_to_expression(ast, scope, true) - end - return handle_compile_opts({e}, parent, opts, ast) - end - local function serialize_number(n) - local _300_ = string.gsub(tostring(n), ",", ".") - return _300_ - end - local function compile_scalar(ast, _scope, parent, opts) - local serialize - do - local _301_ = type(ast) - if (_301_ == "nil") then - serialize = tostring - elseif (_301_ == "boolean") then - serialize = tostring - elseif (_301_ == "string") then - serialize = serialize_string - elseif (_301_ == "number") then - serialize = serialize_number - else - serialize = nil - end - end - return handle_compile_opts({utils.expr(serialize(ast), "literal")}, parent, opts) - end - local function compile_table(ast, scope, parent, opts, compile1) - local buffer = {} - local function write_other_values(k) - if ((type(k) ~= "number") or (math.floor(k) ~= k) or (k < 1) or (k > #ast)) then - if ((type(k) == "string") and utils["valid-lua-identifier?"](k)) then - return {k, k} - else - local _let_303_ = compile1(k, scope, parent, {nval = 1}) - local compiled = _let_303_[1] - local kstr = ("[" .. tostring(compiled) .. "]") - return {kstr, k} - end - else - return nil - end - end - do - local keys - do - local tbl_14_auto = {} - local i_15_auto = #tbl_14_auto - for k, v in utils.stablepairs(ast) do - local val_16_auto = write_other_values(k, v) - if (nil ~= val_16_auto) then - i_15_auto = (i_15_auto + 1) - do end (tbl_14_auto)[i_15_auto] = val_16_auto - else - end - end - keys = tbl_14_auto - end - local function _309_(_307_) - local _arg_308_ = _307_ - local k1 = _arg_308_[1] - local k2 = _arg_308_[2] - local _let_310_ = compile1(ast[k2], scope, parent, {nval = 1}) - local v = _let_310_[1] - return string.format("%s = %s", k1, tostring(v)) - end - utils.map(keys, _309_, buffer) - end - for i = 1, #ast do - local nval = ((i ~= #ast) and 1) - table.insert(buffer, exprs1(compile1(ast[i], scope, parent, {nval = nval}))) - end - return handle_compile_opts({utils.expr(("{" .. table.concat(buffer, ", ") .. "}"), "expression")}, parent, opts, ast) - end - local function compile1(ast, scope, parent, _3fopts) - local opts = (_3fopts or {}) - local ast0 = macroexpand_2a(ast, scope) - if utils["list?"](ast0) then - return compile_call(ast0, scope, parent, opts, compile1) - elseif utils["varg?"](ast0) then - return compile_varg(ast0, scope, parent, opts) - elseif utils["sym?"](ast0) then - return compile_sym(ast0, scope, parent, opts) - elseif (type(ast0) == "table") then - return compile_table(ast0, scope, parent, opts, compile1) - elseif ((type(ast0) == "nil") or (type(ast0) == "boolean") or (type(ast0) == "number") or (type(ast0) == "string")) then - return compile_scalar(ast0, scope, parent, opts) - else - return assert_compile(false, ("could not compile value of type " .. type(ast0)), ast0) - end - end - local function destructure(to, from, ast, scope, parent, opts) - local opts0 = (opts or {}) - local _let_312_ = opts0 - local isvar = _let_312_["isvar"] - local declaration = _let_312_["declaration"] - local forceglobal = _let_312_["forceglobal"] - local forceset = _let_312_["forceset"] - local symtype = _let_312_["symtype"] - local symtype0 = ("_" .. (symtype or "dst")) - local setter - if declaration then - setter = "local %s = %s" - else - setter = "%s = %s" - end - local new_manglings = {} - local function getname(symbol, up1) - local raw = symbol[1] - assert_compile(not (opts0.nomulti and utils["multi-sym?"](raw)), ("unexpected multi symbol " .. raw), up1) - if declaration then - return declare_local(symbol, nil, scope, symbol, new_manglings) - else - local parts = (utils["multi-sym?"](raw) or {raw}) - local meta = scope.symmeta[parts[1]] - assert_compile(not raw:find(":"), "cannot set method sym", symbol) - if ((#parts == 1) and not forceset) then - assert_compile(not (forceglobal and meta), string.format("global %s conflicts with local", tostring(symbol)), symbol) - assert_compile(not (meta and not meta.var), ("expected var " .. raw), symbol) - assert_compile((meta or not opts0.noundef), ("expected local " .. parts[1]), symbol) - else - end - if forceglobal then - assert_compile(not scope.symmeta[scope.unmanglings[raw]], ("global " .. raw .. " conflicts with local"), symbol) - do end (scope.manglings)[raw] = global_mangling(raw) - do end (scope.unmanglings)[global_mangling(raw)] = raw - if allowed_globals then - table.insert(allowed_globals, raw) - else - end - else - end - return symbol_to_expression(symbol, scope)[1] - end - end - local function compile_top_target(lvalues) - local inits - local function _318_(_241) - if scope.manglings[_241] then - return _241 - else - return "nil" - end - end - inits = utils.map(lvalues, _318_) - local init = table.concat(inits, ", ") - local lvalue = table.concat(lvalues, ", ") - local plen, plast = #parent, parent[#parent] - local ret = compile1(from, scope, parent, {target = lvalue}) - if declaration then - for pi = plen, #parent do - if (parent[pi] == plast) then - plen = pi - else - end - end - if ((#parent == (plen + 1)) and parent[#parent].leaf) then - parent[#parent]["leaf"] = ("local " .. parent[#parent].leaf) - elseif (init == "nil") then - table.insert(parent, (plen + 1), {ast = ast, leaf = ("local " .. lvalue)}) - else - table.insert(parent, (plen + 1), {ast = ast, leaf = ("local " .. lvalue .. " = " .. init)}) - end - else - end - return ret - end - local function destructure_sym(left, rightexprs, up1, top_3f) - local lname = getname(left, up1) - check_binding_valid(left, scope, left) - if top_3f then - compile_top_target({lname}) - else - emit(parent, setter:format(lname, exprs1(rightexprs)), left) - end - if declaration then - scope.symmeta[tostring(left)] = {var = isvar} - return nil - else - return nil - end - end - local function destructure_table(left, rightexprs, top_3f, destructure1) - local s = gensym(scope, symtype0) - local right - do - local _325_ - if top_3f then - _325_ = exprs1(compile1(from, scope, parent)) - else - _325_ = exprs1(rightexprs) - end - if (_325_ == "") then - right = "nil" - elseif (nil ~= _325_) then - local right0 = _325_ - right = right0 - else - right = nil - end - end - emit(parent, string.format("local %s = %s", s, right), left) - for k, v in utils.stablepairs(left) do - if not (("number" == type(k)) and tostring(left[(k - 1)]):find("^&")) then - if (utils["sym?"](v) and (tostring(v) == "&")) then - local unpack_str = "(function (t, k)\n local mt = getmetatable(t)\n if \"table\" == type(mt) and mt.__fennelrest then\n return mt.__fennelrest(t, k)\n else\n return {(table.unpack or unpack)(t, k)}\n end\n end)(%s, %s)" - local formatted = string.format(string.gsub(unpack_str, "\n%s*", " "), s, k) - local subexpr = utils.expr(formatted, "expression") - assert_compile((utils["sequence?"](left) and (nil == left[(k + 2)])), "expected rest argument before last parameter", left) - destructure1(left[(k + 1)], {subexpr}, left) - elseif (utils["sym?"](k) and (tostring(k) == "&as")) then - destructure_sym(v, {utils.expr(tostring(s))}, left) - elseif (utils["sequence?"](left) and (tostring(v) == "&as")) then - local _, next_sym, trailing = select(k, unpack(left)) - assert_compile((nil == trailing), "expected &as argument before last parameter", left) - destructure_sym(next_sym, {utils.expr(tostring(s))}, left) - else - local key - if (type(k) == "string") then - key = serialize_string(k) - else - key = k - end - local subexpr = utils.expr(string.format("%s[%s]", s, key), "expression") - destructure1(v, {subexpr}, left) - end - else - end - end - return nil - end - local function destructure_values(left, up1, top_3f, destructure1) - local left_names, tables = {}, {} - for i, name in ipairs(left) do - if utils["sym?"](name) then - table.insert(left_names, getname(name, up1)) - else - local symname = gensym(scope, symtype0) - table.insert(left_names, symname) - do end (tables)[i] = {name, utils.expr(symname, "sym")} - end - end - assert_compile(top_3f, "can't nest multi-value destructuring", left) - compile_top_target(left_names) - if declaration then - for _, sym in ipairs(left) do - if utils["sym?"](sym) then - scope.symmeta[tostring(sym)] = {var = isvar} - else - end - end - else - end - for _, pair in utils.stablepairs(tables) do - destructure1(pair[1], {pair[2]}, left) - end - return nil - end - local function destructure1(left, rightexprs, up1, top_3f) - if (utils["sym?"](left) and (left[1] ~= "nil")) then - destructure_sym(left, rightexprs, up1, top_3f) - elseif utils["table?"](left) then - destructure_table(left, rightexprs, top_3f, destructure1) - elseif utils["list?"](left) then - destructure_values(left, up1, top_3f, destructure1) - else - assert_compile(false, string.format("unable to bind %s %s", type(left), tostring(left)), (((type((up1)[2]) == "table") and (up1)[2]) or up1)) - end - if top_3f then - return {returned = true} - else - return nil - end - end - local ret = destructure1(to, nil, ast, true) - utils.hook("destructure", from, to, scope) - apply_manglings(scope, new_manglings, ast) - return ret - end - local function require_include(ast, scope, parent, opts) - opts.fallback = function(e, no_warn) - if (not no_warn and ("literal" == e.type)) then - utils.warn(("include module not found, falling back to require: %s"):format(tostring(e))) - else - end - return utils.expr(string.format("require(%s)", tostring(e)), "statement") - end - return scopes.global.specials.include(ast, scope, parent, opts) - end - local function compile_stream(strm, options) - local opts = utils.copy(options) - local old_globals = allowed_globals - local scope = (opts.scope or make_scope(scopes.global)) - local vals = {} - local chunk = {} - do end (function(tgt, m, ...) return tgt[m](tgt, ...) end)(utils.root, "set-reset") - allowed_globals = opts.allowedGlobals - if (opts.indent == nil) then - opts.indent = " " - else - end - if opts.requireAsInclude then - scope.specials.require = require_include - else - end - utils.root.chunk, utils.root.scope, utils.root.options = chunk, scope, opts - for _, val in parser.parser(strm, opts.filename, opts) do - table.insert(vals, val) - end - for i = 1, #vals do - local exprs = compile1(vals[i], scope, chunk, {nval = (((i < #vals) and 0) or nil), tail = (i == #vals)}) - keep_side_effects(exprs, chunk, nil, vals[i]) - if (i == #vals) then - utils.hook("chunk", vals[i], scope) - else - end - end - allowed_globals = old_globals - utils.root.reset() - return flatten(chunk, opts) - end - local function compile_string(str, opts) - return compile_stream(parser["string-stream"](str), (opts or {})) - end - local function compile(ast, opts) - local opts0 = utils.copy(opts) - local old_globals = allowed_globals - local chunk = {} - local scope = (opts0.scope or make_scope(scopes.global)) - do end (function(tgt, m, ...) return tgt[m](tgt, ...) end)(utils.root, "set-reset") - allowed_globals = opts0.allowedGlobals - if (opts0.indent == nil) then - opts0.indent = " " - else - end - if opts0.requireAsInclude then - scope.specials.require = require_include - else - end - utils.root.chunk, utils.root.scope, utils.root.options = chunk, scope, opts0 - local exprs = compile1(ast, scope, chunk, {tail = true}) - keep_side_effects(exprs, chunk, nil, ast) - utils.hook("chunk", ast, scope) - allowed_globals = old_globals - utils.root.reset() - return flatten(chunk, opts0) - end - local function traceback_frame(info) - if ((info.what == "C") and info.name) then - return string.format(" [C]: in function '%s'", info.name) - elseif (info.what == "C") then - return " [C]: in ?" - else - local remap = sourcemap[info.source] - if (remap and remap[info.currentline]) then - if ((remap[info.currentline][1] or "unknown") ~= "unknown") then - info.short_src = sourcemap[("@" .. remap[info.currentline][1])].short_src - else - info.short_src = remap.short_src - end - info.currentline = (remap[info.currentline][2] or -1) - else - end - if (info.what == "Lua") then - local function _344_() - if info.name then - return ("'" .. info.name .. "'") - else - return "?" - end - end - return string.format(" %s:%d: in function %s", info.short_src, info.currentline, _344_()) - elseif (info.short_src == "(tail call)") then - return " (tail call)" - else - return string.format(" %s:%d: in main chunk", info.short_src, info.currentline) - end - end - end - local function traceback(_3fmsg, _3fstart) - local msg = tostring((_3fmsg or "")) - if ((msg:find("^Compile error") or msg:find("^Parse error")) and not utils["debug-on?"]("trace")) then - return msg - else - local lines = {} - if (msg:find(":%d+: Compile error") or msg:find(":%d+: Parse error")) then - table.insert(lines, msg) - else - local newmsg = msg:gsub("^[^:]*:%d+:%s+", "runtime error: ") - table.insert(lines, newmsg) - end - table.insert(lines, "stack traceback:") - local done_3f, level = false, (_3fstart or 2) - while not done_3f do - do - local _348_ = debug.getinfo(level, "Sln") - if (_348_ == nil) then - done_3f = true - elseif (nil ~= _348_) then - local info = _348_ - table.insert(lines, traceback_frame(info)) - else - end - end - level = (level + 1) - end - return table.concat(lines, "\n") - end - end - local function entry_transform(fk, fv) - local function _351_(k, v) - if (type(k) == "number") then - return k, fv(v) - else - return fk(k), fv(v) - end - end - return _351_ - end - local function mixed_concat(t, joiner) - local seen = {} - local ret, s = "", "" - for k, v in ipairs(t) do - table.insert(seen, k) - ret = (ret .. s .. v) - s = joiner - end - for k, v in utils.stablepairs(t) do - if not seen[k] then - ret = (ret .. s .. "[" .. k .. "]" .. "=" .. v) - s = joiner - else - end - end - return ret - end - local function do_quote(form, scope, parent, runtime_3f) - local function q(x) - return do_quote(x, scope, parent, runtime_3f) - end - if utils["varg?"](form) then - assert_compile(not runtime_3f, "quoted ... may only be used at compile time", form) - return "_VARARG" - elseif utils["sym?"](form) then - local filename - if form.filename then - filename = string.format("%q", form.filename) - else - filename = "nil" - end - local symstr = tostring(form) - assert_compile(not runtime_3f, "symbols may only be used at compile time", form) - if (symstr:find("#$") or symstr:find("#[:.]")) then - return string.format("sym('%s', {filename=%s, line=%s})", autogensym(symstr, scope), filename, (form.line or "nil")) - else - return string.format("sym('%s', {quoted=true, filename=%s, line=%s})", symstr, filename, (form.line or "nil")) - end - elseif (utils["list?"](form) and utils["sym?"](form[1]) and (tostring(form[1]) == "unquote")) then - local payload = form[2] - local res = unpack(compile1(payload, scope, parent)) - return res[1] - elseif utils["list?"](form) then - local mapped - local function _356_() - return nil - end - mapped = utils.kvmap(form, entry_transform(_356_, q)) - local filename - if form.filename then - filename = string.format("%q", form.filename) - else - filename = "nil" - end - assert_compile(not runtime_3f, "lists may only be used at compile time", form) - return string.format(("setmetatable({filename=%s, line=%s, bytestart=%s, %s}" .. ", getmetatable(list()))"), filename, (form.line or "nil"), (form.bytestart or "nil"), mixed_concat(mapped, ", ")) - elseif utils["sequence?"](form) then - local mapped = utils.kvmap(form, entry_transform(q, q)) - local source = getmetatable(form) - local filename - if source.filename then - filename = string.format("%q", source.filename) - else - filename = "nil" - end - local _359_ - if source then - _359_ = source.line - else - _359_ = "nil" - end - return string.format("setmetatable({%s}, {filename=%s, line=%s, sequence=%s})", mixed_concat(mapped, ", "), filename, _359_, "(getmetatable(sequence()))['sequence']") - elseif (type(form) == "table") then - local mapped = utils.kvmap(form, entry_transform(q, q)) - local source = getmetatable(form) - local filename - if source.filename then - filename = string.format("%q", source.filename) - else - filename = "nil" - end - local function _362_() - if source then - return source.line - else - return "nil" - end - end - return string.format("setmetatable({%s}, {filename=%s, line=%s})", mixed_concat(mapped, ", "), filename, _362_()) - elseif (type(form) == "string") then - return serialize_string(form) - else - return tostring(form) - end - end - return {compile = compile, compile1 = compile1, ["compile-stream"] = compile_stream, ["compile-string"] = compile_string, emit = emit, destructure = destructure, ["require-include"] = require_include, autogensym = autogensym, gensym = gensym, ["do-quote"] = do_quote, ["global-mangling"] = global_mangling, ["global-unmangling"] = global_unmangling, ["apply-manglings"] = apply_manglings, macroexpand = macroexpand_2a, ["declare-local"] = declare_local, ["make-scope"] = make_scope, ["keep-side-effects"] = keep_side_effects, ["symbol-to-expression"] = symbol_to_expression, assert = assert_compile, scopes = scopes, traceback = traceback, metadata = make_metadata(), sourcemap = sourcemap} -end -package.preload["fennel.friend"] = package.preload["fennel.friend"] or function(...) - local utils = require("fennel.utils") - local suggestions = {["unexpected multi symbol (.*)"] = {"removing periods or colons from %s"}, ["use of global (.*) is aliased by a local"] = {"renaming local %s", "refer to the global using _G.%s instead of directly"}, ["local (.*) was overshadowed by a special form or macro"] = {"renaming local %s"}, ["global (.*) conflicts with local"] = {"renaming local %s"}, ["expected var (.*)"] = {"declaring %s using var instead of let/local", "introducing a new local instead of changing the value of %s"}, ["expected macros to be table"] = {"ensuring your macro definitions return a table"}, ["expected each macro to be function"] = {"ensuring that the value for each key in your macros table contains a function", "avoid defining nested macro tables"}, ["macro not found in macro module"] = {"checking the keys of the imported macro module's returned table"}, ["macro tried to bind (.*) without gensym"] = {"changing to %s# when introducing identifiers inside macros"}, ["unknown identifier in strict mode: (.*)"] = {"looking to see if there's a typo", "using the _G table instead, eg. _G.%s if you really want a global", "moving this code to somewhere that %s is in scope", "binding %s as a local in the scope of this code"}, ["expected a function.* to call"] = {"removing the empty parentheses", "using square brackets if you want an empty table"}, ["cannot call literal value"] = {"checking for typos", "checking for a missing function name"}, ["unexpected vararg"] = {"putting \"...\" at the end of the fn parameters if the vararg was intended"}, ["multisym method calls may only be in call position"] = {"using a period instead of a colon to reference a table's fields", "putting parens around this"}, ["unused local (.*)"] = {"renaming the local to _%s if it is meant to be unused", "fixing a typo so %s is used", "disabling the linter which checks for unused locals"}, ["expected parameters"] = {"adding function parameters as a list of identifiers in brackets"}, ["unable to bind (.*)"] = {"replacing the %s with an identifier"}, ["expected rest argument before last parameter"] = {"moving & to right before the final identifier when destructuring"}, ["expected vararg as last parameter"] = {"moving the \"...\" to the end of the parameter list"}, ["expected symbol for function parameter: (.*)"] = {"changing %s to an identifier instead of a literal value"}, ["could not compile value of type "] = {"debugging the macro you're calling to return a list or table"}, ["expected local"] = {"looking for a typo", "looking for a local which is used out of its scope"}, ["expected body expression"] = {"putting some code in the body of this form after the bindings"}, ["expected binding and iterator"] = {"making sure you haven't omitted a local name or iterator"}, ["expected binding sequence"] = {"placing a table here in square brackets containing identifiers to bind"}, ["expected even number of name/value bindings"] = {"finding where the identifier or value is missing"}, ["may only be used at compile time"] = {"moving this to inside a macro if you need to manipulate symbols/lists", "using square brackets instead of parens to construct a table"}, ["unexpected closing delimiter (.)"] = {"deleting %s", "adding matching opening delimiter earlier"}, ["mismatched closing delimiter (.), expected (.)"] = {"replacing %s with %s", "deleting %s", "adding matching opening delimiter earlier"}, ["expected even number of values in table literal"] = {"removing a key", "adding a value"}, ["expected whitespace before opening delimiter"] = {"adding whitespace"}, ["invalid character: (.)"] = {"deleting or replacing %s", "avoiding reserved characters like \", \\, ', ~, ;, @, `, and comma"}, ["could not read number (.*)"] = {"removing the non-digit character", "beginning the identifier with a non-digit if it is not meant to be a number"}, ["can't start multisym segment with a digit"] = {"removing the digit", "adding a non-digit before the digit"}, ["malformed multisym"] = {"ensuring each period or colon is not followed by another period or colon"}, ["method must be last component"] = {"using a period instead of a colon for field access", "removing segments after the colon", "making the method call, then looking up the field on the result"}, ["$ and $... in hashfn are mutually exclusive"] = {"modifying the hashfn so it only contains $... or $, $1, $2, $3, etc"}, ["tried to reference a macro at runtime"] = {"renaming the macro so as not to conflict with locals"}, ["tried to reference a special form at runtime"] = {"wrapping the special in a function if you need it to be first class"}, ["expected even number of pattern/body pairs"] = {"checking that every pattern has a body to go with it", "adding _ before the final body"}, ["unexpected arguments"] = {"removing an argument", "checking for typos"}, ["unexpected iterator clause"] = {"removing an argument", "checking for typos"}} - local unpack = (table.unpack or _G.unpack) - local function suggest(msg) - local suggestion = nil - for pat, sug in pairs(suggestions) do - local matches = {msg:match(pat)} - if (0 < #matches) then - if ("table" == type(sug)) then - local out = {} - for _, s in ipairs(sug) do - table.insert(out, s:format(unpack(matches))) - end - suggestion = out - else - suggestion = sug(matches) - end - else - end - end - return suggestion - end - local function read_line_from_file(filename, line) - local bytes = 0 - local f = assert(io.open(filename)) - local _ - for _0 = 1, (line - 1) do - bytes = (bytes + 1 + #f:read()) - end - _ = nil - local codeline = f:read() - f:close() - return codeline, bytes - end - local function read_line_from_string(matcher, target_line, _3fcurrent_line, _3fbytes) - local this_line, newline = matcher() - local current_line = (_3fcurrent_line or 1) - local bytes = ((_3fbytes or 0) + #this_line + #newline) - if (target_line == current_line) then - return this_line, (bytes - #this_line - 1) - elseif this_line then - return read_line_from_string(matcher, target_line, (current_line + 1), bytes) - else - return nil - end - end - local function read_line(filename, line, source) - if source then - return read_line_from_string(string.gmatch((source .. "\n"), "(.-)(\13?\n)"), line) - else - return read_line_from_file(filename, line) - end - end - local function friendly_msg(msg, _156_, source) - local _arg_157_ = _156_ - local filename = _arg_157_["filename"] - local line = _arg_157_["line"] - local bytestart = _arg_157_["bytestart"] - local byteend = _arg_157_["byteend"] - local ok, codeline, bol = pcall(read_line, filename, line, source) - local suggestions0 = suggest(msg) - local out = {msg, ""} - if (ok and codeline) then - table.insert(out, codeline) - else - end - if (ok and codeline and bytestart and byteend) then - table.insert(out, (string.rep(" ", (bytestart - bol - 1)) .. "^" .. string.rep("^", math.min((byteend - bytestart), ((bol + #codeline) - bytestart))))) - else - end - if (ok and codeline and bytestart and not byteend) then - table.insert(out, (string.rep("-", (bytestart - bol - 1)) .. "^")) - table.insert(out, "") - else - end - if suggestions0 then - for _, suggestion in ipairs(suggestions0) do - table.insert(out, ("* Try %s."):format(suggestion)) - end - else - end - return table.concat(out, "\n") - end - local function assert_compile(condition, msg, ast, source) - if not condition then - local _let_162_ = utils["ast-source"](ast) - local filename = _let_162_["filename"] - local line = _let_162_["line"] - error(friendly_msg(("Compile error in %s:%s\n %s"):format((filename or "unknown"), (line or "?"), msg), utils["ast-source"](ast), source), 0) - else - end - return condition - end - local function parse_error(msg, filename, line, bytestart, source) - return error(friendly_msg(("Parse error in %s:%s\n %s"):format(filename, line, msg), {filename = filename, line = line, bytestart = bytestart}, source), 0) - end - return {["assert-compile"] = assert_compile, ["parse-error"] = parse_error} -end -package.preload["fennel.parser"] = package.preload["fennel.parser"] or function(...) - local utils = require("fennel.utils") - local friend = require("fennel.friend") - local unpack = (table.unpack or _G.unpack) - local function granulate(getchunk) - local c, index, done_3f = "", 1, false - local function _164_(parser_state) - if not done_3f then - if (index <= #c) then - local b = c:byte(index) - index = (index + 1) - return b - else - local _165_ = getchunk(parser_state) - local function _166_() - local char = _165_ - return (char ~= "") - end - if ((nil ~= _165_) and _166_()) then - local char = _165_ - c = char - index = 2 - return c:byte() - elseif true then - local _ = _165_ - done_3f = true - return nil - else - return nil - end - end - else - return nil - end - end - local function _170_() - c = "" - return nil - end - return _164_, _170_ - end - local function string_stream(str) - local str0 = str:gsub("^#!", ";;") - local index = 1 - local function _171_() - local r = str0:byte(index) - index = (index + 1) - return r - end - return _171_ - end - local delims = {[40] = 41, [41] = true, [91] = 93, [93] = true, [123] = 125, [125] = true} - local function whitespace_3f(b) - return ((b == 32) or ((b >= 9) and (b <= 13))) - end - local function sym_char_3f(b) - local b0 - if ("number" == type(b)) then - b0 = b - else - b0 = string.byte(b) - end - return ((b0 > 32) and not delims[b0] and (b0 ~= 127) and (b0 ~= 34) and (b0 ~= 39) and (b0 ~= 126) and (b0 ~= 59) and (b0 ~= 44) and (b0 ~= 64) and (b0 ~= 96)) - end - local prefixes = {[35] = "hashfn", [39] = "quote", [44] = "unquote", [96] = "quote"} - local function parser_fn(getbyte, filename, _173_) - local _arg_174_ = _173_ - local source = _arg_174_["source"] - local unfriendly = _arg_174_["unfriendly"] - local comments = _arg_174_["comments"] - local stack = {} - local line = 1 - local byteindex = 0 - local lastb = nil - local function ungetb(ub) - if (ub == 10) then - line = (line - 1) - else - end - byteindex = (byteindex - 1) - lastb = ub - return nil - end - local function getb() - local r = nil - if lastb then - r, lastb = lastb, nil - else - r = getbyte({["stack-size"] = #stack}) - end - byteindex = (byteindex + 1) - if (r == 10) then - line = (line + 1) - else - end - return r - end - local function parse_error(msg, byteindex_override) - if (nil == utils.hook("parse-error", msg, filename, (line or "?"), (byteindex_override or byteindex), source, utils.root.reset)) then - utils.root.reset() - if (unfriendly or not friend or not _G.io or not _G.io.read) then - return error(string.format("%s:%s: Parse error: %s", filename, (line or "?"), msg), 0) - else - return friend["parse-error"](msg, filename, (line or "?"), (byteindex_override or byteindex), source) - end - else - return nil - end - end - local function parse_stream() - local whitespace_since_dispatch, done_3f, retval = true - local function dispatch(v) - local _180_ = stack[#stack] - if (_180_ == nil) then - retval, done_3f, whitespace_since_dispatch = v, true, false - return nil - elseif ((_G.type(_180_) == "table") and (nil ~= (_180_).prefix)) then - local prefix = (_180_).prefix - local source0 - do - local _181_ = table.remove(stack) - do end (_181_)["byteend"] = byteindex - source0 = _181_ - end - local list = utils.list(utils.sym(prefix, source0), v) - for k, v0 in pairs(source0) do - list[k] = v0 - end - return dispatch(list) - elseif (nil ~= _180_) then - local top = _180_ - whitespace_since_dispatch = false - return table.insert(top, v) - else - return nil - end - end - local function badend() - local accum = utils.map(stack, "closer") - local _183_ - if (#stack == 1) then - _183_ = "" - else - _183_ = "s" - end - return parse_error(string.format("expected closing delimiter%s %s", _183_, string.char(unpack(accum)))) - end - local function skip_whitespace(b) - if (b and whitespace_3f(b)) then - whitespace_since_dispatch = true - return skip_whitespace(getb()) - elseif (not b and (#stack > 0)) then - return badend() - else - return b - end - end - local function parse_comment(b, contents) - if (b and (10 ~= b)) then - local function _187_() - local _186_ = contents - table.insert(_186_, string.char(b)) - return _186_ - end - return parse_comment(getb(), _187_()) - elseif comments then - return dispatch(utils.comment(table.concat(contents), {line = (line - 1), filename = filename})) - else - return b - end - end - local function open_table(b) - if not whitespace_since_dispatch then - parse_error(("expected whitespace before opening delimiter " .. string.char(b))) - else - end - return table.insert(stack, {bytestart = byteindex, closer = delims[b], filename = filename, line = line}) - end - local function close_list(list) - return dispatch(setmetatable(list, getmetatable(utils.list()))) - end - local function close_sequence(tbl) - local val = utils.sequence(unpack(tbl)) - for k, v in pairs(tbl) do - getmetatable(val)[k] = v - end - return dispatch(val) - end - local function add_comment_at(comments0, index, node) - local _190_ = (comments0)[index] - if (nil ~= _190_) then - local existing = _190_ - return table.insert(existing, node) - elseif true then - local _ = _190_ - comments0[index] = {node} - return nil - else - return nil - end - end - local function next_noncomment(tbl, i) - if utils["comment?"](tbl[i]) then - return next_noncomment(tbl, (i + 1)) - else - return tbl[i] - end - end - local function extract_comments(tbl) - local comments0 = {keys = {}, values = {}, last = {}} - while utils["comment?"](tbl[#tbl]) do - table.insert(comments0.last, 1, table.remove(tbl)) - end - local last_key_3f = false - for i, node in ipairs(tbl) do - if not utils["comment?"](node) then - last_key_3f = not last_key_3f - elseif last_key_3f then - add_comment_at(comments0.values, next_noncomment(tbl, i), node) - else - add_comment_at(comments0.keys, next_noncomment(tbl, i), node) - end - end - for i = #tbl, 1, -1 do - if utils["comment?"](tbl[i]) then - table.remove(tbl, i) - else - end - end - return comments0 - end - local function close_curly_table(tbl) - local comments0 = extract_comments(tbl) - local keys = {} - local val = {} - if ((#tbl % 2) ~= 0) then - byteindex = (byteindex - 1) - parse_error("expected even number of values in table literal") - else - end - setmetatable(val, tbl) - for i = 1, #tbl, 2 do - if ((tostring(tbl[i]) == ":") and utils["sym?"](tbl[(i + 1)]) and utils["sym?"](tbl[i])) then - tbl[i] = tostring(tbl[(i + 1)]) - else - end - val[tbl[i]] = tbl[(i + 1)] - table.insert(keys, tbl[i]) - end - tbl.comments = comments0 - tbl.keys = keys - return dispatch(val) - end - local function close_table(b) - local top = table.remove(stack) - if (top == nil) then - parse_error(("unexpected closing delimiter " .. string.char(b))) - else - end - if (top.closer and (top.closer ~= b)) then - parse_error(("mismatched closing delimiter " .. string.char(b) .. ", expected " .. string.char(top.closer))) - else - end - top.byteend = byteindex - if (b == 41) then - return close_list(top) - elseif (b == 93) then - return close_sequence(top) - else - return close_curly_table(top) - end - end - local function parse_string_loop(chars, b, state) - table.insert(chars, b) - local state0 - do - local _200_ = {state, b} - if ((_G.type(_200_) == "table") and ((_200_)[1] == "base") and ((_200_)[2] == 92)) then - state0 = "backslash" - elseif ((_G.type(_200_) == "table") and ((_200_)[1] == "base") and ((_200_)[2] == 34)) then - state0 = "done" - elseif ((_G.type(_200_) == "table") and ((_200_)[1] == "backslash") and ((_200_)[2] == 10)) then - table.remove(chars, (#chars - 1)) - state0 = "base" - elseif true then - local _ = _200_ - state0 = "base" - else - state0 = nil - end - end - if (b and (state0 ~= "done")) then - return parse_string_loop(chars, getb(), state0) - else - return b - end - end - local function escape_char(c) - return ({[7] = "\\a", [8] = "\\b", [9] = "\\t", [10] = "\\n", [11] = "\\v", [12] = "\\f", [13] = "\\r"})[c:byte()] - end - local function parse_string() - table.insert(stack, {closer = 34}) - local chars = {34} - if not parse_string_loop(chars, getb(), "base") then - badend() - else - end - table.remove(stack) - local raw = string.char(unpack(chars)) - local formatted = raw:gsub("[\7-\13]", escape_char) - local _204_ = (rawget(_G, "loadstring") or load)(("return " .. formatted)) - if (nil ~= _204_) then - local load_fn = _204_ - return dispatch(load_fn()) - elseif (_204_ == nil) then - return parse_error(("Invalid string: " .. raw)) - else - return nil - end - end - local function parse_prefix(b) - table.insert(stack, {prefix = prefixes[b], filename = filename, line = line, bytestart = byteindex}) - local nextb = getb() - if (whitespace_3f(nextb) or (true == delims[nextb])) then - if (b ~= 35) then - parse_error("invalid whitespace after quoting prefix") - else - end - table.remove(stack) - dispatch(utils.sym("#")) - else - end - return ungetb(nextb) - end - local function parse_sym_loop(chars, b) - if (b and sym_char_3f(b)) then - table.insert(chars, b) - return parse_sym_loop(chars, getb()) - else - if b then - ungetb(b) - else - end - return chars - end - end - local function parse_number(rawstr) - local number_with_stripped_underscores = (not rawstr:find("^_") and rawstr:gsub("_", "")) - if rawstr:match("^%d") then - dispatch((tonumber(number_with_stripped_underscores) or parse_error(("could not read number \"" .. rawstr .. "\"")))) - return true - else - local _210_ = tonumber(number_with_stripped_underscores) - if (nil ~= _210_) then - local x = _210_ - dispatch(x) - return true - elseif true then - local _ = _210_ - return false - else - return nil - end - end - end - local function check_malformed_sym(rawstr) - if (rawstr:match("^~") and (rawstr ~= "~=")) then - return parse_error("invalid character: ~") - elseif rawstr:match("%.[0-9]") then - return parse_error(("can't start multisym segment with a digit: " .. rawstr), (((byteindex - #rawstr) + rawstr:find("%.[0-9]")) + 1)) - elseif (rawstr:match("[%.:][%.:]") and (rawstr ~= "..") and (rawstr ~= "$...")) then - return parse_error(("malformed multisym: " .. rawstr), ((byteindex - #rawstr) + 1 + rawstr:find("[%.:][%.:]"))) - elseif ((rawstr ~= ":") and rawstr:match(":$")) then - return parse_error(("malformed multisym: " .. rawstr), ((byteindex - #rawstr) + 1 + rawstr:find(":$"))) - elseif rawstr:match(":.+[%.:]") then - return parse_error(("method must be last component of multisym: " .. rawstr), ((byteindex - #rawstr) + rawstr:find(":.+[%.:]"))) - else - return rawstr - end - end - local function parse_sym(b) - local bytestart = byteindex - local rawstr = string.char(unpack(parse_sym_loop({b}, getb()))) - local source0 = {byteend = byteindex, bytestart = bytestart, filename = filename, line = line} - if (rawstr == "true") then - return dispatch(true) - elseif (rawstr == "false") then - return dispatch(false) - elseif (rawstr == "...") then - return dispatch(utils.varg(source0)) - elseif rawstr:match("^:.+$") then - return dispatch(rawstr:sub(2)) - elseif not parse_number(rawstr) then - return dispatch(utils.sym(check_malformed_sym(rawstr), source0)) - else - return nil - end - end - local function parse_loop(b) - if not b then - elseif (b == 59) then - parse_comment(getb(), {";"}) - elseif (type(delims[b]) == "number") then - open_table(b) - elseif delims[b] then - close_table(b) - elseif (b == 34) then - parse_string(b) - elseif prefixes[b] then - parse_prefix(b) - elseif (sym_char_3f(b) or (b == string.byte("~"))) then - parse_sym(b) - elseif not utils.hook("illegal-char", b, getb, ungetb, dispatch) then - parse_error(("invalid character: " .. string.char(b))) - else - end - if not b then - return nil - elseif done_3f then - return true, retval - else - return parse_loop(skip_whitespace(getb())) - end - end - return parse_loop(skip_whitespace(getb())) - end - local function _217_() - stack, line, byteindex, lastb = {}, 1, 0, nil - return nil - end - return parse_stream, _217_ - end - local function parser(stream_or_string, _3ffilename, _3foptions) - local filename = (_3ffilename or "unknown") - local options = (_3foptions or utils.root.options or {}) - assert(("string" == type(filename)), "expected filename as second argument to parser") - if ("string" == type(stream_or_string)) then - return parser_fn(string_stream(stream_or_string), filename, options) - else - return parser_fn(stream_or_string, filename, options) - end - end - return {granulate = granulate, parser = parser, ["string-stream"] = string_stream, ["sym-char?"] = sym_char_3f} -end -package.preload["fennel.view"] = package.preload["fennel.view"] or function(...) - local type_order = {number = 1, boolean = 2, string = 3, table = 4, ["function"] = 5, userdata = 6, thread = 7} - local lua_pairs = pairs - local lua_ipairs = ipairs - local function pairs(t) - local _1_ = getmetatable(t) - if ((_G.type(_1_) == "table") and (nil ~= (_1_).__pairs)) then - local p = (_1_).__pairs - return p(t) - elseif true then - local _ = _1_ - return lua_pairs(t) - else - return nil - end - end - local function ipairs(t) - local _3_ = getmetatable(t) - if ((_G.type(_3_) == "table") and (nil ~= (_3_).__ipairs)) then - local i = (_3_).__ipairs - return i(t) - elseif true then - local _ = _3_ - return lua_ipairs(t) - else - return nil - end - end - local function length_2a(t) - local _5_ = getmetatable(t) - if ((_G.type(_5_) == "table") and (nil ~= (_5_).__len)) then - local l = (_5_).__len - return l(t) - elseif true then - local _ = _5_ - return #t - else - return nil - end - end - local function sort_keys(_7_, _9_) - local _arg_8_ = _7_ - local a = _arg_8_[1] - local _arg_10_ = _9_ - local b = _arg_10_[1] - local ta = type(a) - local tb = type(b) - if ((ta == tb) and ((ta == "string") or (ta == "number"))) then - return (a < b) - else - local dta = type_order[ta] - local dtb = type_order[tb] - if (dta and dtb) then - return (dta < dtb) - elseif dta then - return true - elseif dtb then - return false - else - return (ta < tb) - end - end - end - local function max_index_gap(kv) - local gap = 0 - if (length_2a(kv) > 0) then - local i = 0 - for _, _13_ in ipairs(kv) do - local _each_14_ = _13_ - local k = _each_14_[1] - if ((k - i) > gap) then - gap = (k - i) - else - end - i = k - end - else - end - return gap - end - local function fill_gaps(kv) - local missing_indexes = {} - local i = 0 - for _, _17_ in ipairs(kv) do - local _each_18_ = _17_ - local j = _each_18_[1] - i = (i + 1) - while (i < j) do - table.insert(missing_indexes, i) - i = (i + 1) - end - end - for _, k in ipairs(missing_indexes) do - table.insert(kv, k, {k}) - end - return nil - end - local function table_kv_pairs(t, options) - local assoc_3f = false - local kv = {} - local insert = table.insert - for k, v in pairs(t) do - if ((type(k) ~= "number") or (k < 1)) then - assoc_3f = true - else - end - insert(kv, {k, v}) - end - table.sort(kv, sort_keys) - if not assoc_3f then - if (max_index_gap(kv) > options["max-sparse-gap"]) then - assoc_3f = true - else - fill_gaps(kv) - end - else - end - if (length_2a(kv) == 0) then - return kv, "empty" - else - local function _22_() - if assoc_3f then - return "table" - else - return "seq" - end - end - return kv, _22_() - end - end - local function count_table_appearances(t, appearances) - if (type(t) == "table") then - if not appearances[t] then - appearances[t] = 1 - for k, v in pairs(t) do - count_table_appearances(k, appearances) - count_table_appearances(v, appearances) - end - else - appearances[t] = ((appearances[t] or 0) + 1) - end - else - end - return appearances - end - local function save_table(t, seen) - local seen0 = (seen or {len = 0}) - local id = (seen0.len + 1) - if not (seen0)[t] then - seen0[t] = id - seen0.len = id - else - end - return seen0 - end - local function detect_cycle(t, seen, _3fk) - if ("table" == type(t)) then - seen[t] = true - local _27_, _28_ = next(t, _3fk) - if ((nil ~= _27_) and (nil ~= _28_)) then - local k = _27_ - local v = _28_ - return (seen[k] or detect_cycle(k, seen) or seen[v] or detect_cycle(v, seen) or detect_cycle(t, seen, k)) - else - return nil - end - else - return nil - end - end - local function visible_cycle_3f(t, options) - return (options["detect-cycles?"] and detect_cycle(t, {}) and save_table(t, options.seen) and (1 < (options.appearances[t] or 0))) - end - local function table_indent(indent, id) - local opener_length - if id then - opener_length = (length_2a(tostring(id)) + 2) - else - opener_length = 1 - end - return (indent + opener_length) - end - local pp = nil - local function concat_table_lines(elements, options, multiline_3f, indent, table_type, prefix) - local indent_str = ("\n" .. string.rep(" ", indent)) - local open - local function _32_() - if ("seq" == table_type) then - return "[" - else - return "{" - end - end - open = ((prefix or "") .. _32_()) - local close - if ("seq" == table_type) then - close = "]" - else - close = "}" - end - local oneline = (open .. table.concat(elements, " ") .. close) - if (not options["one-line?"] and (multiline_3f or ((indent + length_2a(oneline)) > options["line-length"]))) then - return (open .. table.concat(elements, indent_str) .. close) - else - return oneline - end - end - local function utf8_len(x) - local n = 0 - for _ in string.gmatch(x, "[%z\1-\127\192-\247]") do - n = (n + 1) - end - return n - end - local function pp_associative(t, kv, options, indent) - local multiline_3f = false - local id = options.seen[t] - if (options.level >= options.depth) then - return "{...}" - elseif (id and options["detect-cycles?"]) then - return ("@" .. id .. "{...}") - else - local visible_cycle_3f0 = visible_cycle_3f(t, options) - local id0 = (visible_cycle_3f0 and options.seen[t]) - local indent0 = table_indent(indent, id0) - local slength - if options["utf8?"] then - slength = utf8_len - else - local function _35_(_241) - return #_241 - end - slength = _35_ - end - local prefix - if visible_cycle_3f0 then - prefix = ("@" .. id0) - else - prefix = "" - end - local items - do - local tbl_14_auto = {} - local i_15_auto = #tbl_14_auto - for _, _38_ in pairs(kv) do - local _each_39_ = _38_ - local k = _each_39_[1] - local v = _each_39_[2] - local val_16_auto - do - local k0 = pp(k, options, (indent0 + 1), true) - local v0 = pp(v, options, (indent0 + slength(k0) + 1)) - multiline_3f = (multiline_3f or k0:find("\n") or v0:find("\n")) - val_16_auto = (k0 .. " " .. v0) - end - if (nil ~= val_16_auto) then - i_15_auto = (i_15_auto + 1) - do end (tbl_14_auto)[i_15_auto] = val_16_auto - else - end - end - items = tbl_14_auto - end - return concat_table_lines(items, options, multiline_3f, indent0, "table", prefix) - end - end - local function pp_sequence(t, kv, options, indent) - local multiline_3f = false - local id = options.seen[t] - if (options.level >= options.depth) then - return "[...]" - elseif (id and options["detect-cycles?"]) then - return ("@" .. id .. "[...]") - else - local visible_cycle_3f0 = visible_cycle_3f(t, options) - local id0 = (visible_cycle_3f0 and options.seen[t]) - local indent0 = table_indent(indent, id0) - local prefix - if visible_cycle_3f0 then - prefix = ("@" .. id0) - else - prefix = "" - end - local items - do - local tbl_14_auto = {} - local i_15_auto = #tbl_14_auto - for _, _43_ in pairs(kv) do - local _each_44_ = _43_ - local _0 = _each_44_[1] - local v = _each_44_[2] - local val_16_auto - do - local v0 = pp(v, options, indent0) - multiline_3f = (multiline_3f or v0:find("\n")) - val_16_auto = v0 - end - if (nil ~= val_16_auto) then - i_15_auto = (i_15_auto + 1) - do end (tbl_14_auto)[i_15_auto] = val_16_auto - else - end - end - items = tbl_14_auto - end - return concat_table_lines(items, options, multiline_3f, indent0, "seq", prefix) - end - end - local function concat_lines(lines, options, indent, force_multi_line_3f) - if (length_2a(lines) == 0) then - if options["empty-as-sequence?"] then - return "[]" - else - return "{}" - end - else - local oneline - local _48_ - do - local tbl_14_auto = {} - local i_15_auto = #tbl_14_auto - for _, line in ipairs(lines) do - local val_16_auto = line:gsub("^%s+", "") - if (nil ~= val_16_auto) then - i_15_auto = (i_15_auto + 1) - do end (tbl_14_auto)[i_15_auto] = val_16_auto - else - end - end - _48_ = tbl_14_auto - end - oneline = table.concat(_48_, " ") - if (not options["one-line?"] and (force_multi_line_3f or oneline:find("\n") or ((indent + length_2a(oneline)) > options["line-length"]))) then - return table.concat(lines, ("\n" .. string.rep(" ", indent))) - else - return oneline - end - end - end - local function pp_metamethod(t, metamethod, options, indent) - if (options.level >= options.depth) then - if options["empty-as-sequence?"] then - return "[...]" - else - return "{...}" - end - else - local _ - local function _53_(_241) - return visible_cycle_3f(_241, options) - end - options["visible-cycle?"] = _53_ - _ = nil - local lines, force_multi_line_3f = metamethod(t, pp, options, indent) - options["visible-cycle?"] = nil - local _54_ = type(lines) - if (_54_ == "string") then - return lines - elseif (_54_ == "table") then - return concat_lines(lines, options, indent, force_multi_line_3f) - elseif true then - local _0 = _54_ - return error("__fennelview metamethod must return a table of lines") - else - return nil - end - end - end - local function pp_table(x, options, indent) - options.level = (options.level + 1) - local x0 - do - local _57_ - if options["metamethod?"] then - local _58_ = x - if (nil ~= _58_) then - local _59_ = getmetatable(_58_) - if (nil ~= _59_) then - _57_ = (_59_).__fennelview - else - _57_ = _59_ - end - else - _57_ = _58_ - end - else - _57_ = nil - end - if (nil ~= _57_) then - local metamethod = _57_ - x0 = pp_metamethod(x, metamethod, options, indent) - elseif true then - local _ = _57_ - local _63_, _64_ = table_kv_pairs(x, options) - if (true and (_64_ == "empty")) then - local _0 = _63_ - if options["empty-as-sequence?"] then - x0 = "[]" - else - x0 = "{}" - end - elseif ((nil ~= _63_) and (_64_ == "table")) then - local kv = _63_ - x0 = pp_associative(x, kv, options, indent) - elseif ((nil ~= _63_) and (_64_ == "seq")) then - local kv = _63_ - x0 = pp_sequence(x, kv, options, indent) - else - x0 = nil - end - else - x0 = nil - end - end - options.level = (options.level - 1) - return x0 - end - local function number__3estring(n) - local _68_ = string.gsub(tostring(n), ",", ".") - return _68_ - end - local function colon_string_3f(s) - return s:find("^[-%w?^_!$%&*+./@|<=>]+$") - end - local utf8_inits = {{["min-byte"] = 0, ["max-byte"] = 127, ["min-code"] = 0, ["max-code"] = 127, len = 1}, {["min-byte"] = 192, ["max-byte"] = 223, ["min-code"] = 128, ["max-code"] = 2047, len = 2}, {["min-byte"] = 224, ["max-byte"] = 239, ["min-code"] = 2048, ["max-code"] = 65535, len = 3}, {["min-byte"] = 240, ["max-byte"] = 247, ["min-code"] = 65536, ["max-code"] = 1114111, len = 4}} - local function utf8_escape(str) - local function validate_utf8(str0, index) - local inits = utf8_inits - local byte = string.byte(str0, index) - local init - do - local ret = nil - for _, init0 in ipairs(inits) do - if ret then break end - ret = (byte and (function(_69_,_70_,_71_) return (_69_ >= _70_) and (_70_ >= _71_) end)(init0["max-byte"],byte,init0["min-byte"]) and init0) - end - init = ret - end - local code - local function _72_() - local code0 - if init then - code0 = (byte - init["min-byte"]) - else - code0 = nil - end - for i = (index + 1), (index + init.len + -1) do - local byte0 = string.byte(str0, i) - code0 = (byte0 and code0 and (function(_74_,_75_,_76_) return (_74_ >= _75_) and (_75_ >= _76_) end)(191,byte0,128) and ((code0 * 64) + (byte0 - 128))) - end - return code0 - end - code = (init and _72_()) - if (code and (function(_77_,_78_,_79_) return (_77_ >= _78_) and (_78_ >= _79_) end)(init["max-code"],code,init["min-code"]) and not (function(_80_,_81_,_82_) return (_80_ >= _81_) and (_81_ >= _82_) end)(57343,code,55296)) then - return init.len - else - return nil - end - end - local index = 1 - local output = {} - while (index <= #str) do - local nexti = (string.find(str, "[\128-\255]", index) or (#str + 1)) - local len = validate_utf8(str, nexti) - table.insert(output, string.sub(str, index, (nexti + (len or 0) + -1))) - if (not len and (nexti <= #str)) then - table.insert(output, string.format("\\%03d", string.byte(str, nexti))) - else - end - if len then - index = (nexti + len) - else - index = (nexti + 1) - end - end - return table.concat(output) - end - local function pp_string(str, options, indent) - local escs - local _86_ - if (options["escape-newlines?"] and (length_2a(str) < (options["line-length"] - indent))) then - _86_ = "\\n" - else - _86_ = "\n" - end - local function _88_(_241, _242) - return ("\\%03d"):format(_242:byte()) - end - escs = setmetatable({["\7"] = "\\a", ["\8"] = "\\b", ["\12"] = "\\f", ["\11"] = "\\v", ["\13"] = "\\r", ["\9"] = "\\t", ["\\"] = "\\\\", ["\""] = "\\\"", ["\n"] = _86_}, {__index = _88_}) - local str0 = ("\"" .. str:gsub("[%c\\\"]", escs) .. "\"") - if options["utf8?"] then - return utf8_escape(str0) - else - return str0 - end - end - local function make_options(t, options) - local defaults = {["line-length"] = 80, ["one-line?"] = false, depth = 128, ["detect-cycles?"] = true, ["empty-as-sequence?"] = false, ["metamethod?"] = true, ["prefer-colon?"] = false, ["escape-newlines?"] = false, ["utf8?"] = true, ["max-sparse-gap"] = 10} - local overrides = {level = 0, appearances = count_table_appearances(t, {}), seen = {len = 0}} - for k, v in pairs((options or {})) do - defaults[k] = v - end - for k, v in pairs(overrides) do - defaults[k] = v - end - return defaults - end - local function _90_(x, options, indent, colon_3f) - local indent0 = (indent or 0) - local options0 = (options or make_options(x)) - local x0 - if options0.preprocess then - x0 = options0.preprocess(x, options0) - else - x0 = x - end - local tv = type(x0) - local function _93_() - local _92_ = getmetatable(x0) - if (nil ~= _92_) then - return (_92_).__fennelview - else - return _92_ - end - end - if ((tv == "table") or ((tv == "userdata") and _93_())) then - return pp_table(x0, options0, indent0) - elseif (tv == "number") then - return number__3estring(x0) - else - local function _95_() - if (colon_3f ~= nil) then - return colon_3f - elseif ("function" == type(options0["prefer-colon?"])) then - return options0["prefer-colon?"](x0) - else - return options0["prefer-colon?"] - end - end - if ((tv == "string") and colon_string_3f(x0) and _95_()) then - return (":" .. x0) - elseif (tv == "string") then - return pp_string(x0, options0, indent0) - elseif ((tv == "boolean") or (tv == "nil")) then - return tostring(x0) - else - return ("#<" .. tostring(x0) .. ">") - end - end - end - pp = _90_ - local function view(x, _3foptions) - return pp(x, make_options(x, _3foptions), 0) - end - return view -end -package.preload["fennel.utils"] = package.preload["fennel.utils"] or function(...) - local view = require("fennel.view") - local version = "1.1.0" - local function luajit_vm_3f() - return ((nil ~= jit) and (type(jit) == "table") and (nil ~= jit.on) and (nil ~= jit.off) and (type(jit.version_num) == "number")) - end - local function luajit_vm_version() - local jit_os - if (jit.os == "OSX") then - jit_os = "macOS" - else - jit_os = jit.os - end - return (jit.version .. " " .. jit_os .. "/" .. jit.arch) - end - local function fengari_vm_3f() - return ((nil ~= fengari) and (type(fengari) == "table") and (nil ~= fengari.VERSION) and (type(fengari.VERSION_NUM) == "number")) - end - local function fengari_vm_version() - return (fengari.RELEASE .. " (" .. _VERSION .. ")") - end - local function lua_vm_version() - if luajit_vm_3f() then - return luajit_vm_version() - elseif fengari_vm_3f() then - return fengari_vm_version() - else - return ("PUC " .. _VERSION) - end - end - local function runtime_version() - return ("Fennel " .. version .. " on " .. lua_vm_version()) - end - local function warn(message) - if (_G.io and _G.io.stderr) then - return (_G.io.stderr):write(("--WARNING: %s\n"):format(tostring(message))) - else - return nil - end - end - local function stablepairs(t) - local keys = {} - local used_keys = {} - local succ = {} - if (getmetatable(t) and getmetatable(t).keys) then - for _, k in ipairs(getmetatable(t).keys) do - if used_keys[k] then - for i = #keys, 1, -1 do - if (keys[i] == k) then - table.remove(keys, i) - else - end - end - else - end - used_keys[k] = true - table.insert(keys, k) - end - else - for k in pairs(t) do - table.insert(keys, k) - end - local function _102_(_241, _242) - return (tostring(_241) < tostring(_242)) - end - table.sort(keys, _102_) - end - for i, k in ipairs(keys) do - succ[k] = keys[(i + 1)] - end - local function stablenext(tbl, idx) - local key - if (idx == nil) then - key = keys[1] - else - key = succ[idx] - end - local value - if (key == nil) then - value = nil - else - value = tbl[key] - end - return key, value - end - return stablenext, t, nil - end - local function map(t, f, _3fout) - local out = (_3fout or {}) - local f0 - if (type(f) == "function") then - f0 = f - else - local function _106_(_241) - return (_241)[f] - end - f0 = _106_ - end - for _, x in ipairs(t) do - local _108_ = f0(x) - if (nil ~= _108_) then - local v = _108_ - table.insert(out, v) - else - end - end - return out - end - local function kvmap(t, f, _3fout) - local out = (_3fout or {}) - local f0 - if (type(f) == "function") then - f0 = f - else - local function _110_(_241) - return (_241)[f] - end - f0 = _110_ - end - for k, x in stablepairs(t) do - local _112_, _113_ = f0(k, x) - if ((nil ~= _112_) and (nil ~= _113_)) then - local key = _112_ - local value = _113_ - out[key] = value - elseif (nil ~= _112_) then - local value = _112_ - table.insert(out, value) - else - end - end - return out - end - local function copy(from, _3fto) - local tbl_11_auto = (_3fto or {}) - for k, v in pairs((from or {})) do - local _115_, _116_ = k, v - if ((nil ~= _115_) and (nil ~= _116_)) then - local k_12_auto = _115_ - local v_13_auto = _116_ - tbl_11_auto[k_12_auto] = v_13_auto - else - end - end - return tbl_11_auto - end - local function member_3f(x, tbl, _3fn) - local _118_ = tbl[(_3fn or 1)] - if (_118_ == x) then - return true - elseif (_118_ == nil) then - return nil - elseif true then - local _ = _118_ - return member_3f(x, tbl, ((_3fn or 1) + 1)) - else - return nil - end - end - local function allpairs(tbl) - assert((type(tbl) == "table"), "allpairs expects a table") - local t = tbl - local seen = {} - local function allpairs_next(_, state) - local next_state, value = next(t, state) - if seen[next_state] then - return allpairs_next(nil, next_state) - elseif next_state then - seen[next_state] = true - return next_state, value - else - local _120_ = getmetatable(t) - if ((_G.type(_120_) == "table") and true) then - local __index = (_120_).__index - if ("table" == type(__index)) then - t = __index - return allpairs_next(t) - else - return nil - end - else - return nil - end - end - end - return allpairs_next - end - local function deref(self) - return self[1] - end - local nil_sym = nil - local function list__3estring(self, _3ftostring2) - local safe, max = {}, 0 - for k in pairs(self) do - if ((type(k) == "number") and (k > max)) then - max = k - else - end - end - for i = 1, max do - safe[i] = (((self[i] == nil) and nil_sym) or self[i]) - end - return ("(" .. table.concat(map(safe, (_3ftostring2 or view)), " ", 1, max) .. ")") - end - local function comment_view(c) - return c, true - end - local function sym_3d(a, b) - return ((deref(a) == deref(b)) and (getmetatable(a) == getmetatable(b))) - end - local function sym_3c(a, b) - return (a[1] < tostring(b)) - end - local symbol_mt = {__fennelview = deref, __tostring = deref, __eq = sym_3d, __lt = sym_3c, "SYMBOL"} - local expr_mt - local function _125_(x) - return tostring(deref(x)) - end - expr_mt = {__tostring = _125_, "EXPR"} - local list_mt = {__fennelview = list__3estring, __tostring = list__3estring, "LIST"} - local comment_mt = {__fennelview = comment_view, __tostring = deref, __eq = sym_3d, __lt = sym_3c, "COMMENT"} - local sequence_marker = {"SEQUENCE"} - local varg_mt = {__fennelview = deref, __tostring = deref, "VARARG"} - local getenv - local function _126_() - return nil - end - getenv = ((os and os.getenv) or _126_) - local function debug_on_3f(flag) - local level = (getenv("FENNEL_DEBUG") or "") - return ((level == "all") or level:find(flag)) - end - local function list(...) - return setmetatable({...}, list_mt) - end - local function sym(str, _3fsource) - local _127_ - do - local tbl_11_auto = {str} - for k, v in pairs((_3fsource or {})) do - local _128_, _129_ = nil, nil - if (type(k) == "string") then - _128_, _129_ = k, v - else - _128_, _129_ = nil - end - if ((nil ~= _128_) and (nil ~= _129_)) then - local k_12_auto = _128_ - local v_13_auto = _129_ - tbl_11_auto[k_12_auto] = v_13_auto - else - end - end - _127_ = tbl_11_auto - end - return setmetatable(_127_, symbol_mt) - end - nil_sym = sym("nil") - local function sequence(...) - return setmetatable({...}, {sequence = sequence_marker}) - end - local function expr(strcode, etype) - return setmetatable({type = etype, strcode}, expr_mt) - end - local function comment_2a(contents, _3fsource) - local _let_132_ = (_3fsource or {}) - local filename = _let_132_["filename"] - local line = _let_132_["line"] - return setmetatable({filename = filename, line = line, contents}, comment_mt) - end - local function varg(_3fsource) - local _133_ - do - local tbl_11_auto = {"..."} - for k, v in pairs((_3fsource or {})) do - local _134_, _135_ = nil, nil - if (type(k) == "string") then - _134_, _135_ = k, v - else - _134_, _135_ = nil - end - if ((nil ~= _134_) and (nil ~= _135_)) then - local k_12_auto = _134_ - local v_13_auto = _135_ - tbl_11_auto[k_12_auto] = v_13_auto - else - end - end - _133_ = tbl_11_auto - end - return setmetatable(_133_, varg_mt) - end - local function expr_3f(x) - return ((type(x) == "table") and (getmetatable(x) == expr_mt) and x) - end - local function varg_3f(x) - return ((type(x) == "table") and (getmetatable(x) == varg_mt) and x) - end - local function list_3f(x) - return ((type(x) == "table") and (getmetatable(x) == list_mt) and x) - end - local function sym_3f(x) - return ((type(x) == "table") and (getmetatable(x) == symbol_mt) and x) - end - local function sequence_3f(x) - local mt = ((type(x) == "table") and getmetatable(x)) - return (mt and (mt.sequence == sequence_marker) and x) - end - local function comment_3f(x) - return ((type(x) == "table") and (getmetatable(x) == comment_mt) and x) - end - local function table_3f(x) - return ((type(x) == "table") and not varg_3f(x) and (getmetatable(x) ~= list_mt) and (getmetatable(x) ~= symbol_mt) and not comment_3f(x) and x) - end - local function string_3f(x) - return (type(x) == "string") - end - local function multi_sym_3f(str) - if sym_3f(str) then - return multi_sym_3f(tostring(str)) - elseif (type(str) ~= "string") then - return false - else - local parts = {} - for part in str:gmatch("[^%.%:]+[%.%:]?") do - local last_char = part:sub(( - 1)) - if (last_char == ":") then - parts["multi-sym-method-call"] = true - else - end - if ((last_char == ":") or (last_char == ".")) then - parts[(#parts + 1)] = part:sub(1, ( - 2)) - else - parts[(#parts + 1)] = part - end - end - return ((#parts > 0) and (str:match("%.") or str:match(":")) and not str:match("%.%.") and (str:byte() ~= string.byte(".")) and (str:byte(( - 1)) ~= string.byte(".")) and parts) - end - end - local function quoted_3f(symbol) - return symbol.quoted - end - local function ast_source(ast) - if table_3f(ast) then - return (getmetatable(ast) or {}) - elseif ("table" == type(ast)) then - return ast - else - return {} - end - end - local function walk_tree(root, f, _3fcustom_iterator) - local function walk(iterfn, parent, idx, node) - if f(idx, node, parent) then - for k, v in iterfn(node) do - walk(iterfn, node, k, v) - end - return nil - else - return nil - end - end - walk((_3fcustom_iterator or pairs), nil, nil, root) - return root - end - local lua_keywords = {"and", "break", "do", "else", "elseif", "end", "false", "for", "function", "if", "in", "local", "nil", "not", "or", "repeat", "return", "then", "true", "until", "while", "goto"} - for i, v in ipairs(lua_keywords) do - lua_keywords[v] = i - end - local function valid_lua_identifier_3f(str) - return (str:match("^[%a_][%w_]*$") and not lua_keywords[str]) - end - local propagated_options = {"allowedGlobals", "indent", "correlate", "useMetadata", "env", "compiler-env", "compilerEnv"} - local function propagate_options(options, subopts) - for _, name in ipairs(propagated_options) do - subopts[name] = options[name] - end - return subopts - end - local root - local function _143_() - end - root = {chunk = nil, scope = nil, options = nil, reset = _143_} - root["set-reset"] = function(_144_) - local _arg_145_ = _144_ - local chunk = _arg_145_["chunk"] - local scope = _arg_145_["scope"] - local options = _arg_145_["options"] - local reset = _arg_145_["reset"] - root.reset = function() - root.chunk, root.scope, root.options, root.reset = chunk, scope, options, reset - return nil - end - return root.reset - end - local warned = {} - local function check_plugin_version(_146_) - local _arg_147_ = _146_ - local name = _arg_147_["name"] - local versions = _arg_147_["versions"] - local plugin = _arg_147_ - if (not member_3f(version:gsub("-dev", ""), (versions or {})) and not warned[plugin]) then - warned[plugin] = true - return warn(string.format("plugin %s does not support Fennel version %s", (name or "unknown"), version)) - else - return nil - end - end - local function hook(event, ...) - local result = nil - if (root.options and root.options.plugins) then - for _, plugin in ipairs(root.options.plugins) do - if result then break end - check_plugin_version(plugin) - local _149_ = plugin[event] - if (nil ~= _149_) then - local f = _149_ - result = f(...) - else - end - end - else - end - return result - end - return {warn = warn, allpairs = allpairs, stablepairs = stablepairs, copy = copy, kvmap = kvmap, map = map, ["walk-tree"] = walk_tree, ["member?"] = member_3f, list = list, sequence = sequence, sym = sym, varg = varg, expr = expr, comment = comment_2a, ["comment?"] = comment_3f, ["expr?"] = expr_3f, ["list?"] = list_3f, ["multi-sym?"] = multi_sym_3f, ["sequence?"] = sequence_3f, ["sym?"] = sym_3f, ["table?"] = table_3f, ["varg?"] = varg_3f, ["quoted?"] = quoted_3f, ["string?"] = string_3f, ["valid-lua-identifier?"] = valid_lua_identifier_3f, ["lua-keywords"] = lua_keywords, hook = hook, ["propagate-options"] = propagate_options, root = root, ["debug-on?"] = debug_on_3f, ["ast-source"] = ast_source, version = version, ["runtime-version"] = runtime_version, path = table.concat({"./?.fnl", "./?/init.fnl", getenv("FENNEL_PATH")}, ";"), ["macro-path"] = table.concat({"./?.fnl", "./?/init-macros.fnl", "./?/init.fnl", getenv("FENNEL_MACRO_PATH")}, ";")} -end -package.preload["fennel"] = package.preload["fennel"] or function(...) - local utils = require("fennel.utils") - local parser = require("fennel.parser") - local compiler = require("fennel.compiler") - local specials = require("fennel.specials") - local repl = require("fennel.repl") - local view = require("fennel.view") - local function eval_env(env, opts) - if (env == "_COMPILER") then - local env0 = specials["make-compiler-env"](nil, compiler.scopes.compiler, {}, opts) - if (opts.allowedGlobals == nil) then - opts.allowedGlobals = specials["current-global-names"](env0) - else - end - return specials["wrap-env"](env0) - else - return (env and specials["wrap-env"](env)) - end - end - local function eval_opts(options, str) - local opts = utils.copy(options) - if (opts.allowedGlobals == nil) then - opts.allowedGlobals = specials["current-global-names"](opts.env) - else - end - if (not opts.filename and not opts.source) then - opts.source = str - else - end - if (opts.env == "_COMPILER") then - opts.scope = compiler["make-scope"](compiler.scopes.compiler) - else - end - return opts - end - local function eval(str, options, ...) - local opts = eval_opts(options, str) - local env = eval_env(opts.env, opts) - local lua_source = compiler["compile-string"](str, opts) - local loader - local function _678_(...) - if opts.filename then - return ("@" .. opts.filename) - else - return str - end - end - loader = specials["load-code"](lua_source, env, _678_(...)) - opts.filename = nil - return loader(...) - end - local function dofile_2a(filename, options, ...) - local opts = utils.copy(options) - local f = assert(io.open(filename, "rb")) - local source = assert(f:read("*all"), ("Could not read " .. filename)) - f:close() - opts.filename = filename - return eval(source, opts, ...) - end - local function syntax() - local body_3f = {"when", "with-open", "collect", "icollect", "lambda", "\206\187", "macro", "match", "accumulate"} - local binding_3f = {"collect", "icollect", "each", "for", "let", "with-open", "accumulate"} - local define_3f = {"fn", "lambda", "\206\187", "var", "local", "macro", "macros", "global"} - local out = {} - for k, v in pairs(compiler.scopes.global.specials) do - local metadata = (compiler.metadata[v] or {}) - do end (out)[k] = {["special?"] = true, ["body-form?"] = metadata["fnl/body-form?"], ["binding-form?"] = utils["member?"](k, binding_3f), ["define?"] = utils["member?"](k, define_3f)} - end - for k, v in pairs(compiler.scopes.global.macros) do - out[k] = {["macro?"] = true, ["body-form?"] = utils["member?"](k, body_3f), ["binding-form?"] = utils["member?"](k, binding_3f), ["define?"] = utils["member?"](k, define_3f)} - end - for k, v in pairs(_G) do - local _679_ = type(v) - if (_679_ == "function") then - out[k] = {["global?"] = true, ["function?"] = true} - elseif (_679_ == "table") then - for k2, v2 in pairs(v) do - if (("function" == type(v2)) and (k ~= "_G")) then - out[(k .. "." .. k2)] = {["function?"] = true, ["global?"] = true} - else - end - end - out[k] = {["global?"] = true} - else - end - end - return out - end - local mod = {list = utils.list, ["list?"] = utils["list?"], sym = utils.sym, ["sym?"] = utils["sym?"], sequence = utils.sequence, ["sequence?"] = utils["sequence?"], comment = utils.comment, ["comment?"] = utils["comment?"], varg = utils.varg, ["varg?"] = utils["varg?"], ["sym-char?"] = parser["sym-char?"], parser = parser.parser, compile = compiler.compile, ["compile-string"] = compiler["compile-string"], ["compile-stream"] = compiler["compile-stream"], eval = eval, repl = repl, view = view, dofile = dofile_2a, ["load-code"] = specials["load-code"], doc = specials.doc, metadata = compiler.metadata, traceback = compiler.traceback, version = utils.version, ["runtime-version"] = utils["runtime-version"], path = utils.path, ["macro-path"] = utils["macro-path"], ["macro-loaded"] = specials["macro-loaded"], ["macro-searchers"] = specials["macro-searchers"], ["search-module"] = specials["search-module"], ["make-searcher"] = specials["make-searcher"], searcher = specials["make-searcher"](), syntax = syntax, gensym = compiler.gensym, scope = compiler["make-scope"], mangle = compiler["global-mangling"], unmangle = compiler["global-unmangling"], compile1 = compiler.compile1, ["string-stream"] = parser["string-stream"], granulate = parser.granulate, loadCode = specials["load-code"], make_searcher = specials["make-searcher"], makeSearcher = specials["make-searcher"], searchModule = specials["search-module"], macroPath = utils["macro-path"], macroSearchers = specials["macro-searchers"], macroLoaded = specials["macro-loaded"], compileStream = compiler["compile-stream"], compileString = compiler["compile-string"], stringStream = parser["string-stream"], runtimeVersion = utils["runtime-version"]} - utils["fennel-module"] = mod - do - local builtin_macros = [===[;; These macros are awkward because their definition cannot rely on the any - ;; built-in macros, only special forms. (no when, no icollect, etc) - - (fn copy [t] - (let [out []] - (each [_ v (ipairs t)] (table.insert out v)) - (setmetatable out (getmetatable t)))) - - (fn ->* [val ...] - "Thread-first macro. - Take the first value and splice it into the second form as its first argument. - The value of the second form is spliced into the first arg of the third, etc." - (var x val) - (each [_ e (ipairs [...])] - (let [elt (copy (if (list? e) e (list e)))] - (table.insert elt 2 x) - (set x elt))) - x) - - (fn ->>* [val ...] - "Thread-last macro. - Same as ->, except splices the value into the last position of each form - rather than the first." - (var x val) - (each [_ e (ipairs [...])] - (let [elt (copy (if (list? e) e (list e)))] - (table.insert elt x) - (set x elt))) - x) - - (fn -?>* [val ?e ...] - "Nil-safe thread-first macro. - Same as -> except will short-circuit with nil when it encounters a nil value." - (if (= nil ?e) - val - (let [e (copy ?e) - el (if (list? e) e (list e)) - tmp (gensym)] - (table.insert el 2 tmp) - `(let [,tmp ,val] - (if (not= nil ,tmp) - (-?> ,el ,...) - ,tmp))))) - - (fn -?>>* [val ?e ...] - "Nil-safe thread-last macro. - Same as ->> except will short-circuit with nil when it encounters a nil value." - (if (= nil ?e) - val - (let [e (copy ?e) - el (if (list? e) e (list e)) - tmp (gensym)] - (table.insert el tmp) - `(let [,tmp ,val] - (if (not= ,tmp nil) - (-?>> ,el ,...) - ,tmp))))) - - (fn ?dot [tbl ...] - "Nil-safe table look up. - Same as . (dot), except will short-circuit with nil when it encounters - a nil value in any of subsequent keys." - (let [head (gensym :t) - lookups `(do (var ,head ,tbl) ,head)] - (each [_ k (ipairs [...])] - ;; Kinda gnarly to reassign in place like this, but it emits the best lua. - ;; With this impl, it emits a flat, concise, and readable set of ifs - (table.insert lookups (# lookups) `(if (not= nil ,head) - (set ,head (. ,head ,k))))) - lookups)) - - (fn doto* [val ...] - "Evaluate val and splice it into the first argument of subsequent forms." - (let [name (gensym) - form `(let [,name ,val])] - (each [_ elt (ipairs [...])] - (let [elt (copy (if (list? elt) elt (list elt)))] - (table.insert elt 2 name) - (table.insert form elt))) - (table.insert form name) - form)) - - (fn when* [condition body1 ...] - "Evaluate body for side-effects only when condition is truthy." - (assert body1 "expected body") - `(if ,condition - (do - ,body1 - ,...))) - - (fn with-open* [closable-bindings ...] - "Like `let`, but invokes (v:close) on each binding after evaluating the body. - The body is evaluated inside `xpcall` so that bound values will be closed upon - encountering an error before propagating it." - (let [bodyfn `(fn [] - ,...) - closer `(fn close-handlers# [ok# ...] - (if ok# ... (error ... 0))) - traceback `(. (or package.loaded.fennel debug) :traceback)] - (for [i 1 (length closable-bindings) 2] - (assert (sym? (. closable-bindings i)) - "with-open only allows symbols in bindings") - (table.insert closer 4 `(: ,(. closable-bindings i) :close))) - `(let ,closable-bindings - ,closer - (close-handlers# (_G.xpcall ,bodyfn ,traceback))))) - - (fn extract-into [iter-tbl] - (var (into iter-out found?) (values [] (copy iter-tbl))) - (for [i (length iter-tbl) 2 -1] - (if (= :into (. iter-tbl i)) - (do (assert (not found?) "expected only one :into clause") - (set found? true) - (set into (. iter-tbl (+ i 1))) - (table.remove iter-out i) - (table.remove iter-out i)))) - (assert (or (not found?) (sym? into) (table? into) (list? into)) - "expected table, function call, or symbol in :into clause") - (values into iter-out)) - - (fn collect* [iter-tbl key-expr value-expr ...] - "Return a table made by running an iterator and evaluating an expression that - returns key-value pairs to be inserted sequentially into the table. This can - be thought of as a table comprehension. The body should provide two expressions - (used as key and value) or nil, which causes it to be omitted. - - For example, - (collect [k v (pairs {:apple \"red\" :orange \"orange\"})] - (values v k)) - returns - {:red \"apple\" :orange \"orange\"} - - Supports an :into clause after the iterator to put results in an existing table. - Supports early termination with an :until clause." - (assert (and (sequence? iter-tbl) (>= (length iter-tbl) 2)) - "expected iterator binding table") - (assert (not= nil key-expr) "expected key and value expression") - (assert (= nil ...) - "expected 1 or 2 body expressions; wrap multiple expressions with do") - (let [kv-expr (if (= nil value-expr) key-expr `(values ,key-expr ,value-expr)) - (into iter) (extract-into iter-tbl)] - `(let [tbl# ,into] - (each ,iter - (match ,kv-expr - (k# v#) (tset tbl# k# v#))) - tbl#))) - - (fn icollect* [iter-tbl value-expr ...] - "Return a sequential table made by running an iterator and evaluating an - expression that returns values to be inserted sequentially into the table. - This can be thought of as a table comprehension. If the body evaluates to nil - that element is omitted. - - For example, - (icollect [_ v (ipairs [1 2 3 4 5])] - (when (not= v 3) - (* v v))) - returns - [1 4 16 25] - - Supports an :into clause after the iterator to put results in an existing table. - Supports early termination with an :until clause." - (assert (and (sequence? iter-tbl) (>= (length iter-tbl) 2)) - "expected iterator binding table") - (assert (not= nil value-expr) "expected table value expression") - (assert (= nil ...) - "expected exactly one body expression. Wrap multiple expressions in do") - (let [(into iter) (extract-into iter-tbl)] - `(let [tbl# ,into] - ;; believe it or not, using a var here has a pretty good performance - ;; boost: https://p.hagelb.org/icollect-performance.html - (var i# (length tbl#)) - (each ,iter - (let [val# ,value-expr] - (when (not= nil val#) - (set i# (+ i# 1)) - (tset tbl# i# val#)))) - tbl#))) - - (fn accumulate* [iter-tbl body ...] - "Accumulation macro. - - It takes a binding table and an expression as its arguments. In the binding - table, the first form starts out bound to the second value, which is an initial - accumulator. The rest are an iterator binding table in the format `each` takes. - - It runs through the iterator in each step of which the given expression is - evaluated, and the accumulator is set to the value of the expression. It - eventually returns the final value of the accumulator. - - For example, - (accumulate [total 0 - _ n (pairs {:apple 2 :orange 3})] - (+ total n)) - returns 5" - (assert (and (sequence? iter-tbl) (>= (length iter-tbl) 4)) - "expected initial value and iterator binding table") - (assert (not= nil body) "expected body expression") - (assert (= nil ...) - "expected exactly one body expression. Wrap multiple expressions with do") - (let [accum-var (. iter-tbl 1) - accum-init (. iter-tbl 2)] - `(do (var ,accum-var ,accum-init) - (each ,[(unpack iter-tbl 3)] - (set ,accum-var ,body)) - ,(if (list? accum-var) - (list (sym :values) (unpack accum-var)) - accum-var)))) - - (fn double-eval-safe? [x type] - (or (= :number type) (= :string type) (= :boolean type) - (and (sym? x) (not (multi-sym? x))))) - - (fn partial* [f ...] - "Return a function with all arguments partially applied to f." - (assert f "expected a function to partially apply") - (let [bindings [] - args []] - (each [_ arg (ipairs [...])] - (if (double-eval-safe? arg (type arg)) - (table.insert args arg) - (let [name (gensym)] - (table.insert bindings name) - (table.insert bindings arg) - (table.insert args name)))) - (let [body (list f (unpack args))] - (table.insert body _VARARG) - ;; only use the extra let if we need double-eval protection - (if (= 0 (length bindings)) - `(fn [,_VARARG] ,body) - `(let ,bindings - (fn [,_VARARG] ,body)))))) - - (fn pick-args* [n f] - "Create a function of arity n that applies its arguments to f. - - For example, - (pick-args 2 func) - expands to - (fn [_0_ _1_] (func _0_ _1_))" - (if (and _G.io _G.io.stderr) - (_G.io.stderr:write - "-- WARNING: pick-args is deprecated and will be removed in the future.\n")) - (assert (and (= (type n) :number) (= n (math.floor n)) (>= n 0)) - (.. "Expected n to be an integer literal >= 0, got " (tostring n))) - (let [bindings []] - (for [i 1 n] - (tset bindings i (gensym))) - `(fn ,bindings - (,f ,(unpack bindings))))) - - (fn pick-values* [n ...] - "Evaluate to exactly n values. - - For example, - (pick-values 2 ...) - expands to - (let [(_0_ _1_) ...] - (values _0_ _1_))" - (assert (and (= :number (type n)) (>= n 0) (= n (math.floor n))) - (.. "Expected n to be an integer >= 0, got " (tostring n))) - (let [let-syms (list) - let-values (if (= 1 (select "#" ...)) ... `(values ,...))] - (for [i 1 n] - (table.insert let-syms (gensym))) - (if (= n 0) `(values) - `(let [,let-syms ,let-values] - (values ,(unpack let-syms)))))) - - (fn lambda* [...] - "Function literal with nil-checked arguments. - Like `fn`, but will throw an exception if a declared argument is passed in as - nil, unless that argument's name begins with a question mark." - (let [args [...] - has-internal-name? (sym? (. args 1)) - arglist (if has-internal-name? (. args 2) (. args 1)) - docstring-position (if has-internal-name? 3 2) - has-docstring? (and (> (length args) docstring-position) - (= :string (type (. args docstring-position)))) - arity-check-position (- 4 (if has-internal-name? 0 1) - (if has-docstring? 0 1)) - empty-body? (< (length args) arity-check-position)] - (fn check! [a] - (if (table? a) - (each [_ a (pairs a)] - (check! a)) - (let [as (tostring a)] - (and (not (as:match "^?")) (not= as "&") (not= as "_") - (not= as "...") (not= as "&as"))) - (table.insert args arity-check-position - `(_G.assert (not= nil ,a) - ,(: "Missing argument %s on %s:%s" :format - (tostring a) - (or a.filename :unknown) - (or a.line "?")))))) - - (assert (= :table (type arglist)) "expected arg list") - (each [_ a (ipairs arglist)] - (check! a)) - (if empty-body? - (table.insert args (sym :nil))) - `(fn ,(unpack args)))) - - (fn macro* [name ...] - "Define a single macro." - (assert (sym? name) "expected symbol for macro name") - (local args [...]) - `(macros {,(tostring name) (fn ,(unpack args))})) - - (fn macrodebug* [form return?] - "Print the resulting form after performing macroexpansion. - With a second argument, returns expanded form as a string instead of printing." - (let [handle (if return? `do `print)] - `(,handle ,(view (macroexpand form _SCOPE))))) - - (fn import-macros* [binding1 module-name1 ...] - "Bind a table of macros from each macro module according to a binding form. - Each binding form can be either a symbol or a k/v destructuring table. - Example: - (import-macros mymacros :my-macros ; bind to symbol - {:macro1 alias : macro2} :proj.macros) ; import by name" - (assert (and binding1 module-name1 (= 0 (% (select "#" ...) 2))) - "expected even number of binding/modulename pairs") - (for [i 1 (select "#" binding1 module-name1 ...) 2] - ;; delegate the actual loading of the macros to the require-macros - ;; special which already knows how to set up the compiler env and stuff. - ;; this is weird because require-macros is deprecated but it works. - (let [(binding modname) (select i binding1 module-name1 ...) - scope (get-scope) - macros* (_SPECIALS.require-macros `(import-macros ,modname) - scope {} binding1)] - (if (sym? binding) - ;; bind whole table of macros to table bound to symbol - (tset scope.macros (. binding 1) macros*) - ;; 1-level table destructuring for importing individual macros - (table? binding) - (each [macro-name [import-key] (pairs binding)] - (assert (= :function (type (. macros* macro-name))) - (.. "macro " macro-name " not found in module " - (tostring modname))) - (tset scope.macros import-key (. macros* macro-name)))))) - nil) - - ;;; Pattern matching - - (fn match-values [vals pattern unifications match-pattern] - (let [condition `(and) - bindings []] - (each [i pat (ipairs pattern)] - (let [(subcondition subbindings) (match-pattern [(. vals i)] pat - unifications)] - (table.insert condition subcondition) - (each [_ b (ipairs subbindings)] - (table.insert bindings b)))) - (values condition bindings))) - - (fn match-table [val pattern unifications match-pattern] - (let [condition `(and (= (_G.type ,val) :table)) - bindings []] - (each [k pat (pairs pattern)] - (if (= pat `&) - (let [rest-pat (. pattern (+ k 1)) - rest-val `(select ,k ((or table.unpack _G.unpack) ,val)) - subcondition (match-table `(pick-values 1 ,rest-val) - rest-pat unifications match-pattern)] - (if (not (sym? rest-pat)) - (table.insert condition subcondition)) - (assert (= nil (. pattern (+ k 2))) - "expected & rest argument before last parameter") - (table.insert bindings rest-pat) - (table.insert bindings [rest-val])) - (= k `&as) - (do - (table.insert bindings pat) - (table.insert bindings val)) - (and (= :number (type k)) (= `&as pat)) - (do - (assert (= nil (. pattern (+ k 2))) - "expected &as argument before last parameter") - (table.insert bindings (. pattern (+ k 1))) - (table.insert bindings val)) - ;; don't process the pattern right after &/&as; already got it - (or (not= :number (type k)) (and (not= `&as (. pattern (- k 1))) - (not= `& (. pattern (- k 1))))) - (let [subval `(. ,val ,k) - (subcondition subbindings) (match-pattern [subval] pat - unifications)] - (table.insert condition subcondition) - (each [_ b (ipairs subbindings)] - (table.insert bindings b))))) - (values condition bindings))) - - (fn match-pattern [vals pattern unifications] - "Take the AST of values and a single pattern and returns a condition - to determine if it matches as well as a list of bindings to - introduce for the duration of the body if it does match." - ;; we have to assume we're matching against multiple values here until we - ;; know we're either in a multi-valued clause (in which case we know the # - ;; of vals) or we're not, in which case we only care about the first one. - (let [[val] vals] - (if (or (and (sym? pattern) ; unification with outer locals (or nil) - (not= "_" (tostring pattern)) ; never unify _ - (or (in-scope? pattern) (= :nil (tostring pattern)))) - (and (multi-sym? pattern) (in-scope? (. (multi-sym? pattern) 1)))) - (values `(= ,val ,pattern) []) - ;; unify a local we've seen already - (and (sym? pattern) (. unifications (tostring pattern))) - (values `(= ,(. unifications (tostring pattern)) ,val) []) - ;; bind a fresh local - (sym? pattern) - (let [wildcard? (: (tostring pattern) :find "^_")] - (if (not wildcard?) (tset unifications (tostring pattern) val)) - (values (if (or wildcard? (string.find (tostring pattern) "^?")) true - `(not= ,(sym :nil) ,val)) [pattern val])) - ;; guard clause - (and (list? pattern) (= (. pattern 2) `?)) - (let [(pcondition bindings) (match-pattern vals (. pattern 1) - unifications) - condition `(and ,(unpack pattern 3))] - (values `(and ,pcondition - (let ,bindings - ,condition)) bindings)) - ;; multi-valued patterns (represented as lists) - (list? pattern) - (match-values vals pattern unifications match-pattern) - ;; table patterns - (= (type pattern) :table) - (match-table val pattern unifications match-pattern) - ;; literal value - (values `(= ,val ,pattern) [])))) - - (fn match-condition [vals clauses] - "Construct the actual `if` AST for the given match values and clauses." - (if (not= 0 (% (length clauses) 2)) ; treat odd final clause as default - (table.insert clauses (length clauses) (sym "_"))) - (let [out `(if)] - (for [i 1 (length clauses) 2] - (let [pattern (. clauses i) - body (. clauses (+ i 1)) - (condition bindings) (match-pattern vals pattern {})] - (table.insert out condition) - (table.insert out `(let ,bindings - ,body)))) - out)) - - (fn match-val-syms [clauses] - "How many multi-valued clauses are there? return a list of that many gensyms." - (let [syms (list (gensym))] - (for [i 1 (length clauses) 2] - (let [clause (if (and (list? (. clauses i)) (= `? (. clauses i 2))) - (. clauses i 1) - (. clauses i))] - (if (list? clause) - (each [valnum (ipairs clause)] - (if (not (. syms valnum)) - (tset syms valnum (gensym))))))) - syms)) - - (fn match* [val ...] - ;; Old implementation of match macro, which doesn't directly support - ;; `where' and `or'. New syntax is implemented in `match-where', - ;; which simply generates old syntax and feeds it to `match*'. - (let [clauses [...] - vals (match-val-syms clauses)] - ;; protect against multiple evaluation of the value, bind against as - ;; many values as we ever match against in the clauses. - (list `let [vals val] (match-condition vals clauses)))) - - ;; Construction of old match syntax from new syntax - - (fn partition-2 [seq] - ;; Partition `seq` by 2. - ;; If `seq` has odd amount of elements, the last one is dropped. - ;; - ;; Input: [1 2 3 4 5] - ;; Output: [[1 2] [3 4]] - (let [firsts [] - seconds [] - res []] - (for [i 1 (length seq) 2] - (let [first (. seq i) - second (. seq (+ i 1))] - (table.insert firsts (if (not= nil first) first `nil)) - (table.insert seconds (if (not= nil second) second `nil)))) - (each [i v1 (ipairs firsts)] - (let [v2 (. seconds i)] - (if (not= nil v2) - (table.insert res [v1 v2])))) - res)) - - (fn transform-or [[_ & pats] guards] - ;; Transforms `(or pat pats*)` lists into match `guard` patterns. - ;; - ;; (or pat1 pat2), guard => [(pat1 ? guard) (pat2 ? guard)] - (let [res []] - (each [_ pat (ipairs pats)] - (table.insert res (list pat `? (unpack guards)))) - res)) - - (fn transform-cond [cond] - ;; Transforms `where` cond into sequence of `match` guards. - ;; - ;; pat => [pat] - ;; (where pat guard) => [(pat ? guard)] - ;; (where (or pat1 pat2) guard) => [(pat1 ? guard) (pat2 ? guard)] - (if (and (list? cond) (= (. cond 1) `where)) - (let [second (. cond 2)] - (if (and (list? second) (= (. second 1) `or)) - (transform-or second [(unpack cond 3)]) - :else - [(list second `? (unpack cond 3))])) - :else - [cond])) - - (fn match-where [val ...] - "Perform pattern matching on val. See reference for details. - - Syntax: - - (match data-expression - pattern body - (where pattern guard guards*) body - (where (or pattern patterns*) guard guards*) body)" - (assert (= 0 (math.fmod (select :# ...) 2)) - "expected even number of pattern/body pairs") - (let [conds-bodies (partition-2 [...]) - match-body []] - (each [_ [cond body] (ipairs conds-bodies)] - (each [_ cond (ipairs (transform-cond cond))] - (table.insert match-body cond) - (table.insert match-body body))) - (match* val (unpack match-body)))) - - (fn match-try-step [expr else pattern body ...] - (if (= nil pattern body) - expr - ;; unlike regular match, we can't know how many values the value - ;; might evaluate to, so we have to capture them all in ... via IIFE - ;; to avoid double-evaluation. - `((fn [...] - (match ... - ,pattern ,(match-try-step body else ...) - ,(unpack else))) - ,expr))) - - (fn match-try* [expr pattern body ...] - "Perform chained pattern matching for a sequence of steps which might fail. - - The values from the initial expression are matched against the first pattern. - If they match, the first body is evaluated and its values are matched against - the second pattern, etc. - - If there is a (catch pat1 body1 pat2 body2 ...) form at the end, any mismatch - from the steps will be tried against these patterns in sequence as a fallback - just like a normal match. If there is no catch, the mismatched values will be - returned as the value of the entire expression." - (let [clauses [pattern body ...] - last (. clauses (length clauses)) - catch (if (= `catch (and (= :table (type last)) (. last 1))) - (let [[_ & e] (table.remove clauses)] e) ; remove `catch sym - [`_# `...])] - (assert (= 0 (math.fmod (length clauses) 2)) - "expected every pattern to have a body") - (assert (= 0 (math.fmod (length catch) 2)) - "expected every catch pattern to have a body") - (match-try-step expr catch (unpack clauses)))) - - {:-> ->* - :->> ->>* - :-?> -?>* - :-?>> -?>>* - :?. ?dot - :doto doto* - :when when* - :with-open with-open* - :collect collect* - :icollect icollect* - :accumulate accumulate* - :partial partial* - :lambda lambda* - :pick-args pick-args* - :pick-values pick-values* - :macro macro* - :macrodebug macrodebug* - :import-macros import-macros* - :match match-where - :match-try match-try*} - ]===] - local module_name = "fennel.macros" - local _ - local function _682_() - return mod - end - package.preload[module_name] = _682_ - _ = nil - local env - do - local _683_ = specials["make-compiler-env"](nil, compiler.scopes.compiler, {}) - do end (_683_)["utils"] = utils - _683_["fennel"] = mod - env = _683_ - end - local built_ins = eval(builtin_macros, {env = env, scope = compiler.scopes.compiler, allowedGlobals = false, useMetadata = true, filename = "src/fennel/macros.fnl", moduleName = module_name}) - for k, v in pairs(built_ins) do - compiler.scopes.global.macros[k] = v - end - compiler.scopes.global.macros["\206\187"] = compiler.scopes.global.macros.lambda - package.preload[module_name] = nil - end - return mod -end -fennel = require("fennel") -local unpack = (table.unpack or _G.unpack) -local help = "\nUsage: fennel [FLAG] [FILE]\n\nRun fennel, a lisp programming language for the Lua runtime.\n\n --repl : Command to launch an interactive repl session\n --compile FILES (-c) : Command to AOT compile files, writing Lua to stdout\n --eval SOURCE (-e) : Command to evaluate source code and print the result\n\n --no-searcher : Skip installing package.searchers entry\n --indent VAL : Indent compiler output with VAL\n --add-package-path PATH : Add PATH to package.path for finding Lua modules\n --add-fennel-path PATH : Add PATH to fennel.path for finding Fennel modules\n --add-macro-path PATH : Add PATH to fennel.macro-path for macro modules\n --globals G1[,G2...] : Allow these globals in addition to standard ones\n --globals-only G1[,G2] : Same as above, but exclude standard ones\n --require-as-include : Inline required modules in the output\n --skip-include M1[,M2] : Omit certain modules from output when included\n --use-bit-lib : Use LuaJITs bit library instead of operators\n --metadata : Enable function metadata, even in compiled output\n --no-metadata : Disable function metadata, even in REPL\n --correlate : Make Lua output line numbers match Fennel input\n --load FILE (-l) : Load the specified FILE before executing the command\n --lua LUA_EXE : Run in a child process with LUA_EXE\n --no-fennelrc : Skip loading ~/.fennelrc when launching repl\n --raw-errors : Disable friendly compile error reporting\n --plugin FILE : Activate the compiler plugin in FILE\n --compile-binary FILE\n OUT LUA_LIB LUA_DIR : Compile FILE to standalone binary OUT\n --compile-binary --help : Display further help for compiling binaries\n --no-compiler-sandbox : Do not limit compiler environment to minimal sandbox\n\n --help (-h) : Display this text\n --version (-v) : Show version\n\nGlobals are not checked when doing AOT (ahead-of-time) compilation unless\nthe --globals-only or --globals flag is provided. Use --globals \"*\" to disable\nstrict globals checking in other contexts.\n\nMetadata is typically considered a development feature and is not recommended\nfor production. It is used for docstrings and enabled by default in the REPL.\n\nWhen not given a command, runs the file given as the first argument.\nWhen given neither command nor file, launches a repl.\n\nIf ~/.fennelrc exists, it will be loaded before launching a repl." -local options = {plugins = {}} -local function dosafely(f, ...) - local args = {...} - local _684_, _685_ = nil, nil - local function _686_() - return f(unpack(args)) - end - _684_, _685_ = xpcall(_686_, fennel.traceback) - if ((_684_ == true) and (nil ~= _685_)) then - local val = _685_ - return val - elseif (true and (nil ~= _685_)) then - local _ = _684_ - local msg = _685_ - do end (io.stderr):write((msg .. "\n")) - return os.exit(1) - else - return nil - end -end -local function allow_globals(global_names, globals) - if (global_names == "*") then - options.allowedGlobals = false - return nil - else - do - local tbl_14_auto = {} - local i_15_auto = #tbl_14_auto - for g in global_names:gmatch("([^,]+),?") do - local val_16_auto = g - if (nil ~= val_16_auto) then - i_15_auto = (i_15_auto + 1) - do end (tbl_14_auto)[i_15_auto] = val_16_auto - else - end - end - options.allowedGlobals = tbl_14_auto - end - for global_name in pairs(globals) do - table.insert(options.allowedGlobals, global_name) - end - return nil - end -end -local function handle_load(i) - local file = table.remove(arg, (i + 1)) - dosafely(fennel.dofile, file, options) - return table.remove(arg, i) -end -local function handle_lua(i) - table.remove(arg, i) - local tgt_lua = table.remove(arg, i) - local cmd = {string.format("%s %s", tgt_lua, arg[0])} - for i0 = 1, #arg do - table.insert(cmd, string.format("%q", arg[i0])) - end - local ok = os.execute(table.concat(cmd, " ")) - local _690_ - if ok then - _690_ = 0 - else - _690_ = 1 - end - return os.exit(_690_, true) -end -for i = #arg, 1, -1 do - local _692_ = arg[i] - if (_692_ == "--lua") then - handle_lua(i) - else - end -end -for i = #arg, 1, -1 do - local _694_ = arg[i] - if (_694_ == "--no-searcher") then - options["no-searcher"] = true - table.remove(arg, i) - elseif (_694_ == "--indent") then - options.indent = table.remove(arg, (i + 1)) - if (options.indent == "false") then - options.indent = false - else - end - table.remove(arg, i) - elseif (_694_ == "--add-package-path") then - local entry = table.remove(arg, (i + 1)) - package.path = (entry .. ";" .. package.path) - table.remove(arg, i) - elseif (_694_ == "--add-fennel-path") then - local entry = table.remove(arg, (i + 1)) - fennel.path = (entry .. ";" .. fennel.path) - table.remove(arg, i) - elseif (_694_ == "--add-macro-path") then - local entry = table.remove(arg, (i + 1)) - fennel["macro-path"] = (entry .. ";" .. fennel["macro-path"]) - table.remove(arg, i) - elseif (_694_ == "--load") then - handle_load(i) - elseif (_694_ == "-l") then - handle_load(i) - elseif (_694_ == "--no-fennelrc") then - options.fennelrc = false - table.remove(arg, i) - elseif (_694_ == "--correlate") then - options.correlate = true - table.remove(arg, i) - elseif (_694_ == "--check-unused-locals") then - options.checkUnusedLocals = true - table.remove(arg, i) - elseif (_694_ == "--globals") then - allow_globals(table.remove(arg, (i + 1)), _G) - table.remove(arg, i) - elseif (_694_ == "--globals-only") then - allow_globals(table.remove(arg, (i + 1)), {}) - table.remove(arg, i) - elseif (_694_ == "--require-as-include") then - options.requireAsInclude = true - table.remove(arg, i) - elseif (_694_ == "--skip-include") then - local skip_names = table.remove(arg, (i + 1)) - local skip - do - local tbl_14_auto = {} - local i_15_auto = #tbl_14_auto - for m in skip_names:gmatch("([^,]+)") do - local val_16_auto = m - if (nil ~= val_16_auto) then - i_15_auto = (i_15_auto + 1) - do end (tbl_14_auto)[i_15_auto] = val_16_auto - else - end - end - skip = tbl_14_auto - end - options.skipInclude = skip - table.remove(arg, i) - elseif (_694_ == "--use-bit-lib") then - options.useBitLib = true - table.remove(arg, i) - elseif (_694_ == "--metadata") then - options.useMetadata = true - table.remove(arg, i) - elseif (_694_ == "--no-metadata") then - options.useMetadata = false - table.remove(arg, i) - elseif (_694_ == "--no-compiler-sandbox") then - options["compiler-env"] = _G - table.remove(arg, i) - elseif (_694_ == "--raw-errors") then - options.unfriendly = true - table.remove(arg, i) - elseif (_694_ == "--plugin") then - local opts = {env = "_COMPILER", useMetadata = true, ["compiler-env"] = _G} - local plugin = fennel.dofile(table.remove(arg, (i + 1)), opts) - table.insert(options.plugins, 1, plugin) - table.remove(arg, i) - else - end -end -local searcher_opts = {} -if not options["no-searcher"] then - for k, v in pairs(options) do - searcher_opts[k] = v - end - table.insert((package.loaders or package.searchers), fennel["make-searcher"](searcher_opts)) -else -end -local function load_initfile() - local home = (os.getenv("HOME") or "/") - local xdg_config_home = (os.getenv("XDG_CONFIG_HOME") or (home .. "/.config")) - local xdg_initfile = (xdg_config_home .. "/fennel/fennelrc") - local home_initfile = (home .. "/.fennelrc") - local init = io.open(xdg_initfile, "rb") - local init_filename - if init then - init_filename = xdg_initfile - else - init_filename = home_initfile - end - local init0 = (init or io.open(home_initfile, "rb")) - if init0 then - init0:close() - return dosafely(fennel.dofile, init_filename, options, options, fennel) - else - return nil - end -end -local function repl() - local readline_3f = (("dumb" ~= os.getenv("TERM")) and pcall(require, "readline")) - searcher_opts.useMetadata = (false ~= options.useMetadata) - if (false ~= options.fennelrc) then - load_initfile() - else - end - print(("Welcome to " .. fennel["runtime-version"]() .. "!")) - print("Use ,help to see available commands.") - if (not readline_3f and ("dumb" ~= os.getenv("TERM"))) then - print("Try installing readline via luarocks for a better repl experience.") - else - end - return fennel.repl(options) -end -local function eval(form) - local _703_ - if (form == "-") then - _703_ = (io.stdin):read("*a") - else - _703_ = form - end - return print(dosafely(fennel.eval, _703_, options)) -end -local function compile(files) - for _, filename in ipairs(files) do - options.filename = filename - local f - if (filename == "-") then - f = io.stdin - else - f = assert(io.open(filename, "rb")) - end - do - local _706_, _707_ = nil, nil - local function _708_() - return fennel["compile-string"](f:read("*a"), options) - end - _706_, _707_ = xpcall(_708_, fennel.traceback) - if ((_706_ == true) and (nil ~= _707_)) then - local val = _707_ - print(val) - elseif (true and (nil ~= _707_)) then - local _0 = _706_ - local msg = _707_ - do end (io.stderr):write((msg .. "\n")) - os.exit(1) - else - end - end - f:close() - end - return nil -end -local _710_ = arg -local function _711_(...) - return (0 == #arg) -end -if ((_G.type(_710_) == "table") and _711_(...)) then - return repl() -elseif ((_G.type(_710_) == "table") and ((_710_)[1] == "--repl")) then - return repl() -elseif ((_G.type(_710_) == "table") and ((_710_)[1] == "--compile")) then - local files = {select(2, (table.unpack or _G.unpack)(_710_))} - return compile(files) -elseif ((_G.type(_710_) == "table") and ((_710_)[1] == "-c")) then - local files = {select(2, (table.unpack or _G.unpack)(_710_))} - return compile(files) -elseif ((_G.type(_710_) == "table") and ((_710_)[1] == "--compile-binary") and (nil ~= (_710_)[2]) and (nil ~= (_710_)[3]) and (nil ~= (_710_)[4]) and (nil ~= (_710_)[5])) then - local filename = (_710_)[2] - local out = (_710_)[3] - local static_lua = (_710_)[4] - local lua_include_dir = (_710_)[5] - local args = {select(6, (table.unpack or _G.unpack)(_710_))} - local bin = require("fennel.binary") - options.filename = filename - options.requireAsInclude = true - return bin.compile(filename, out, static_lua, lua_include_dir, options, args) -elseif ((_G.type(_710_) == "table") and ((_710_)[1] == "--compile-binary")) then - return print((require("fennel.binary")).help) -elseif ((_G.type(_710_) == "table") and ((_710_)[1] == "--eval") and (nil ~= (_710_)[2])) then - local form = (_710_)[2] - return eval(form) -elseif ((_G.type(_710_) == "table") and ((_710_)[1] == "-e") and (nil ~= (_710_)[2])) then - local form = (_710_)[2] - return eval(form) -else - local function _739_(...) - local a = (_710_)[1] - return ((a == "-v") or (a == "--version")) - end - if (((_G.type(_710_) == "table") and (nil ~= (_710_)[1])) and _739_(...)) then - local a = (_710_)[1] - return print(fennel["runtime-version"]()) - elseif ((_G.type(_710_) == "table") and ((_710_)[1] == "--help")) then - return print(help) - elseif ((_G.type(_710_) == "table") and ((_710_)[1] == "-h")) then - return print(help) - elseif ((_G.type(_710_) == "table") and ((_710_)[1] == "-")) then - local args = {select(2, (table.unpack or _G.unpack)(_710_))} - return dosafely(fennel.eval, (io.stdin):read("*a")) - elseif ((_G.type(_710_) == "table") and (nil ~= (_710_)[1])) then - local filename = (_710_)[1] - local args = {select(2, (table.unpack or _G.unpack)(_710_))} - arg[-2] = arg[-1] - arg[-1] = arg[0] - arg[0] = table.remove(arg, 1) - return dosafely(fennel.dofile, filename, options, unpack(args)) - else - return nil - end -end diff --git a/src/forem-nvim/api.ts b/src/forem-nvim/api.ts new file mode 100644 index 0000000..8103bb1 --- /dev/null +++ b/src/forem-nvim/api.ts @@ -0,0 +1,142 @@ +import * as curl from "plenary.curl"; +import * as Feed from "./feed"; +import * as job from "plenary.job"; +import * as notify from "./notify"; +import * as article from "./article"; + +export const key = () => vim.env.FOREM_API_KEY; + +type ResponseCallback = (response: T) => any; + +const baseUrl = "https://dev.to/api"; + +const handleAsyncError = (response: any) => { + notify.error(`Error: ${response.body.error}`); +}; + +export const handleError = (response: any, onSuccess: ResponseCallback) => { + const startStatus = string.sub(response.status, 1, 2); + + return startStatus === "20" + ? onSuccess(response.body) + : notify.error(`Error: ${response.body.error}`); +}; + +const request = ( + requestFunction: (params: any) => any, + endpoint: string, + options: any, +) => { + const parameters = vim.tbl_extend( + "force", + { + url: baseUrl + endpoint, + headers: { + "api-key": key(), + content_type: "application/json", + accept: "application/vnd.forem.api-v1+json", + }, + }, + options, + ); + + const response = requestFunction(parameters); + + if (response.body) { + return vim.tbl_extend("force", response, { + body: vim.fn.json_decode(response.body), + }); + } + + return response; +}; + +const requestAsync = ( + method: string, + endpoint: string, + options: any, + onSuccess: ResponseCallback, + onError?: ResponseCallback, +) => + job + .new({ + command: "curl", + args: [ + "-X", + method, + "-H", + "Content-Type: application/json", + "-H", + "Accept: application/vnd.forem.api-v1+json", + "-H", + `api-key: ${key()}`, + "-d", + vim.fn.json_encode(options), + baseUrl + endpoint, + ], + on_exit: (job: Job, code: number) => { + vim.schedule(() => { + const result = job.result().join("\n"); + const response = vim.fn.json_decode(result); + + if (code === 0) { + onSuccess(response); + return; + } + + handleAsyncError(response); + if (onError) { + onError(response); + } + }); + }, + }) + .start(); + +const get = ( + endpoint: string, + onSuccess: ResponseCallback, + onError?: ResponseCallback, +) => requestAsync("GET", endpoint, {}, onSuccess, onError); + +const put = (endpoint: string, body: any) => + request(curl.put, endpoint, { body }); + +const post = (endpoint: string, body: any) => + request(curl.post, endpoint, { body }); + +export const myArticles = ( + onSuccess: ResponseCallback, + onError?: ResponseCallback, +) => get("/articles/me/all", onSuccess, onError); + +export const saveArticle = (id: number, content: string) => + put( + `/articles/${id}`, + vim.fn.json_encode({ article: { body_markdown: content } }), + ); + +export const newArticle = (title: string) => + post( + "/articles", + vim.fn.json_encode({ + article: { body_markdown: article.getTemplate(title) }, + }), + ); + +export const feed = ( + onSuccess: ResponseCallback, + onError?: ResponseCallback, +) => get("/articles", onSuccess, onError); + +export const getArticle = ( + id: number, + onSuccess: ResponseCallback, + onError?: ResponseCallback, +) => get(`/articles/${id}`, onSuccess, onError); + +export const getArticleByPath = ( + path: string, + onSuccess: ResponseCallback, + onError?: ResponseCallback, +) => get(`/articles/${path}`, onSuccess, onError); diff --git a/src/forem-nvim/article.ts b/src/forem-nvim/article.ts new file mode 100644 index 0000000..d4c1cf8 --- /dev/null +++ b/src/forem-nvim/article.ts @@ -0,0 +1,36 @@ +export type Article = { + id: number; + type_of: string; + title: string; + slug: string; + description: string; + url: string; + body_markdown: string | null; + user: Author; + reading_time_minutes: number; + tags: string[]; + positive_reactions_count: number; + comments_count: number; + readable_publish_date: string; + published_at: string | null; +}; + +export type Author = { + name: string; + username: string; +}; + +export const getBodyLines = (article: Article): string[] => + vim.split(article.body_markdown || "", "\n") + +export const getTemplate = (title: string) => + `--- +title: ${title} +published: false +description: +tags: +# cover_image: https://direct_url_to_image.jpg +# Use a ratio of 100:42 for best results. +--- + +`; diff --git a/src/forem-nvim/buffer.ts b/src/forem-nvim/buffer.ts new file mode 100644 index 0000000..f208441 --- /dev/null +++ b/src/forem-nvim/buffer.ts @@ -0,0 +1,59 @@ +import * as Article from "./article"; +import { setLocals, getOption } from "./util"; + +export const setBasicOptions = () => { + setLocals([ + ["filetype", "markdown"], + ["modified", false], + ]); +}; + +export const write = (buffer: number, lines: string[], offset?: number) => { + const modifiable = getOption(vim.opt_local.modifiable); + + vim.opt_local.modifiable = true; + vim.api.nvim_buf_set_lines(buffer, offset || 0, -1, false, lines); + vim.opt_local.modifiable = modifiable; +}; + +export const getContent = () => { + const buffer = vim.api.nvim_get_current_buf(); + + const lines: string[] = vim.api.nvim_buf_get_lines(buffer, 0, -1, true); + + return { content: lines.join("\n"), bufnr: buffer }; +}; + +export const openMyArticle = (article: Article.Article) => { + vim.cmd(`:edit forem://my-article/${article.id}`); + + const buffer = vim.api.nvim_get_current_buf(); + + write(buffer, Article.getBodyLines(article)); + setBasicOptions(); + setLocals([ + ["buftype", "acwrite"], + ["swapfile", false], + ]); +}; + +export const loadArticle = (article: Article.Article) => { + vim.cmd(`:edit forem://article/${article.title}`); + setLocals([ + ["linebreak", true], + ["textwidth", 80], + ]); + + const buffer = vim.api.nvim_get_current_buf(); + const body = Article.getBodyLines(article); + + write(buffer, body); + + setBasicOptions(); + setLocals([ + ["modifiable", false], + ["spell", false], + ["buftype", "nowrite"], + ["swapfile", false], + ]); +}; diff --git a/src/forem-nvim/feed.ts b/src/forem-nvim/feed.ts new file mode 100644 index 0000000..c1f3cae --- /dev/null +++ b/src/forem-nvim/feed.ts @@ -0,0 +1,163 @@ +import * as api from "./api"; +import * as buffer from "./buffer"; +import * as notify from "./notify"; +import * as util from "./util"; +import { setLocals } from "./util"; + +export type FeedArticle = { + id: number; + title: string; + url: string; + description: string; + user: { + name: string; + username: string; + }; + reading_time_minutes: number; + tag_list: string[]; + positive_reactions_count: number; + comments_count: number; + readable_publish_date: string; +}; + +export const open = () => vim.cmd("edit forem://articles/feed"); + +const setBasicOptions = () => { + buffer.setBasicOptions(); + setLocals([ + ["modifiable", false], + ["spell", false], + ["buftype", "nowrite"], + ["swapfile", false], + ]); +}; + +const seekTitle = ( + line: number, + getNextLine: (line: number) => number, + count: number, +): string | null => { + const lineContent = vim.fn.getline(line); + const [title] = string.match(lineContent, " ## (.+)", 1); + + if (title !== undefined) { + return title; + } + + if (count > 1000) { + notify.error("Could not find the title of the article"); + return null; + } + + return seekTitle(getNextLine(line), getNextLine, count + 1); +}; + +export const openArticle = (location: "buffer" | "browser") => { + const title = getCardTitle(vim.fn.line(".")); + + if (!title) { + notify.error("Could not find article data. Please reopen the feed."); + return; + } + + const articleData = foremFeedArticles?.get(title); + + if (!articleData) { + notify.error("Could not find article data. Please reload the feed."); + return; + } + + if (location === "browser") { + util.openUrlOnBrowser(articleData.url); + } else { + api.getArticle(articleData.id, buffer.loadArticle); + } +}; + +const getCardTitle = (line: number): string | null => { + const content = vim.fn.getline(line); + + const [isInsideOfCard] = string.match(content, "^[ |🭽|▏|🭼]", 1); + + if (!isInsideOfCard) { + return null; + } + + const [isUpperBorder] = string.match(content, "🭽", 1); + + const getNextLine = + isUpperBorder !== undefined + ? (line: number) => line + 1 + : (line: number) => line - 1; + + return seekTitle(line, getNextLine, 0); +}; + +export const load = () => { + setBasicOptions(); + const bufnr: number = vim.api.nvim_get_current_buf(); + buffer.write(bufnr, ["Loading feed..."]); + + api.feed((articles) => { + setKeyMaps(); + + populateGlobalFeedArticles(articles); + + const maxColumn = articles.reduce( + (max, article) => + Math.max(article.title.length, article.description.length, max), + 0, + ); + + const feed = articles.flatMap((article) => + articleToFeed(article, maxColumn), + ); + + buffer.write(bufnr, [ + "# Your Feed", + "", + "Press in a card to open the article in a new buffer", + "and to open it in the browser.", + "", + ...feed, + ]); + }); +}; + +const setKeyMaps = () => { + vim.keymap.set("n", "", () => openArticle("buffer"), { + buffer: true, + silent: true, + }); + vim.keymap.set("n", "", () => openArticle("browser"), { + buffer: true, + silent: true, + }); +}; + +const populateGlobalFeedArticles = (articles: FeedArticle[]) => { + foremFeedArticles = new Map( + articles.map((article) => [ + article.title, + { id: article.id, url: article.url }, + ]), + ); +}; + +export const tagsToString = (tags: string[]): string => + tags.map((tag) => `#${tag}`).join(", "); +const articleToFeed = (article: FeedArticle, maxColumns: number): string[] => [ + `🭽${string.rep("▔", maxColumns)}🭾`, + ` ## ${article.title}`, + ` ${article.description}`, + ` 👤${article.user.name} (${article.user.username})`, + "▏", + + ` 🕒 ${article.reading_time_minutes} ${util.pluralize(article.reading_time_minutes, "minute")} of reading time`, + ` Tags: ${tagsToString(article.tag_list)}`, + ` 💕${article.positive_reactions_count} 💬${article.comments_count}`, + ` 📆${article.readable_publish_date}`, + "▏", + `🭼${string.rep("▁", maxColumns)}🭿`, + "", +]; diff --git a/src/forem-nvim/init.ts b/src/forem-nvim/init.ts new file mode 100644 index 0000000..c4d7da5 --- /dev/null +++ b/src/forem-nvim/init.ts @@ -0,0 +1,124 @@ +import * as Article from "./article"; +import * as api from "./api"; +import * as buffer from "./buffer"; +import * as Feed from "./feed"; +import * as notify from "./notify"; +import * as picker from "./picker"; + +const NO_API_KEY_ERROR = + "forem.nvim: FOREM_API_KEY environment variable is missing"; + +const checkApiKey = (callback: () => any) => { + if (api.key()) { + callback(); + return; + } + + notify.error(NO_API_KEY_ERROR); +}; + +const myArticles = () => api.myArticles(picker.myArticles); + +const saveArticle = () => { + const { bufnr, content } = buffer.getContent(); + const id = Number(vim.fn.expand("%:t")); + + if (!id) { + notify.error("forem.nvim: Could not find article id"); + return; + } + + const response = api.saveArticle(id, content); + + api.handleError(response, () => { + notify.info("Article saved"); + vim.api.nvim_set_option_value("modified", false, { buf: bufnr }); + }); +}; + +const newArticle = () => { + const [status, title] = pcall( + (prompt: string) => vim.fn.input(prompt), + "Article's Title: ", + ); + + if (!status || title === "") { + return; + } + + const response = api.newArticle(title); + + api.handleError(response, (article: Article.Article) => { + buffer.openMyArticle(article); + }); +}; + +const openByUrl = () => { + const [status, url] = pcall( + (prompt: string) => vim.fn.input(prompt), + "Article's URL: ", + ); + + if (!status || url === "") { + return; + } + + const [path] = string.match(url, "(%w+/[%w|-]+)$"); + + if (path === undefined) { + notify.error(`This URL is not valid: ${url}`); + return; + } + + api.getArticleByPath(path, buffer.loadArticle); +}; + +// Autocmd + +const foremAuGroup = vim.api.nvim_create_augroup("forem_autocmds", {}); + +const setAutoCmds = () => { + vim.api.nvim_create_autocmd("BufWriteCmd", { + group: foremAuGroup, + pattern: "forem://my-article/*", + callback: saveArticle, + }); + vim.api.nvim_create_autocmd("BufEnter", { + group: foremAuGroup, + pattern: "forem://articles/feed", + callback: Feed.load, + }); + vim.api.nvim_create_autocmd("CursorMoved", { + group: foremAuGroup, + pattern: "forem://*/floatmenu", + callback: () => { + const [buffer, line, column, off] = vim.fn.getpos("."); + + if (column <= 1) { + return; + } + + vim.fn.setpos(".", [buffer, line, 1, off]); + }, + }); + vim.api.nvim_create_autocmd("BufEnter", { + group: foremAuGroup, + pattern: "forem://*/floatmenu", + callback: () => { + vim.keymap.set("n", "", () => vim.api.nvim_win_close(0, false)); + }, + }); +}; + +// Setup + +setAutoCmds(); + +if (!api.key()) { + notify.error(NO_API_KEY_ERROR); +} + +export const my_articles = () => checkApiKey(myArticles); +export const new_article = () => checkApiKey(newArticle); +export const feed = () => checkApiKey(Feed.open); +export const open_url = () => checkApiKey(openByUrl); diff --git a/src/forem-nvim/notify.ts b/src/forem-nvim/notify.ts new file mode 100644 index 0000000..e31256e --- /dev/null +++ b/src/forem-nvim/notify.ts @@ -0,0 +1,9 @@ +type LogLevel = "DEBUG" | "ERROR" | "INFO" | "TRACE" | "WARN" | "OFF"; + +const notify = (message: string, level: LogLevel): void => { + vim.notify(message, vim.log.levels[level], { title: "Forem.nvim" }); +}; + +export const error = (message: string): void => notify(message, "ERROR"); + +export const info = (message: string): void => notify(message, "INFO"); diff --git a/src/forem-nvim/picker.ts b/src/forem-nvim/picker.ts new file mode 100644 index 0000000..96c7e26 --- /dev/null +++ b/src/forem-nvim/picker.ts @@ -0,0 +1,66 @@ +import * as actions from "telescope.actions"; +import * as actionState from "telescope.actions.state"; +import * as finders from "telescope.finders"; +import * as pickers from "telescope.pickers"; +import * as previewers from "telescope.previewers"; +import * as buffer from "./buffer"; +import { Article, getBodyLines } from "./article"; +import { values as configValues } from "telescope.config"; + +type Entry = { + value: Article; + display: string; + type_of: string; + ordinal: string; +}; + +const myArticlesPicker = (articles: Article[]): Picker => + pickers.new( + {}, + { + prompt_title: "My Articles", + finder: finders.new_table({ + results: articles, + entry_maker: (article: Article): Entry => { + return { + value: article, + display: article.title, + type_of: article.type_of, + ordinal: article.title, + }; + }, + }), + previewer: previewers.new_buffer_previewer({ + title: "Article Preview", + dyn_title: (_: any, entry: Entry) => entry.display, + define_preview: (self: any, entry: Entry) => { + if (self.state.bufname) { + return; + } + + const body = getBodyLines(entry.value); + + vim.api.nvim_set_option_value("filetype", "markdown", { + buf: self.state.bufnr, + }); + buffer.write(self.state.bufnr, body); + }, + get_buffer_by_name: (_self: any, entry: Entry) => entry.value.slug, + }), + sorter: configValues.prefilter_sorter({ + tag: "type_of", + sorter: configValues.generic_sorter({}), + }), + attach_mappings: (prompt_bufnr: number) => + actions.select_default.replace(() => { + const selection = actionState.get_selected_entry(prompt_bufnr); + + actions.close(prompt_bufnr); + buffer.openMyArticle(selection.value); + }), + }, + ); + +export const myArticles = (articles: Article[]): void => { + myArticlesPicker(articles).find(); +}; diff --git a/src/forem-nvim/util.ts b/src/forem-nvim/util.ts new file mode 100644 index 0000000..32b29c9 --- /dev/null +++ b/src/forem-nvim/util.ts @@ -0,0 +1,67 @@ +export const setLocals = (values: [string, any][]): void => { + values.forEach(([key, value]) => (vim.opt_local[key] = value)); +}; + +export const getOption = (option: any) => { + return option.get(option) +} + +export const isExecutable = (path: string): boolean => + vim.fn.executable(path) === 1; + +export const openUrlOnBrowser = (url: string): void => { + const cmd = getOpenCommand(); + + if (!cmd) { + vim.api.nvim_err_writeln( + `Could not find a command to open the URL: $[url]`, + ); + return; + } + + vim.fn.system(`${cmd} ${url}`); +}; + +const getOpenCommand = (): string | null => { + if (isExecutable("xdg-open")) return "xdg-open"; + if (isExecutable("open")) return "open"; + if (isExecutable("start")) return "start"; + return null; +}; + +export const pluralize = ( + count: number, + singular: string, + plural?: string, +): string => { + if (count === 1) return singular; + + return plural || `${singular}s`; +}; + +export const openFloatMenu = ( + content: string[], + options?: Record, +): void => { + const width = content.reduce((acc, line) => Math.max(acc, line.length), 0); + + const bufnr = vim.api.nvim_create_buf(false, true); + + const floatOptions = vim.tbl_extend("keep", options || {}, { + relative: "cursor", + col: 0, + row: 1, + style: "minimal", + width: width, + border: "rounded", + height: content.length, + }); + + const window = vim.api.nvim_open_win(bufnr, false, floatOptions); + + vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, content); + vim.api.nvim_buf_set_name(bufnr, "forem://feed/floatmenu"); + vim.api.nvim_set_option_value("modifiable", false, { buf: bufnr }); + vim.api.nvim_set_option_value("bufhidden", "delete", { buf: bufnr }); + vim.api.nvim_set_option_value("cursorline", true, { win: window }); +}; diff --git a/src/types/_G.d.ts b/src/types/_G.d.ts new file mode 100644 index 0000000..9f3fa32 --- /dev/null +++ b/src/types/_G.d.ts @@ -0,0 +1,3 @@ +declare let foremFeedArticles: + | Map + | undefined; diff --git a/src/types/api.d.ts b/src/types/api.d.ts new file mode 100644 index 0000000..4a55c9b --- /dev/null +++ b/src/types/api.d.ts @@ -0,0 +1,2010 @@ +/** @noSelf **/ +interface api { + /** + * Find files in runtime directories + * + * Attributes: ~ + * |api-fast| + * + * @param pat - pattern of files to search for + * @param all - whether to return all matches or only the first + * @param opts - is_lua: only search Lua subdirs + * + * @returns list of absolute paths to the found files + */ + nvim__get_runtime: (pat?: any, all?: any, opts?: any) => any; + /** + * Returns object given as argument. + * + * This API function is used for testing. One should not rely on its presence + * in plugins. + * + * @param obj - Object to return. + * + * @returns its argument. + */ + nvim__id: (obj?: any) => any; + /** + * Returns array given as argument. + * + * This API function is used for testing. One should not rely on its presence + * in plugins. + * + * @param arr - Array to return. + * + * @returns its argument. + */ + nvim__id_array: (arr?: any) => any; + /** + * Returns dictionary given as argument. + * + * This API function is used for testing. One should not rely on its presence + * in plugins. + * + * @param dct - Dictionary to return. + * + * @returns its argument. + */ + nvim__id_dictionary: (dct?: any) => any; + /** + * Returns floating-point value given as argument. + * + * This API function is used for testing. One should not rely on its presence + * in plugins. + * + * @param flt - Value to return. + * + * @returns its argument. + */ + nvim__id_float: (flt?: any) => any; + /** + * NB: if your UI doesn't use hlstate, this will not return hlstate first + * time. + */ + nvim__inspect_cell: (grid?: any, row?: any, col?: any) => any; + /** + * For testing. The condition in schar_cache_clear_if_full is hard to reach, + * so this function can be used to force a cache clear in a test. + */ + nvim__invalidate_glyph_cache: () => any; + /** + * Gets internal stats. + * + * @returns Map of various internal stats. + */ + nvim__stats: () => any; + /** + * Calls many API methods atomically. + * + * This has two main usages: + * 1. To perform several requests from an async context atomically, i.e. + * without interleaving redraws, RPC requests from other clients, or user + * interactions (however API methods may trigger autocommands or event + * processing which have such side effects, e.g. |:sleep| may wake + * timers). + * 2. To minimize RPC overhead (roundtrips) of a sequence of many requests. + * + * Attributes: ~ + * |RPC| only + * + * @param calls - an array of calls, where each call is described by an array + * with two elements: the request name, and an array of + * arguments. + * + * @returns Array of two elements. The first is an array of return values. The + * second is NIL if all calls succeeded. If a call resulted in an error, + * it is a three-element array with the zero-based index of the call + * which resulted in an error, the error type and the error message. If + * an error occurred, the values from all preceding calls will still be + * returned. + */ + nvim_call_atomic: (calls?: any) => any; + /** + * Send data to channel `id`. For a job, it writes it to the stdin of the + * process. For the stdio channel |channel-stdio|, it writes to Nvim's + * stdout. For an internal terminal instance (|nvim_open_term()|) it writes + * directly to terminal output. See |channel-bytes| for more information. + * + * This function writes raw data, not RPC messages. If the channel was + * created with `rpc=true` then the channel expects RPC messages, use + * |vim.rpcnotify()| and |vim.rpcrequest()| instead. + * + * Attributes: ~ + * |RPC| only + * Lua |vim.api| only + * + * @param chan - id of the channel + * @param data - data to write. 8-bit clean: can contain NUL bytes. + */ + nvim_chan_send: (chan?: any, data?: any) => any; + /** + * Set info for the completion candidate index. if the info was shown in a + * window, then the window and buffer ids are returned for further + * customization. If the text was not shown, an empty dict is returned. + * + * @param index - the completion candidate index + * @param opts - Optional parameters. + * • info: (string) info text. + * + * @returns Dictionary containing these keys: + * • winid: (number) floating window id + * • bufnr: (number) buffer id in floating window + */ + nvim_complete_set: (index?: any, opts?: any) => any; + /** + * Creates a new, empty, unnamed buffer. + * + * @param listed - Sets 'buflisted' + * @param scratch - Creates a "throwaway" |scratch-buffer| for temporary work + * (always 'nomodified'). Also sets 'nomodeline' on the + * buffer. + * + * @returns Buffer handle, or 0 on error + * + * See also: ~ + * • buf_open_scratch + */ + nvim_create_buf: (listed?: any, scratch?: any) => any; + /** + * Deletes the current line. + * + * Attributes: ~ + * not allowed when |textlock| is active + */ + nvim_del_current_line: () => any; + /** + * Unmaps a global |mapping| for the given mode. + * + * To unmap a buffer-local mapping, use |nvim_buf_del_keymap()|. + * + * See also: ~ + * • |nvim_set_keymap()| + */ + nvim_del_keymap: (mode?: any, lhs?: any) => any; + /** + * Deletes an uppercase/file named mark. See |mark-motions|. + * + * Note: ~ + * • Lowercase name (or other buffer-local mark) is an error. + * + * @param name - Mark name + * + * @returns true if the mark was deleted, else false. + * + * See also: ~ + * • |nvim_buf_del_mark()| + * • |nvim_get_mark()| + */ + nvim_del_mark: (name?: any) => any; + /** + * Removes a global (g:) variable. + * + * @param name - Variable name + */ + nvim_del_var: (name?: any) => any; + /** + * Echo a message. + * + * @param chunks - A list of [text, hl_group] arrays, each representing a text + * chunk with specified highlight. `hl_group` element can be + * omitted for no highlight. + * @param history - if true, add to |message-history|. + * @param opts - Optional parameters. + * • verbose: Message was printed as a result of 'verbose' + * option if Nvim was invoked with -V3log_file, the message + * will be redirected to the log_file and suppressed from + * direct output. + */ + nvim_echo: (chunks?: any, history?: any, opts?: any) => any; + /** + * Writes a message to the Vim error buffer. Does not append "\n", the + * message is buffered (won't display) until a linefeed is written. + * + * @param str - Message + */ + nvim_err_write: (str?: any) => any; + /** + * Writes a message to the Vim error buffer. Appends "\n", so the buffer is + * flushed (and displayed). + * + * @param str - Message + * + * See also: ~ + * • nvim_err_write() + */ + nvim_err_writeln: (str?: any) => any; + /** + * Evaluates statusline string. + * + * Attributes: ~ + * |api-fast| + * + * @param str - Statusline string (see 'statusline'). + * @param opts - Optional parameters. + * • winid: (number) |window-ID| of the window to use as context + * for statusline. + * • maxwidth: (number) Maximum width of statusline. + * • fillchar: (string) Character to fill blank spaces in the + * statusline (see 'fillchars'). Treated as single-width even + * if it isn't. + * • highlights: (boolean) Return highlight information. + * • use_winbar: (boolean) Evaluate winbar instead of statusline. + * • use_tabline: (boolean) Evaluate tabline instead of + * @param winid - is ignored. Mutually + * @param use_winbar - . + * • use_statuscol_lnum: (number) Evaluate statuscolumn for this + * line number instead of statusline. + * + * @returns Dictionary containing statusline information, with these keys: + * • str: (string) Characters that will be displayed on the statusline. + * • width: (number) Display width of the statusline. + * • highlights: Array containing highlight information of the + * statusline. Only included when the "highlights" key in {opts} is + * true. Each element of the array is a |Dictionary| with these keys: + * • start: (number) Byte index (0-based) of first character that uses + * the highlight. + * • group: (string) Name of highlight group. + */ + nvim_eval_statusline: (str?: any, opts?: any) => any; + /** + * Execute Lua code. Parameters (if any) are available as `...` inside the + * chunk. The chunk can return a value. + * + * Only statements are executed. To evaluate an expression, prefix it with + * `return`: return my_function(...) + * + * Attributes: ~ + * |RPC| only + * + * @param code - Lua code to execute + * @param args - Arguments to the code + * + * @returns Return value of Lua code if present or NIL. + */ + nvim_exec_lua: (code?: any, args?: any) => any; + /** + * Sends input-keys to Nvim, subject to various quirks controlled by `mode` + * flags. This is a blocking call, unlike |nvim_input()|. + * + * On execution error: does not fail, but updates v:errmsg. + * + * To input sequences like use |nvim_replace_termcodes()| (typically + * with escape_ks=false) to replace |keycodes|, then pass the result to + * nvim_feedkeys(). + * + * Example: >vim + * :let key = nvim_replace_termcodes("", v:true, v:false, v:true) + * :call nvim_feedkeys(key, 'n', v:false) + * + * + * @param keys - to be typed + * @param mode - behavior flags, see |feedkeys()| + * @param escape_ks - If true, escape K_SPECIAL bytes in `keys`. This should be + * false if you already used |nvim_replace_termcodes()|, and + * true otherwise. + * + * See also: ~ + * • feedkeys() + * • vim_strsave_escape_ks + */ + nvim_feedkeys: (keys?: any, mode?: any, escape_ks?: any) => any; + /** + * Returns a 2-tuple (Array), where item 0 is the current channel id and item + * 1 is the |api-metadata| map (Dictionary). + * + * Attributes: ~ + * |api-fast| + * |RPC| only + * + * @returns 2-tuple [{channel-id}, {api-metadata}] + */ + nvim_get_api_info: () => any; + /** + * Gets information about a channel. + * + * @param chan - channel_id, or 0 for current channel + * + * @returns Dictionary describing a channel, with these keys: + * • "id" Channel id. + * • "argv" (optional) Job arguments list. + * • "stream" Stream underlying the channel. + * • "stdio" stdin and stdout of this Nvim instance + * • "stderr" stderr of this Nvim instance + * • "socket" TCP/IP socket or named pipe + * • "job" Job with communication over its stdio. + * • "mode" How data received on the channel is interpreted. + * • "bytes" Send and receive raw bytes. + * • "terminal" |terminal| instance interprets ASCII sequences. + * • "rpc" |RPC| communication on the channel is active. + * • "pty" (optional) Name of pseudoterminal. On a POSIX system this is a + * device path like "/dev/pts/1". If the name is unknown, the key will + * still be present if a pty is used (e.g. for conpty on Windows). + * • "buffer" (optional) Buffer with connected |terminal| instance. + * • "client" (optional) Info about the peer (client on the other end of + * the RPC channel), if provided by it via |nvim_set_client_info()|. + */ + nvim_get_chan_info: (chan?: any) => any; + /** + * Returns the 24-bit RGB value of a |nvim_get_color_map()| color name or + * "#rrggbb" hexadecimal string. + * + * Example: >vim + * :echo nvim_get_color_by_name("Pink") + * :echo nvim_get_color_by_name("#cbcbcb") + * + * + * @param name - Color name or "#rrggbb" string + * + * @returns 24-bit RGB value, or -1 for invalid argument. + */ + nvim_get_color_by_name: (name?: any) => any; + /** + * Returns a map of color names and RGB values. + * + * Keys are color names (e.g. "Aqua") and values are 24-bit RGB color values + * (e.g. 65535). + * + * @returns Map of color names and RGB values. + */ + nvim_get_color_map: () => any; + /** + * Gets a map of the current editor state. + * + * @param opts - Optional parameters. + * • types: List of |context-types| ("regs", "jumps", "bufs", + * "gvars", …) to gather, or empty for "all". + * + * @returns map of global |context|. + */ + nvim_get_context: (opts?: any) => any; + /** + * Gets the current buffer. + * + * @returns Buffer handle + */ + nvim_get_current_buf: () => any; + /** + * Gets the current line. + * + * @returns Current line string + */ + nvim_get_current_line: () => any; + /** + * Gets the current tabpage. + * + * @returns Tabpage handle + */ + nvim_get_current_tabpage: () => any; + /** + * Gets the current window. + * + * @returns Window handle + */ + nvim_get_current_win: () => any; + /** + * Gets all or specific highlight groups in a namespace. + * + * Note: ~ + * • When the `link` attribute is defined in the highlight definition map, + * other attributes will not be taking effect (see |:hi-link|). + * + * @param ns_id - Get highlight groups for namespace ns_id + * |nvim_get_namespaces()|. Use 0 to get global highlight groups + * |:highlight|. + * @param opts - Options dict: + * • name: (string) Get a highlight definition by name. + * • id: (integer) Get a highlight definition by id. + * • link: (boolean, default true) Show linked group name + * instead of effective definition |:hi-link|. + * • create: (boolean, default true) When highlight group + * doesn't exist create it. + * + * @returns Highlight groups as a map from group name to a highlight definition + * map as in |nvim_set_hl()|, or only a single highlight definition map + * if requested by name or id. + */ + nvim_get_hl: (ns_id?: any, opts?: any) => any; + /** + * Gets a highlight group by name + * + * similar to |hlID()|, but allocates a new ID if not present. + */ + nvim_get_hl_id_by_name: (name?: any) => any; + /** + * Gets the active highlight namespace. + * + * @param opts - Optional parameters + * • winid: (number) |window-ID| for retrieving a window's + * highlight namespace. A value of -1 is returned when + * |nvim_win_set_hl_ns()| has not been called for the window + * (or was called with a namespace of -1). + * + * @returns Namespace id, or -1 + */ + nvim_get_hl_ns: (opts?: any) => any; + /** + * Gets a list of global (non-buffer-local) |mapping| definitions. + * + * @param mode - Mode short-name ("n", "i", "v", ...) + * + * @returns Array of |maparg()|-like dictionaries describing mappings. The + * "buffer" key is always zero. + */ + nvim_get_keymap: (mode?: any) => any; + /** + * Returns a `(row, col, buffer, buffername)` tuple representing the position + * of the uppercase/file named mark. "End of line" column position is + * returned as |v:maxcol| (big number). See |mark-motions|. + * + * Marks are (1,0)-indexed. |api-indexing| + * + * Note: ~ + * • Lowercase name (or other buffer-local mark) is an error. + * + * @param name - Mark name + * @param opts - Optional parameters. Reserved for future use. + * + * @returns 4-tuple (row, col, buffer, buffername), (0, 0, 0, '') if the mark is + * not set. + * + * See also: ~ + * • |nvim_buf_set_mark()| + * • |nvim_del_mark()| + */ + nvim_get_mark: (name?: any, opts?: any) => any; + /** + * Gets the current mode. |mode()| "blocking" is true if Nvim is waiting for + * input. + * + * Attributes: ~ + * |api-fast| + * + * @returns Dictionary { "mode": String, "blocking": Boolean } + */ + nvim_get_mode: () => any; + /** + * Gets info describing process `pid`. + * + * @returns Map of process properties, or NIL if process not found. + */ + nvim_get_proc: (pid?: any) => any; + /** + * Gets the immediate children of process `pid`. + * + * @returns Array of child process ids, empty if process not found. + */ + nvim_get_proc_children: (pid?: any) => any; + /** + * Find files in runtime directories + * + * "name" can contain wildcards. For example + * nvim_get_runtime_file("colors/‍*.vim", true) will return all color scheme + * files. Always use forward slashes (/) in the search pattern for + * subdirectories regardless of platform. + * + * It is not an error to not find any files. An empty array is returned then. + * + * Attributes: ~ + * |api-fast| + * + * @param name - pattern of files to search for + * @param all - whether to return all matches or only the first + * + * @returns list of absolute paths to the found files + */ + nvim_get_runtime_file: (name?: any, all?: any) => any; + /** + * Gets a global (g:) variable. + * + * @param name - Variable name + * + * @returns Variable value + */ + nvim_get_var: (name?: any) => any; + /** + * Gets a v: variable. + * + * @param name - Variable name + * + * @returns Variable value + */ + nvim_get_vvar: (name?: any) => any; + /** + * Queues raw user-input. Unlike |nvim_feedkeys()|, this uses a low-level + * input buffer and the call is non-blocking (input is processed + * asynchronously by the eventloop). + * + * On execution error: does not fail, but updates v:errmsg. + * + * Note: ~ + * • |keycodes| like are translated, so "<" is special. To input a + * literal "<", send . + * • For mouse events use |nvim_input_mouse()|. The pseudokey form + * "" is deprecated since |api-level| 6. + * + * Attributes: ~ + * |api-fast| + * + * @param keys - to be typed + * + * @returns Number of bytes actually written (can be fewer than requested if the + * buffer becomes full). + */ + nvim_input: (keys?: any) => any; + /** + * Send mouse event from GUI. + * + * Non-blocking: does not wait on any result, but queues the event to be + * processed soon by the event loop. + * + * Note: ~ + * • Currently this doesn't support "scripting" multiple mouse events by + * calling it multiple times in a loop: the intermediate mouse positions + * will be ignored. It should be used to implement real-time mouse input + * in a GUI. The deprecated pseudokey form ("") of + * |nvim_input()| has the same limitation. + * + * Attributes: ~ + * |api-fast| + * + * @param button - Mouse button: one of "left", "right", "middle", "wheel", + * "move", "x1", "x2". + * @param action - For ordinary buttons, one of "press", "drag", "release". + * For the wheel, one of "up", "down", "left", "right". + * Ignored for "move". + * @param modifier - String of modifiers each represented by a single char. The + * same specifiers are used as for a key press, except that + * the "-" separator is optional, so "C-A-", "c-a" and "CA" + * can all be used to specify Ctrl+Alt+click. + * @param grid - Grid number if the client uses |ui-multigrid|, else 0. + * @param row - Mouse row-position (zero-based, like redraw events) + * @param col - Mouse column-position (zero-based, like redraw events) + */ + nvim_input_mouse: ( + button?: any, + action?: any, + modifier?: any, + grid?: any, + row?: any, + col?: any, + ) => any; + /** + * Gets the current list of buffer handles + * + * Includes unlisted (unloaded/deleted) buffers, like `:ls!`. Use + * |nvim_buf_is_loaded()| to check if a buffer is loaded. + * + * @returns List of buffer handles + */ + nvim_list_bufs: () => any; + /** + * Get information about all open channels. + * + * @returns Array of Dictionaries, each describing a channel with the format + * specified at |nvim_get_chan_info()|. + */ + nvim_list_chans: () => any; + /** + * Gets the paths contained in |runtime-search-path|. + * + * @returns List of paths + */ + nvim_list_runtime_paths: () => any; + /** + * Gets the current list of tabpage handles. + * + * @returns List of tabpage handles + */ + nvim_list_tabpages: () => any; + /** + * Gets a list of dictionaries representing attached UIs. + * + * @returns Array of UI dictionaries, each with these keys: + * • "height" Requested height of the UI + * • "width" Requested width of the UI + * • "rgb" true if the UI uses RGB colors (false implies |cterm-colors|) + * • "ext_..." Requested UI extensions, see |ui-option| + * • "chan" |channel-id| of remote UI + */ + nvim_list_uis: () => any; + /** + * Gets the current list of window handles. + * + * @returns List of window handles + */ + nvim_list_wins: () => any; + /** + * Sets the current editor state from the given |context| map. + * + * @param dict - |Context| map. + */ + nvim_load_context: (dict?: any) => any; + /** + * Notify the user with a message + * + * Relays the call to vim.notify . By default forwards your message in the + * echo area but can be overridden to trigger desktop notifications. + * + * @param msg - Message to display to the user + * @param log_level - The log level + * @param opts - Reserved for future use. + */ + nvim_notify: (msg?: any, log_level?: any, opts?: any) => any; + /** + * Open a terminal instance in a buffer + * + * By default (and currently the only option) the terminal will not be + * connected to an external process. Instead, input send on the channel will + * be echoed directly by the terminal. This is useful to display ANSI + * terminal sequences returned as part of a rpc message, or similar. + * + * Note: to directly initiate the terminal using the right size, display the + * buffer in a configured window before calling this. For instance, for a + * floating display, first create an empty buffer using |nvim_create_buf()|, + * then display it using |nvim_open_win()|, and then call this function. Then + * |nvim_chan_send()| can be called immediately to process sequences in a + * virtual terminal having the intended size. + * + * Attributes: ~ + * not allowed when |textlock| is active + * + * @param buffer - the buffer to use (expected to be empty) + * @param opts - Optional parameters. + * • on_input: Lua callback for input sent, i e keypresses in + * terminal mode. Note: keypresses are sent raw as they would + * be to the pty master end. For instance, a carriage return + * is sent as a "\r", not as a "\n". |textlock| applies. It + * is possible to call |nvim_chan_send()| directly in the + * callback however. ["input", term, bufnr, data] + * • force_crlf: (boolean, default true) Convert "\n" to + * "\r\n". + * + * @returns Channel id, or 0 on error + */ + nvim_open_term: (buffer?: any, opts?: any) => any; + /** + * Writes a message to the Vim output buffer. Does not append "\n", the + * message is buffered (won't display) until a linefeed is written. + * + * @param str - Message + */ + nvim_out_write: (str?: any) => any; + /** + * Pastes at cursor, in any mode. + * + * Invokes the `vim.paste` handler, which handles each mode appropriately. + * Sets redo/undo. Faster than |nvim_input()|. Lines break at LF ("\n"). + * + * Errors ('nomodifiable', `vim.paste()` failure, …) are reflected in `err` + * but do not affect the return value (which is strictly decided by + * `vim.paste()`). On error, subsequent calls are ignored ("drained") until + * the next paste is initiated (phase 1 or -1). + * + * Attributes: ~ + * not allowed when |textlock| is active + * + * @param data - Multiline input. May be binary (containing NUL bytes). + * @param crlf - Also break lines at CR and CRLF. + * @param phase - -1: paste in a single call (i.e. without streaming). To + * "stream" a paste, call `nvim_paste` sequentially with these + * `phase` values: + * • 1: starts the paste (exactly once) + * • 2: continues the paste (zero or more times) + * • 3: ends the paste (exactly once) + * + * @returns • true: Client may continue pasting. + * • false: Client must cancel the paste. + */ + nvim_paste: (data?: any, crlf?: any, phase?: any) => any; + /** + * Puts text at cursor, in any mode. + * + * Compare |:put| and |p| which are always linewise. + * + * Attributes: ~ + * not allowed when |textlock| is active + * + * @param lines - |readfile()|-style list of lines. |channel-lines| + * @param type - Edit behavior: any |getregtype()| result, or: + * • "b" |blockwise-visual| mode (may include width, e.g. "b3") + * • "c" |charwise| mode + * • "l" |linewise| mode + * • "" guess by contents, see |setreg()| + * @param after - If true insert after cursor (like |p|), or before (like + * |P|). + * @param follow - If true place cursor at end of inserted text. + */ + nvim_put: (lines?: any, type?: any, after?: any, follow?: any) => any; + /** + * Replaces terminal codes and |keycodes| (, , ...) in a string with + * the internal representation. + * + * @param str - String to be converted. + * @param from_part - Legacy Vim parameter. Usually true. + * @param do_lt - Also translate . Ignored if `special` is false. + * @param special - Replace |keycodes|, e.g. becomes a "\r" char. + * + * See also: ~ + * • replace_termcodes + * • cpoptions + */ + nvim_replace_termcodes: ( + str?: any, + from_part?: any, + do_lt?: any, + special?: any, + ) => any; + /** + * Selects an item in the completion popup menu. + * + * If neither |ins-completion| nor |cmdline-completion| popup menu is active + * this API call is silently ignored. Useful for an external UI using + * |ui-popupmenu| to control the popup menu with the mouse. Can also be used + * in a mapping; use |:map-cmd| or a Lua mapping to ensure the mapping + * doesn't end completion mode. + * + * @param item - Index (zero-based) of the item to select. Value of -1 + * selects nothing and restores the original text. + * @param insert - For |ins-completion|, whether the selection should be + * inserted in the buffer. Ignored for |cmdline-completion|. + * @param finish - Finish the completion and dismiss the popup menu. Implies + * @param insert - . + * @param opts - Optional parameters. Reserved for future use. + */ + nvim_select_popupmenu_item: ( + item?: any, + insert?: any, + finish?: any, + opts?: any, + ) => any; + /** + * Self-identifies the client. + * + * The client/plugin/application should call this after connecting, to + * provide hints about its identity and purpose, for debugging and + * orchestration. + * + * Can be called more than once; the caller should merge old info if + * appropriate. Example: library first identifies the channel, then a plugin + * using that library later identifies itself. + * + * Note: ~ + * • "Something is better than nothing". You don't need to include all the + * fields. + * + * Attributes: ~ + * |RPC| only + * + * @param name - Short name for the connected client + * @param version - Dictionary describing the version, with these (optional) + * keys: + * • "major" major version (defaults to 0 if not set, for + * no release yet) + * • "minor" minor version + * • "patch" patch number + * • "prerelease" string describing a prerelease, like + * "dev" or "beta1" + * • "commit" hash or similar identifier of commit + * @param type - Must be one of the following values. Client libraries + * should default to "remote" unless overridden by the + * user. + * • "remote" remote client connected "Nvim flavored" + * MessagePack-RPC (responses must be in reverse order of + * requests). |msgpack-rpc| + * • "msgpack-rpc" remote client connected to Nvim via + * fully MessagePack-RPC compliant protocol. + * • "ui" gui frontend + * • "embedder" application using Nvim as a component (for + * example, IDE/editor implementing a vim mode). + * • "host" plugin host, typically started by nvim + * • "plugin" single plugin, started by nvim + * @param methods - Builtin methods in the client. For a host, this does not + * include plugin methods which will be discovered later. + * The key should be the method name, the values are dicts + * with these (optional) keys (more keys may be added in + * future versions of Nvim, thus unknown keys are ignored. + * Clients must only use keys defined in this or later + * versions of Nvim): + * • "async" if true, send as a notification. If false or + * unspecified, use a blocking request + * • "nargs" Number of arguments. Could be a single integer + * or an array of two integers, minimum and maximum + * inclusive. + * @param attributes - Arbitrary string:string map of informal client + * properties. Suggested keys: + * • "website": Client homepage URL (e.g. GitHub + * repository) + * • "license": License description ("Apache 2", "GPLv3", + * "MIT", …) + * • "logo": URI or path to image, preferably small logo or + * icon. .png or .svg format is preferred. + */ + nvim_set_client_info: ( + name?: any, + version?: any, + type?: any, + methods?: any, + attributes?: any, + ) => any; + /** + * Sets the current buffer. + * + * Attributes: ~ + * not allowed when |textlock| is active or in the |cmdwin| + * + * @param buffer - Buffer handle + */ + nvim_set_current_buf: (buffer?: any) => any; + /** + * Changes the global working directory. + * + * @param dir - Directory path + */ + nvim_set_current_dir: (dir?: any) => any; + /** + * Sets the current line. + * + * Attributes: ~ + * not allowed when |textlock| is active + * + * @param line - Line contents + */ + nvim_set_current_line: (line?: any) => any; + /** + * Sets the current tabpage. + * + * Attributes: ~ + * not allowed when |textlock| is active or in the |cmdwin| + * + * @param tabpage - Tabpage handle + */ + nvim_set_current_tabpage: (tabpage?: any) => any; + /** + * Sets the current window. + * + * Attributes: ~ + * not allowed when |textlock| is active or in the |cmdwin| + * + * @param window - Window handle + */ + nvim_set_current_win: (window?: any) => any; + /** + * Sets a highlight group. + * + * Note: ~ + * • Unlike the `:highlight` command which can update a highlight group, + * this function completely replaces the definition. For example: + * `nvim_set_hl(0, 'Visual', {})` will clear the highlight group + * 'Visual'. + * • The fg and bg keys also accept the string values `"fg"` or `"bg"` + * which act as aliases to the corresponding foreground and background + * values of the Normal group. If the Normal group has not been defined, + * using these values results in an error. + * • If `link` is used in combination with other attributes; only the + * `link` will take effect (see |:hi-link|). + * + * @param ns_id - Namespace id for this highlight |nvim_create_namespace()|. + * Use 0 to set a highlight group globally |:highlight|. + * Highlights from non-global namespaces are not active by + * default, use |nvim_set_hl_ns()| or |nvim_win_set_hl_ns()| to + * activate them. + * @param name - Highlight group name, e.g. "ErrorMsg" + * @param val - Highlight definition map, accepts the following keys: + * • fg: color name or "#RRGGBB", see note. + * • bg: color name or "#RRGGBB", see note. + * • sp: color name or "#RRGGBB" + * • blend: integer between 0 and 100 + * • bold: boolean + * • standout: boolean + * • underline: boolean + * • undercurl: boolean + * • underdouble: boolean + * • underdotted: boolean + * • underdashed: boolean + * • strikethrough: boolean + * • italic: boolean + * • reverse: boolean + * • nocombine: boolean + * • link: name of another highlight group to link to, see + * |:hi-link|. + * • default: Don't override existing definition |:hi-default| + * • ctermfg: Sets foreground of cterm color |ctermfg| + * • ctermbg: Sets background of cterm color |ctermbg| + * • cterm: cterm attribute map, like |highlight-args|. If not + * set, cterm attributes will match those from the attribute + * map documented above. + * • force: if true force update the highlight group when it + * exists. + */ + nvim_set_hl: (ns_id?: any, name?: any, val?: any) => any; + /** + * Set active namespace for highlights defined with |nvim_set_hl()|. This can + * be set for a single window, see |nvim_win_set_hl_ns()|. + * + * @param ns_id - the namespace to use + */ + nvim_set_hl_ns: (ns_id?: any) => any; + /** + * Set active namespace for highlights defined with |nvim_set_hl()| while + * redrawing. + * + * This function meant to be called while redrawing, primarily from + * |nvim_set_decoration_provider()| on_win and on_line callbacks, which are + * allowed to change the namespace during a redraw cycle. + * + * Attributes: ~ + * |api-fast| + * + * @param ns_id - the namespace to activate + */ + nvim_set_hl_ns_fast: (ns_id?: any) => any; + /** + * Sets a global |mapping| for the given mode. + * + * To set a buffer-local mapping, use |nvim_buf_set_keymap()|. + * + * Unlike |:map|, leading/trailing whitespace is accepted as part of the + * {lhs} or {rhs}. Empty {rhs} is ||. |keycodes| are replaced as usual. + * + * Example: >vim + * call nvim_set_keymap('n', ' ', '', {'nowait': v:true}) + * + * + * is equivalent to: >vim + * nmap + * + * + * @param mode - Mode short-name (map command prefix: "n", "i", "v", "x", …) + * or "!" for |:map!|, or empty string for |:map|. "ia", "ca" or + * "!a" for abbreviation in Insert mode, Cmdline mode, or both, + * respectively + * @param lhs - Left-hand-side |{lhs}| of the mapping. + * @param rhs - Right-hand-side |{rhs}| of the mapping. + * @param opts - Optional parameters map: Accepts all |:map-arguments| as keys + * except ||, values are booleans (default false). Also: + * • "noremap" disables |recursive_mapping|, like |:noremap| + * • "desc" human-readable description. + * @param rhs - . + * • "replace_keycodes" (boolean) When "expr" is true, replace + * keycodes in the resulting string (see + * |nvim_replace_termcodes()|). Returning nil from the Lua + * "callback" is equivalent to returning an empty string. + */ + nvim_set_keymap: (mode?: any, lhs?: any, rhs?: any, opts?: any) => any; + /** + * Sets a global (g:) variable. + * + * @param name - Variable name + * @param value - Variable value + */ + nvim_set_var: (name?: any, value?: any) => any; + /** + * Sets a v: variable, if it is not readonly. + * + * @param name - Variable name + * @param value - Variable value + */ + nvim_set_vvar: (name?: any, value?: any) => any; + /** + * Calculates the number of display cells occupied by `text`. Control + * characters including count as one cell. + * + * @param text - Some text + * + * @returns Number of cells + */ + nvim_strwidth: (text?: any) => any; + /** + * Subscribes to event broadcasts. + * + * Attributes: ~ + * |RPC| only + * + * @param event - Event type string + */ + nvim_subscribe: (event?: any) => any; + /** + * Unsubscribes to event broadcasts. + * + * Attributes: ~ + * |RPC| only + * + * @param event - Event type string + */ + nvim_unsubscribe: (event?: any) => any; + /** + * Activates buffer-update events on a channel, or as Lua callbacks. + * + * Example (Lua): capture buffer updates in a global `events` variable (use + * "vim.print(events)" to see its contents): >lua + * events = {} + * vim.api.nvim_buf_attach(0, false, { + * on_lines = function(...) + * table.insert(events, {...}) + * end, + * }) + * + * + * @param buffer - Buffer handle, or 0 for current buffer + * @param send_buffer - True if the initial notification should contain the + * whole buffer: first notification will be + * `nvim_buf_lines_event`. Else the first notification + * will be `nvim_buf_changedtick_event`. Not for Lua + * callbacks. + * @param opts - Optional parameters. + * • on_lines: Lua callback invoked on change. Return a + * truthy value (not `false` or `nil`) to detach. Args: + * • the string "lines" + * • buffer handle + * • b:changedtick + * • first line that changed (zero-indexed) + * • last line that was changed + * • last line in the updated range + * • byte count of previous contents + * • deleted_codepoints (if `utf_sizes` is true) + * • deleted_codeunits (if `utf_sizes` is true) + * • on_bytes: Lua callback invoked on change. This + * callback receives more granular information about the + * change compared to on_lines. Return a truthy value + * (not `false` or `nil`) to detach. Args: + * • the string "bytes" + * • buffer handle + * • b:changedtick + * • start row of the changed text (zero-indexed) + * • start column of the changed text + * • byte offset of the changed text (from the start of + * the buffer) + * • old end row of the changed text (offset from start + * row) + * • old end column of the changed text (if old end row + * = 0, offset from start column) + * • old end byte length of the changed text + * • new end row of the changed text (offset from start + * row) + * • new end column of the changed text (if new end row + * = 0, offset from start column) + * • new end byte length of the changed text + * • on_changedtick: Lua callback invoked on changedtick + * increment without text change. Args: + * • the string "changedtick" + * • buffer handle + * • b:changedtick + * • on_detach: Lua callback invoked on detach. Args: + * • the string "detach" + * • buffer handle + * • on_reload: Lua callback invoked on reload. The entire + * buffer content should be considered changed. Args: + * • the string "reload" + * • buffer handle + * • utf_sizes: include UTF-32 and UTF-16 size of the + * replaced region, as args to `on_lines`. + * • preview: also attach to command preview (i.e. + * 'inccommand') events. + * + * @returns False if attach failed (invalid parameter, or buffer isn't loaded); + * otherwise True. TODO: LUA_API_NO_EVAL + * + * See also: ~ + * • |nvim_buf_detach()| + * • |api-buffer-updates-lua| + */ + nvim_buf_attach: (buffer?: any, send_buffer?: any, opts?: any) => any; + /** + * call a function with buffer as temporary current buffer + * + * This temporarily switches current buffer to "buffer". If the current + * window already shows "buffer", the window is not switched If a window + * inside the current tabpage (including a float) already shows the buffer + * One of these windows will be set as current window temporarily. Otherwise + * a temporary scratch window (called the "autocmd window" for historical + * reasons) will be used. + * + * This is useful e.g. to call Vimscript functions that only work with the + * current buffer/window currently, like |termopen()|. + * + * Attributes: ~ + * Lua |vim.api| only + * + * @param buffer - Buffer handle, or 0 for current buffer + * @param fun - Function to call inside the buffer (currently Lua callable + * only) + * + * @returns Return value of function. + */ + nvim_buf_call: (buffer?: any, fun?: any) => any; + /** + * Unmaps a buffer-local |mapping| for the given mode. + * + * @param buffer - Buffer handle, or 0 for current buffer + * + * See also: ~ + * • |nvim_del_keymap()| + */ + nvim_buf_del_keymap: (buffer?: any, mode?: any, lhs?: any) => any; + /** + * Deletes a named mark in the buffer. See |mark-motions|. + * + * Note: ~ + * • only deletes marks set in the buffer, if the mark is not set in the + * buffer it will return false. + * + * @param buffer - Buffer to set the mark on + * @param name - Mark name + * + * @returns true if the mark was deleted, else false. + * + * See also: ~ + * • |nvim_buf_set_mark()| + * • |nvim_del_mark()| + */ + nvim_buf_del_mark: (buffer?: any, name?: any) => any; + /** + * Removes a buffer-scoped (b:) variable + * + * @param buffer - Buffer handle, or 0 for current buffer + * @param name - Variable name + */ + nvim_buf_del_var: (buffer?: any, name?: any) => any; + /** + * Deletes the buffer. See |:bwipeout| + * + * Attributes: ~ + * not allowed when |textlock| is active or in the |cmdwin| + * + * @param buffer - Buffer handle, or 0 for current buffer + * @param opts - Optional parameters. Keys: + * • force: Force deletion and ignore unsaved changes. + * • unload: Unloaded only, do not delete. See |:bunload| + */ + nvim_buf_delete: (buffer?: any, opts?: any) => any; + /** + * Deactivates buffer-update events on the channel. + * + * Attributes: ~ + * |RPC| only + * + * @param buffer - Buffer handle, or 0 for current buffer + * + * @returns False if detach failed (because the buffer isn't loaded); otherwise + * True. + * + * See also: ~ + * • |nvim_buf_attach()| + * • |api-lua-detach| for detaching Lua callbacks + */ + nvim_buf_detach: (buffer?: any) => any; + /** + * Gets a changed tick of a buffer + * + * @param buffer - Buffer handle, or 0 for current buffer + * + * @returns `b:changedtick` value. + */ + nvim_buf_get_changedtick: (buffer?: any) => any; + /** + * Gets a list of buffer-local |mapping| definitions. + * + * @param buffer - Buffer handle, or 0 for current buffer + * @param mode - Mode short-name ("n", "i", "v", ...) + * + * @returns Array of |maparg()|-like dictionaries describing mappings. The + * "buffer" key holds the associated buffer handle. + */ + nvim_buf_get_keymap: (buffer?: any, mode?: any) => any; + /** + * Gets a line-range from the buffer. + * + * Indexing is zero-based, end-exclusive. Negative indices are interpreted as + * length+1+index: -1 refers to the index past the end. So to get the last + * element use start=-2 and end=-1. + * + * Out-of-bounds indices are clamped to the nearest valid value, unless + * `strict_indexing` is set. + * + * @param buffer - Buffer handle, or 0 for current buffer + * @param start - First line index + * @param end - Last line index, exclusive + * @param strict_indexing - Whether out-of-bounds should be an error. + * + * @returns Array of lines, or empty array for unloaded buffer. + */ + nvim_buf_get_lines: ( + buffer?: any, + start?: any, + end?: any, + strict_indexing?: any, + ) => any; + /** + * Returns a `(row,col)` tuple representing the position of the named mark. + * "End of line" column position is returned as |v:maxcol| (big number). See + * |mark-motions|. + * + * Marks are (1,0)-indexed. |api-indexing| + * + * @param buffer - Buffer handle, or 0 for current buffer + * @param name - Mark name + * + * @returns (row, col) tuple, (0, 0) if the mark is not set, or is an + * uppercase/file mark set in another buffer. + * + * See also: ~ + * • |nvim_buf_set_mark()| + * • |nvim_buf_del_mark()| + */ + nvim_buf_get_mark: (buffer?: any, name?: any) => any; + /** + * Gets the full file name for the buffer + * + * @param buffer - Buffer handle, or 0 for current buffer + * + * @returns Buffer name + */ + nvim_buf_get_name: (buffer?: any) => any; + /** + * Returns the byte offset of a line (0-indexed). |api-indexing| + * + * Line 1 (index=0) has offset 0. UTF-8 bytes are counted. EOL is one byte. + * 'fileformat' and 'fileencoding' are ignored. The line index just after the + * last line gives the total byte-count of the buffer. A final EOL byte is + * counted if it would be written, see 'eol'. + * + * Unlike |line2byte()|, throws error for out-of-bounds indexing. Returns -1 + * for unloaded buffer. + * + * @param buffer - Buffer handle, or 0 for current buffer + * @param index - Line index + * + * @returns Integer byte offset, or -1 for unloaded buffer. + */ + nvim_buf_get_offset: (buffer?: any, index?: any) => any; + /** + * {opts}) + * rom the buffer. + * + * rom |nvim_buf_get_lines()| in that it allows retrieving only + * line. + * + * ro-based. Row indices are end-inclusive, and column indices + * ive. + * + * uf_get_lines()| when retrieving entire lines. + * + * + * Buffer handle, or 0 for current buffer + * } First line index + * } Starting column (byte offset) on first line + * Last line index, inclusive + * Ending column (byte offset) on last line, exclusive + * Optional parameters. Currently unused. + * + * + * ines, or empty array for unloaded buffer. + */ + nvim_buf_get_text: () => any; + /** + * Gets a buffer-scoped (b:) variable. + * + * @param buffer - Buffer handle, or 0 for current buffer + * @param name - Variable name + * + * @returns Variable value + */ + nvim_buf_get_var: (buffer?: any, name?: any) => any; + /** + * Checks if a buffer is valid and loaded. See |api-buffer| for more info + * about unloaded buffers. + * + * @param buffer - Buffer handle, or 0 for current buffer + * + * @returns true if the buffer is valid and loaded, false otherwise. + */ + nvim_buf_is_loaded: (buffer?: any) => any; + /** + * Checks if a buffer is valid. + * + * Note: ~ + * • Even if a buffer is valid it may have been unloaded. See |api-buffer| + * for more info about unloaded buffers. + * + * @param buffer - Buffer handle, or 0 for current buffer + * + * @returns true if the buffer is valid, false otherwise. + */ + nvim_buf_is_valid: (buffer?: any) => any; + /** + * Returns the number of lines in the given buffer. + * + * @param buffer - Buffer handle, or 0 for current buffer + * + * @returns Line count, or 0 for unloaded buffer. |api-buffer| + */ + nvim_buf_line_count: (buffer?: any) => any; + /** + * Sets a buffer-local |mapping| for the given mode. + * + * @param buffer - Buffer handle, or 0 for current buffer + * + * See also: ~ + * • |nvim_set_keymap()| + */ + nvim_buf_set_keymap: ( + buffer?: any, + mode?: any, + lhs?: any, + rhs?: any, + opts?: any, + ) => any; + /** + * Sets (replaces) a line-range in the buffer. + * + * Indexing is zero-based, end-exclusive. Negative indices are interpreted as + * length+1+index: -1 refers to the index past the end. So to change or + * delete the last element use start=-2 and end=-1. + * + * To insert lines at a given index, set `start` and `end` to the same index. + * To delete a range of lines, set `replacement` to an empty array. + * + * Out-of-bounds indices are clamped to the nearest valid value, unless + * `strict_indexing` is set. + * + * Attributes: ~ + * not allowed when |textlock| is active + * + * @param buffer - Buffer handle, or 0 for current buffer + * @param start - First line index + * @param end - Last line index, exclusive + * @param strict_indexing - Whether out-of-bounds should be an error. + * @param replacement - Array of lines to use as replacement + * + * See also: ~ + * • |nvim_buf_set_text()| + */ + nvim_buf_set_lines: ( + buffer?: any, + start?: any, + end?: any, + strict_indexing?: any, + replacement?: any, + ) => any; + /** + * Sets a named mark in the given buffer, all marks are allowed + * file/uppercase, visual, last change, etc. See |mark-motions|. + * + * Marks are (1,0)-indexed. |api-indexing| + * + * Note: ~ + * • Passing 0 as line deletes the mark + * + * @param buffer - Buffer to set the mark on + * @param name - Mark name + * @param line - Line number + * @param col - Column/row number + * @param opts - Optional parameters. Reserved for future use. + * + * @returns true if the mark was set, else false. + * + * See also: ~ + * • |nvim_buf_del_mark()| + * • |nvim_buf_get_mark()| + */ + nvim_buf_set_mark: ( + buffer?: any, + name?: any, + line?: any, + col?: any, + opts?: any, + ) => any; + /** + * Sets the full file name for a buffer + * + * @param buffer - Buffer handle, or 0 for current buffer + * @param name - Buffer name + */ + nvim_buf_set_name: (buffer?: any, name?: any) => any; + /** + * {replacement}) + * ) a range in the buffer + * + * ended over |nvim_buf_set_lines()| when only modifying parts + * extmarks will be preserved on non-modified parts of the + * + * + * ro-based. Row indices are end-inclusive, and column indices + * ive. + * + * at a given `(row, column)` location, use `start_row = + * and `start_col = end_col = col`. To delete the text in a + * placement = {}`. + * + * uf_set_lines()| if you are only adding or deleting entire + * + * + * ut()| if you want to insert text at the cursor position. + * + * + * d when |textlock| is active + * + * + * Buffer handle, or 0 for current buffer + * } First line index + * } Starting column (byte offset) on first line + * Last line index, inclusive + * Ending column (byte offset) on last line, exclusive + * nt} Array of lines to use as replacement + * + * + * set_lines()| + * )| + */ + nvim_buf_set_text: () => any; + /** + * Sets a buffer-scoped (b:) variable + * + * @param buffer - Buffer handle, or 0 for current buffer + * @param name - Variable name + * @param value - Variable value + */ + nvim_buf_set_var: (buffer?: any, name?: any, value?: any) => any; + /** + * Removes a tab-scoped (t:) variable + * + * @param tabpage - Tabpage handle, or 0 for current tabpage + * @param name - Variable name + */ + nvim_tabpage_del_var: (tabpage?: any, name?: any) => any; + /** + * Gets the tabpage number + * + * @param tabpage - Tabpage handle, or 0 for current tabpage + * + * @returns Tabpage number + */ + nvim_tabpage_get_number: (tabpage?: any) => any; + /** + * Gets a tab-scoped (t:) variable + * + * @param tabpage - Tabpage handle, or 0 for current tabpage + * @param name - Variable name + * + * @returns Variable value + */ + nvim_tabpage_get_var: (tabpage?: any, name?: any) => any; + /** + * Gets the current window in a tabpage + * + * @param tabpage - Tabpage handle, or 0 for current tabpage + * + * @returns Window handle + */ + nvim_tabpage_get_win: (tabpage?: any) => any; + /** + * Checks if a tabpage is valid + * + * @param tabpage - Tabpage handle, or 0 for current tabpage + * + * @returns true if the tabpage is valid, false otherwise + */ + nvim_tabpage_is_valid: (tabpage?: any) => any; + /** + * Gets the windows in a tabpage + * + * @param tabpage - Tabpage handle, or 0 for current tabpage + * + * @returns List of windows in `tabpage` + */ + nvim_tabpage_list_wins: (tabpage?: any) => any; + /** + * Sets a tab-scoped (t:) variable + * + * @param tabpage - Tabpage handle, or 0 for current tabpage + * @param name - Variable name + * @param value - Variable value + */ + nvim_tabpage_set_var: (tabpage?: any, name?: any, value?: any) => any; + /** + * Sets the current window in a tabpage + * + * @param tabpage - Tabpage handle, or 0 for current tabpage + * @param win - Window handle, must already belong to {tabpage} + */ + nvim_tabpage_set_win: (tabpage?: any, win?: any) => any; + /** + * Activates UI events on the channel. + * + * Entry point of all UI clients. Allows |--embed| to continue startup. + * Implies that the client is ready to show the UI. Adds the client to the + * list of UIs. |nvim_list_uis()| + * + * Note: ~ + * • If multiple UI clients are attached, the global screen dimensions + * degrade to the smallest client. E.g. if client A requests 80x40 but + * client B requests 200x100, the global screen has size 80x40. + * + * Attributes: ~ + * |RPC| only + * + * @param width - Requested screen columns + * @param height - Requested screen rows + * @param options - |ui-option| map + */ + nvim_ui_attach: (width?: any, height?: any, options?: any) => any; + /** + * Deactivates UI events on the channel. + * + * Removes the client from the list of UIs. |nvim_list_uis()| + * + * Attributes: ~ + * |RPC| only + */ + nvim_ui_detach: () => any; + /** + * Tells Nvim the geometry of the popupmenu, to align floating windows with + * an external popup menu. + * + * Note that this method is not to be confused with + * |nvim_ui_pum_set_height()|, which sets the number of visible items in the + * popup menu, while this function sets the bounding box of the popup menu, + * including visual elements such as borders and sliders. Floats need not use + * the same font size, nor be anchored to exact grid corners, so one can set + * floating-point numbers to the popup menu geometry. + * + * Attributes: ~ + * |RPC| only + * + * @param width - Popupmenu width. + * @param height - Popupmenu height. + * @param row - Popupmenu row. + * @param col - Popupmenu height. + */ + nvim_ui_pum_set_bounds: ( + width?: any, + height?: any, + row?: any, + col?: any, + ) => any; + /** + * Tells Nvim the number of elements displaying in the popupmenu, to decide + * and movement. + * + * Attributes: ~ + * |RPC| only + * + * @param height - Popupmenu height, must be greater than zero. + */ + nvim_ui_pum_set_height: (height?: any) => any; + /** + * Tells the nvim server if focus was gained or lost by the GUI + * + * Attributes: ~ + * |RPC| only + */ + nvim_ui_set_focus: (gained?: any) => any; + /** + * Attributes: ~ + * |RPC| only + */ + nvim_ui_set_option: (name?: any, value?: any) => any; + /** + * Tells Nvim when a terminal event has occurred + * + * The following terminal events are supported: + * • "termresponse": The terminal sent an OSC or DCS response sequence to + * Nvim. The payload is the received response. Sets |v:termresponse| and + * fires |TermResponse|. + * + * Attributes: ~ + * |RPC| only + * + * @param event - Event name + * @param value - Event payload + */ + nvim_ui_term_event: (event?: any, value?: any) => any; + /** + * Attributes: ~ + * |RPC| only + */ + nvim_ui_try_resize: (width?: any, height?: any) => any; + /** + * Tell Nvim to resize a grid. Triggers a grid_resize event with the + * requested grid size or the maximum size if it exceeds size limits. + * + * On invalid grid handle, fails with error. + * + * Attributes: ~ + * |RPC| only + * + * @param grid - The handle of the grid to be changed. + * @param width - The new requested width. + * @param height - The new requested height. + * + * + * :tw=78:ts=8:sw=4:sts=4:et:ft=help:norl: + */ + nvim_ui_try_resize_grid: (grid?: any, width?: any, height?: any) => any; + /** + * Calls a function with window as temporary current window. + * + * Attributes: ~ + * Lua |vim.api| only + * + * @param window - Window handle, or 0 for current window + * @param fun - Function to call inside the window (currently Lua callable + * only) + * + * @returns Return value of function. + * + * See also: ~ + * • |win_execute()| + * • |nvim_buf_call()| + */ + nvim_win_call: (window?: any, fun?: any) => any; + /** + * Closes the window (like |:close| with a |window-ID|). + * + * Attributes: ~ + * not allowed when |textlock| is active + * + * @param window - Window handle, or 0 for current window + * @param force - Behave like `:close!` The last window of a buffer with + * unwritten changes can be closed. The buffer will become + * hidden, even if 'hidden' is not set. + */ + nvim_win_close: (window?: any, force?: any) => any; + /** + * Removes a window-scoped (w:) variable + * + * @param window - Window handle, or 0 for current window + * @param name - Variable name + */ + nvim_win_del_var: (window?: any, name?: any) => any; + /** + * Gets the current buffer in a window + * + * @param window - Window handle, or 0 for current window + * + * @returns Buffer handle + */ + nvim_win_get_buf: (window?: any) => any; + /** + * Gets the (1,0)-indexed, buffer-relative cursor position for a given window + * (different windows showing the same buffer have independent cursor + * positions). |api-indexing| + * + * @param window - Window handle, or 0 for current window + * + * @returns (row, col) tuple + * + * See also: ~ + * • |getcurpos()| + */ + nvim_win_get_cursor: (window?: any) => any; + /** + * Gets the window height + * + * @param window - Window handle, or 0 for current window + * + * @returns Height as a count of rows + */ + nvim_win_get_height: (window?: any) => any; + /** + * Gets the window number + * + * @param window - Window handle, or 0 for current window + * + * @returns Window number + */ + nvim_win_get_number: (window?: any) => any; + /** + * Gets the window position in display cells. First position is zero. + * + * @param window - Window handle, or 0 for current window + * + * @returns (row, col) tuple with the window position + */ + nvim_win_get_position: (window?: any) => any; + /** + * Gets the window tabpage + * + * @param window - Window handle, or 0 for current window + * + * @returns Tabpage that contains the window + */ + nvim_win_get_tabpage: (window?: any) => any; + /** + * Gets a window-scoped (w:) variable + * + * @param window - Window handle, or 0 for current window + * @param name - Variable name + * + * @returns Variable value + */ + nvim_win_get_var: (window?: any, name?: any) => any; + /** + * Gets the window width + * + * @param window - Window handle, or 0 for current window + * + * @returns Width as a count of columns + */ + nvim_win_get_width: (window?: any) => any; + /** + * Closes the window and hide the buffer it contains (like |:hide| with a + * |window-ID|). + * + * Like |:hide| the buffer becomes hidden unless another window is editing + * it, or 'bufhidden' is `unload`, `delete` or `wipe` as opposed to |:close| + * or |nvim_win_close()|, which will close the buffer. + * + * Attributes: ~ + * not allowed when |textlock| is active + * + * @param window - Window handle, or 0 for current window + */ + nvim_win_hide: (window?: any) => any; + /** + * Checks if a window is valid + * + * @param window - Window handle, or 0 for current window + * + * @returns true if the window is valid, false otherwise + */ + nvim_win_is_valid: (window?: any) => any; + /** + * Sets the current buffer in a window, without side effects + * + * Attributes: ~ + * not allowed when |textlock| is active + * + * @param window - Window handle, or 0 for current window + * @param buffer - Buffer handle + */ + nvim_win_set_buf: (window?: any, buffer?: any) => any; + /** + * Sets the (1,0)-indexed cursor position in the window. |api-indexing| This + * scrolls the window even if it is not the current one. + * + * @param window - Window handle, or 0 for current window + * @param pos - (row, col) tuple representing the new position + */ + nvim_win_set_cursor: (window?: any, pos?: any) => any; + /** + * Sets the window height. + * + * @param window - Window handle, or 0 for current window + * @param height - Height as a count of rows + */ + nvim_win_set_height: (window?: any, height?: any) => any; + /** + * Set highlight namespace for a window. This will use highlights defined + * with |nvim_set_hl()| for this namespace, but fall back to global + * highlights (ns=0) when missing. + * + * This takes precedence over the 'winhighlight' option. + * + * @param ns_id - the namespace to use + */ + nvim_win_set_hl_ns: (window?: any, ns_id?: any) => any; + /** + * Sets a window-scoped (w:) variable + * + * @param window - Window handle, or 0 for current window + * @param name - Variable name + * @param value - Variable value + */ + nvim_win_set_var: (window?: any, name?: any, value?: any) => any; + /** + * Sets the window width. This will only succeed if the screen is split + * vertically. + * + * @param window - Window handle, or 0 for current window + * @param width - Width as a count of columns + */ + nvim_win_set_width: (window?: any, width?: any) => any; + /** + * Computes the number of screen lines occupied by a range of text in a given + * window. Works for off-screen text and takes folds into account. + * + * Diff filler or virtual lines above a line are counted as a part of that + * line, unless the line is on "start_row" and "start_vcol" is specified. + * + * Diff filler or virtual lines below the last buffer line are counted in the + * result when "end_row" is omitted. + * + * Line indexing is similar to |nvim_buf_get_text()|. + * + * @param window - Window handle, or 0 for current window. + * @param opts - Optional parameters: + * • start_row: Starting line index, 0-based inclusive. When + * omitted start at the very top. + * • end_row: Ending line index, 0-based inclusive. When + * omitted end at the very bottom. + * • start_vcol: Starting virtual column index on "start_row", + * 0-based inclusive, rounded down to full screen lines. When + * omitted include the whole line. + * • end_vcol: Ending virtual column index on "end_row", + * 0-based exclusive, rounded up to full screen lines. When + * omitted include the whole line. + * + * @returns Dictionary containing text height information, with these keys: + * • all: The total number of screen lines occupied by the range. + * • fill: The number of diff filler or virtual lines among them. + * + * See also: ~ + * • |virtcol()| for text width. + */ + nvim_win_text_height: (window?: any, opts?: any) => any; + nvim_open_win: (buffer?: any, enter?: any, config?: any) => any; + nvim_set_option_value: ( + name: string, + value: any, + opts?: { scope?: "global" | "local"; win?: any; buf?: number }, + ) => void; + + nvim_create_augroup: (name: string, options: { clear?: boolean }) => number; + nvim_create_autocmd: ( + event: AutoCmdEvent | AutoCmdEvent[], + options: { + group?: string | number; + pattern?: string | string[]; + buffer?: number; + desc?: string; + callback: string | (() => any); + command?: string; + once?: boolean; + nested?: boolean; + }, + ) => number; + nvim_create_user_command: ( + name: string, + command: (args: { + name: string; + args: string; + fargs: string[]; + nargs: string; + bang: boolean; + line1: number; + line2: number; + range: number; + count: number; + reg: string; + mods: string; + smods: any; + }) => any, + options?: { + nargs?: 0 | 1 | "*" | "?" | "+"; + bang?: boolean; + bar?: boolean; + complete?: + | string + | ((argLead: string, cmdLine: string, cursorPos: number) => string[]); + desc?: string; + force?: boolean; + preview?: () => any; + }, + ) => void; +} + +type AutoCmdEvent = + | "BufAdd" + | "BufDelete" + | "BufEnter" + | "BufFilePost" + | "BufFilePre" + | "BufHidden" + | "BufLeave" + | "BufModifiedSet" + | "BufNew" + | "BufNewFile" + | "BufRead" + | "BufReadPost" + | "BufReadCmd" + | "BufReadPre" + | "BufUnload" + | "BufWinEnter" + | "BufWinLeave" + | "BufWipeout" + | "BufWrite" + | "BufWritePre" + | "BufWriteCmd" + | "BufWritePost" + | "ChanInfo" + | "ChanOpen" + | "CmdUndefined" + | "CmdlineChanged" + | "CmdlineEnter" + | "CmdlineLeave" + | "CmdwinEnter" + | "CmdwinLeave" + | "ColorScheme" + | "ColorSchemePre" + | "CompleteChanged" + | "CompleteDonePre" + | "CompleteDone" + | "CursorHold" + | "CursorHoldI" + | "CursorMoved" + | "CursorMovedI" + | "DiffUpdated" + | "DirChanged" + | "DirChangedPre" + | "ExitPre" + | "FileAppendCmd" + | "FileAppendPost" + | "FileAppendPre" + | "FileChangedRO" + | "FileChangedShell" + | "FileChangedShellPost" + | "FileReadCmd" + | "FileReadPost" + | "FileReadPre" + | "FileType" + | "FileWriteCmd" + | "FileWritePost" + | "FileWritePre" + | "FilterReadPost" + | "FilterReadPre" + | "FilterWritePost" + | "FilterWritePre" + | "FocusGained" + | "FocusLost" + | "FuncUndefined" + | "UIEnter" + | "UILeave" + | "InsertChange" + | "InsertCharPre" + | "InsertEnter" + | "InsertLeavePre" + | "InsertLeave" + | "MenuPopup" + | "ModeChanged" + | "OptionSet" + | "QuickFixCmdPre" + | "QuickFixCmdPost" + | "QuitPre" + | "RemoteReply" + | "SearchWrapped" + | "RecordingEnter" + | "RecordingLeave" + | "SafeState" + | "SessionLoadPost" + | "ShellCmdPost" + | "Signal" + | "ShellFilterPost" + | "SourcePre" + | "SourcePost" + | "SourceCmd" + | "SpellFileMissing" + | "StdinReadPost" + | "StdinReadPre" + | "SwapExists" + | "Syntax" + | "TabEnter" + | "TabLeave" + | "TabNew" + | "TabNewEntered" + | "TabClosed" + | "TermOpen" + | "TermEnter" + | "TermLeave" + | "TermClose" + | "TermRequest" + | "TermResponse" + | "TextChanged" + | "TextChangedI" + | "TextChangedP" + | "TextChangedT" + | "TextYankPost" + | "User" + | "UserGettingBored" + | "VimEnter" + | "VimLeave" + | "VimLeavePre" + | "VimResized" + | "VimResume" + | "VimSuspend" + | "WinClosed" + | "WinEnter" + | "WinLeave" + | "WinNew" + | "WinScrolled" + | "WinResized"; diff --git a/src/types/fn.d.ts b/src/types/fn.d.ts new file mode 100644 index 0000000..62591f8 --- /dev/null +++ b/src/types/fn.d.ts @@ -0,0 +1,19 @@ +/** @noSelf **/ +interface fn { + executable: (path: string | string[]) => 1 | 0 | -1; + system: (cmd: string | string[], opts?: any) => any; + json_decode: (json: string) => string; + json_encode: (a: any) => string; + getline: (lnum: string | number, end?: string | number) => string; + line: (expr: string, winid?: number) => number; + expand: (keywords: string, nosuf?: boolean, list?: boolean) => string; + input: (prompt: string, text?: string, completion?: string) => string; + getpos: (expr: string) => [number, number, number, number]; + setpos: ( + expr: string, + list: + | [number, number, number, number] + | [number, number, number, number, number], + ) => 0 | -1; + isdirectory: (directory: string) => 0 | 1; +} diff --git a/src/types/forem-nvim.d.ts b/src/types/forem-nvim.d.ts new file mode 100644 index 0000000..b0c5474 --- /dev/null +++ b/src/types/forem-nvim.d.ts @@ -0,0 +1,15 @@ +/** @noResolution */ +declare module "forem-nvim" { + const exports: ForemNvim; + export = exports; +} +declare interface ForemNvim { + feed: any; + my_articles: any; + new_article: any; + open_url: any; +} +/** @noResolution */ +declare module "forem-nvim.notify" { + const error: any; +} diff --git a/src/types/global.d.ts b/src/types/global.d.ts new file mode 100644 index 0000000..5aaf4d3 --- /dev/null +++ b/src/types/global.d.ts @@ -0,0 +1,694 @@ +/** @noSelf **/ +declare namespace vim { + /** + * Executes Vim script commands. + * + * Note that `vim.cmd` can be indexed with a command name to return a + * callable function to the command. + * + * Example: >lua + * vim.cmd('echo 42') + * vim.cmd([[ + * augroup My_group + * autocmd! + * autocmd FileType c setlocal cindent + * augroup END + * ]]) + * + * -- Ex command :echo "foo" + * -- Note string literals need to be double quoted. + * vim.cmd('echo "foo"') + * vim.cmd { cmd = 'echo', args = { '"foo"' } } + * vim.cmd.echo({ args = { '"foo"' } }) + * vim.cmd.echo('"foo"') + * + * -- Ex command :write! myfile.txt + * vim.cmd('write! myfile.txt') + * vim.cmd { cmd = 'write', args = { "myfile.txt" }, bang = true } + * vim.cmd.write { args = { "myfile.txt" }, bang = true } + * vim.cmd.write { "myfile.txt", bang = true } + * + * -- Ex command :colorscheme blue + * vim.cmd('colorscheme blue') + * vim.cmd.colorscheme('blue') + * + * + * @param command - (`string|table`) Command(s) to execute. If a string, + * executes multiple lines of Vim script at once. In this + * case, it is an alias to |nvim_exec2()|, where `opts.output` + * is set to false. Thus it works identical to |:source|. If a + * table, executes a single command. In this case, it is an + * alias to |nvim_cmd()| where `opts` is empty. + * + * See also: ~ + * • |ex-cmd-index| + */ + function cmd(command?: any): any; + /** + * Defers calling {fn} until {timeout} ms passes. + * + * Use to do a one-shot timer that calls {fn} Note: The {fn} is + * |vim.schedule_wrap()|ped automatically, so API functions are safe to call. + * + * @param fn - (`function`) Callback to call once `timeout` expires + * @param timeout - (`integer`) Number of milliseconds to wait before calling + * `fn` + * + * @returns (`table`) timer luv timer object + */ + function defer_fn(fn?: any, timeout?: any): any; + /** + * Shows a deprecation message to the user. + * + * @param name - (`string`) Deprecated feature (function, API, etc.). + * @param alternative - (`string?`) Suggested alternative feature. + * @param version - (`string`) Version when the deprecated function will be + * removed. + * @param plugin - (`string?`) Name of the plugin that owns the deprecated + * feature. Defaults to "Nvim". + * @param backtrace - (`boolean?`) Prints backtrace. Defaults to true. + * + * @returns (`string?`) Deprecated message, or nil if no message was shown. + */ + function deprecate( + name?: any, + alternative?: any, + version?: any, + plugin?: any, + backtrace?: any, + ): any; + /** + * Gets a human-readable representation of the given object. + * + * @returns (`string`) + * + * See also: ~ + * • |vim.print()| + * • https://github.com/kikito/inspect.lua + * • https://github.com/mpeterv/vinspect + */ + function inspect(a: any): any; + /** + * Translates keycodes. + * + * Example: >lua + * local k = vim.keycode + * vim.g.mapleader = k'' + * + * + * @param str - (`string`) String to be converted. + * + * @returns (`string`) + * + * See also: ~ + * • |nvim_replace_termcodes()| + */ + function keycode(str?: any): any; + /** + * Omnifunc for completing Lua values from the runtime Lua interpreter, + * similar to the builtin completion for the `:lua` command. + * + * Activate using `set omnifunc=v:lua.vim.lua_omnifunc` in a Lua buffer. + * + * @param find_start - (`1|0`) + */ + function lua_omnifunc(find_start?: any): any; + /** + * Displays a notification to the user. + * + * This function can be overridden by plugins to display notifications using + * a custom provider (such as the system notification provider). By default, + * writes to |:messages|. + * + * @param msg - (`string`) Content of the notification to show to the user. + * @param level - (`integer?`) One of the values from |vim.log.levels|. + * @param opts - (`table?`) Optional parameters. Unused by default. + */ + function notify(msg?: any, level?: any, opts?: any): any; + /** + * Displays a notification only one time. + * + * Like |vim.notify()|, but subsequent calls with the same message will not + * display a notification. + * + * @param msg - (`string`) Content of the notification to show to the user. + * @param level - (`integer?`) One of the values from |vim.log.levels|. + * @param opts - (`table?`) Optional parameters. Unused by default. + * + * @returns (`boolean`) true if message was displayed, else false + */ + function notify_once(msg?: any, level?: any, opts?: any): any; + /** + * Adds Lua function {fn} with namespace id {ns_id} as a listener to every, + * yes every, input key. + * + * The Nvim command-line option |-w| is related but does not support + * callbacks and cannot be toggled dynamically. + * + * Note: ~ + * • {fn} will be removed on error. + * • {fn} will not be cleared by |nvim_buf_clear_namespace()| + * • {fn} will receive the keys after mappings have been evaluated + * + * @param fn - (`fun(key: string)?`) Function invoked on every key press. + * @param ns_id - is specified removes + * @param ns_id - . + * @param ns_id - (`integer?`) Namespace ID. If nil or 0, generates and returns + * a new |nvim_create_namespace()| id. + * + * @returns (`integer`) Namespace id associated with {fn}. Or count of all + * callbacks if on_key() is called without arguments. + */ + function on_key(fn?: any, ns_id?: any): any; + /** + * Paste handler, invoked by |nvim_paste()| when a conforming UI (such as the + * |TUI|) pastes text into the editor. + * + * Example: To remove ANSI color codes when pasting: >lua + * vim.paste = (function(overridden) + * return function(lines, phase) + * for i,line in ipairs(lines) do + * -- Scrub ANSI color codes from paste input. + * lines[i] = line:gsub('\27%[[0-9;mK]+', '') + * end + * overridden(lines, phase) + * end + * end)(vim.paste) + * + * + * @param lines - (`string[]`) |readfile()|-style list of lines to paste. + * |channel-lines| + * @param phase - (`-1|1|2|3`) -1: "non-streaming" paste: the call contains all + * lines. If paste is "streamed", `phase` indicates the stream + * state: + * • 1: starts the paste (exactly once) + * • 2: continues the paste (zero or more times) + * • 3: ends the paste (exactly once) + * + * @returns (`boolean`) result false if client should cancel the paste. + * + * See also: ~ + * • |paste| + */ + function paste(lines?: any, phase?: any): any; + /** + * "Pretty prints" the given arguments and returns them unmodified. + * + * Example: >lua + * local hl_normal = vim.print(vim.api.nvim_get_hl(0, { name = 'Normal' })) + * + * + * @param ... - (`any`) + * + * @returns (`any`) given arguments. + * + * See also: ~ + * • |vim.inspect()| + * • |:=| + */ + function print(...arguments: any[]): any; + /** + * Gets a dict of line segment ("chunk") positions for the region from `pos1` + * to `pos2`. + * + * Input and output positions are byte positions, (0,0)-indexed. "End of + * line" column position (for example, |linewise| visual selection) is + * returned as |v:maxcol| (big number). + * + * @param bufnr - (`integer`) Buffer number, or 0 for current buffer + * @param pos1 - (`integer[]|string`) Start of region as a (line, column) + * tuple or |getpos()|-compatible string + * @param pos2 - (`integer[]|string`) End of region as a (line, column) + * tuple or |getpos()|-compatible string + * @param regtype - (`string`) |setreg()|-style selection type + * @param inclusive - (`boolean`) Controls whether the ending column is + * inclusive (see also 'selection'). + * + * @returns (`table`) region Dict of the form `{linenr = {startcol,endcol}}`. + * `endcol` is exclusive, and whole lines are returned as + * `{startcol,endcol} = {0,-1}`. + */ + function region( + bufnr?: any, + pos1?: any, + pos2?: any, + regtype?: any, + inclusive?: any, + ): any; + /** + * Returns a function which calls {fn} via |vim.schedule()|. + * + * The returned function passes all arguments to {fn}. + * + * Example: >lua + * function notify_readable(_err, readable) + * vim.notify("readable? " .. tostring(readable)) + * end + * vim.uv.fs_access(vim.fn.stdpath("config"), "R", vim.schedule_wrap(notify_readable)) + * + * + * @param fn - (`function`) + * + * @returns (`function`) + * + * See also: ~ + * • |lua-loop-callbacks| + * • |vim.schedule()| + * • |vim.in_fast_event()| + */ + function schedule_wrap(fn?: any): any; + /** + * Runs a system command or throws an error if {cmd} cannot be run. + * + * Examples: >lua + * local on_exit = function(obj) + * print(obj.code) + * print(obj.signal) + * print(obj.stdout) + * print(obj.stderr) + * end + * + * -- Runs asynchronously: + * vim.system({'echo', 'hello'}, { text = true }, on_exit) + * + * -- Runs synchronously: + * local obj = vim.system({'echo', 'hello'}, { text = true }):wait() + * -- { code = 0, signal = 0, stdout = 'hello', stderr = '' } + * + * + * See |uv.spawn()| for more details. Note: unlike |uv.spawn()|, vim.system + * throws an error if {cmd} cannot be run. + * + * @param cmd - (`string[]`) Command to execute + * @param opts - (`vim.SystemOpts?`) Options: + * • cwd: (string) Set the current working directory for the + * sub-process. + * • env: table Set environment variables for + * the new process. Inherits the current environment with + * `NVIM` set to |v:servername|. + * • clear_env: (boolean) `env` defines the job environment + * exactly, instead of merging current environment. + * • stdin: (string|string[]|boolean) If `true`, then a pipe + * to stdin is opened and can be written to via the + * `write()` method to SystemObj. If string or string[] then + * will be written to stdin and closed. Defaults to `false`. + * • stdout: (boolean|function) Handle output from stdout. + * When passed as a function must have the signature + * `fun(err: string, data: string)`. Defaults to `true` + * • stderr: (boolean|function) Handle output from stderr. + * When passed as a function must have the signature + * `fun(err: string, data: string)`. Defaults to `true`. + * • text: (boolean) Handle stdout and stderr as text. + * Replaces `\r\n` with `\n`. + * • timeout: (integer) Run the command with a time limit. + * Upon timeout the process is sent the TERM signal (15) and + * the exit code is set to 124. + * • detach: (boolean) If true, spawn the child process in a + * detached state - this will make it a process group + * leader, and will effectively enable the child to keep + * running after the parent exits. Note that the child + * process will still keep the parent's event loop alive + * unless the parent process calls |uv.unref()| on the + * child's process handle. + * @param on_exit - (`fun(out: vim.SystemCompleted)?`) Called when subprocess + * exits. When provided, the command runs asynchronously. + * Receives SystemCompleted object, see return of + * SystemObj:wait(). + * + * @returns (`vim.SystemObj`) Object with the fields: + * • pid (integer) Process ID + * • wait (fun(timeout: integer|nil): SystemCompleted) Wait for the + * process to complete. Upon timeout the process is sent the KILL + * signal (9) and the exit code is set to 124. Cannot be called in + * |api-fast|. + * • SystemCompleted is an object with the fields: + * • code: (integer) + * • signal: (integer) + * • stdout: (string), nil if stdout argument is passed + * • stderr: (string), nil if stderr argument is passed + * • kill (fun(signal: integer|string)) + * • write (fun(data: string|nil)) Requires `stdin=true`. Pass `nil` to + * close the stream. + * • is_closing (fun(): boolean) + */ + function system(cmd?: any, opts?: any, on_exit?: any): any; + /** + * Invokes |vim-function| or |user-function| {func} with arguments {...}. + * See also |vim.fn|. + * Equivalent to: >lua + * vim.fn[func]({...}) + * + * cmd({command}) + * See |vim.cmd()|. + */ + function call(func?: any, ...arguments: any[]): any; + /** + * Global (|g:|) editor variables. + * Key with no value returns `nil`. + */ + let g: any; + /** + * Buffer-scoped (|b:|) variables for the current buffer. + * Invalid or unset key returns `nil`. Can be indexed with + * an integer to access variables for a specific buffer. + */ + let b: any; + /** + * Window-scoped (|w:|) variables for the current window. + * Invalid or unset key returns `nil`. Can be indexed with + * an integer to access variables for a specific window. + */ + let w: any; + /** + * Tabpage-scoped (|t:|) variables for the current tabpage. + * Invalid or unset key returns `nil`. Can be indexed with + * an integer to access variables for a specific tabpage. + */ + let t: any; + /** + * |v:| variables. + * Invalid or unset key returns `nil`. + */ + let v: any; + /** + * + */ + let opt: any; + let opt_local: any; + /** + * Get or set buffer-scoped |options| for the buffer with number {bufnr}. If + * [{bufnr}] is omitted then the current buffer is used. Invalid {bufnr} or + * key is an error. + * + * Note: this is equivalent to `:setlocal` for |global-local| options and + * `:set` otherwise. + * + * Example: >lua + * local bufnr = vim.api.nvim_get_current_buf() + * vim.bo[bufnr].buflisted = true -- same as vim.bo.buflisted = true + * print(vim.bo.comments) + * print(vim.bo.baz) -- error: invalid key + * + */ + let bo: any; + /** + * Environment variables defined in the editor session. See |expand-env| and + * |:let-environment| for the Vimscript behavior. Invalid or unset key + * returns `nil`. + * + * Example: >lua + * vim.env.FOO = 'bar' + * print(vim.env.TERM) + * + */ + let env: any; + /** + * Get or set global |options|. Like `:setglobal`. Invalid key is an error. + * + * Note: this is different from |vim.o| because this accesses the global + * option value and thus is mostly useful for use with |global-local| + * options. + * + * Example: >lua + * vim.go.cmdheight = 4 + * print(vim.go.columns) + * print(vim.go.bar) -- error: invalid key + * + */ + let go: any; + /** + * Get or set |options|. Like `:set`. Invalid key is an error. + * + * Note: this works on both buffer-scoped and window-scoped options using the + * current buffer and window. + * + * Example: >lua + * vim.o.cmdheight = 4 + * print(vim.o.columns) + * print(vim.o.foo) -- error: invalid key + * + */ + let o: any; + /** + * Get or set window-scoped |options| for the window with handle {winid} and + * buffer with number {bufnr}. Like `:setlocal` if setting a |global-local| + * option or if {bufnr} is provided, like `:set` otherwise. If [{winid}] is + * omitted then the current window is used. Invalid {winid}, {bufnr} or key + * is an error. + * + * Note: only {bufnr} with value `0` (the current buffer in the window) is + * supported. + * + * Example: >lua + * local winid = vim.api.nvim_get_current_win() + * vim.wo[winid].number = true -- same as vim.wo.number = true + * print(vim.wo.foldmarker) + * print(vim.wo.quux) -- error: invalid key + * vim.wo[winid][0].spell = false -- like ':setlocal nospell' + * + */ + let wo: any; + /** + * Get a URI from a bufnr + * + * @param bufnr - (number): Buffer number + * + * @returns URI + */ + function uri_from_bufnr(bufnr?: any): any; + /** + * Get a URI from a file path. + * + * @param path - (string): Path to file + * + * @returns URI + */ + function uri_from_fname(path?: any): any; + /** + * Return or create a buffer for a uri. + * + * @param uri - (string): The URI + * + * @returns bufnr. + * + * Note: + * Creates buffer but does not load it + */ + function uri_to_bufnr(uri?: any): any; + /** + * Get a filename from a URI + * + * @param uri - (string): The URI + * + * @returns Filename + * + * ft=help:norl: + */ + function uri_to_fname(uri?: any): any; + /** + * Gets the version of the current Nvim build. + */ + function version(): any; + /** + * Returns true if the code is executing as part of a "fast" event + * handler, where most of the API is disabled. These are low-level events + * (e.g. |lua-loop-callbacks|) which can be invoked whenever Nvim polls + * for input. When this is `false` most API functions are callable (but + * may be subject to other restrictions such as |textlock|). + */ + function in_fast_event(): any; + /** + * Special value representing NIL in |RPC| and |v:null| in Vimscript + * conversion, and similar cases. Lua `nil` cannot be used as part of + * a Lua table representing a Dictionary or Array, because it is + * treated as missing: `{"foo", nil}` is the same as `{"foo"}`. + */ + let NIL: any; + /** + * Creates a special empty table (marked with a metatable), which Nvim + * converts to an empty dictionary when translating Lua values to + * Vimscript or API types. Nvim by default converts an empty table `{}` + * without this metatable to an list/array. + * + * Note: if numeric keys are present in the table, Nvim ignores the + * metatable marker and converts the dict to a list/array anyway. + */ + function empty_dict(): any; + /** + * Sends {event} to {channel} via |RPC| and returns immediately. If + * {channel} is 0, the event is broadcast to all channels. + * + * This function also works in a fast callback |lua-loop-callbacks|. + */ + function rpcnotify(channel?: any, method?: any, ...arguments: any[]): any; + /** + * Sends a request to {channel} to invoke {method} via |RPC| and blocks + * until a response is received. + * + * Note: NIL values as part of the return value is represented as + * |vim.NIL| special value + */ + function rpcrequest(channel?: any, method?: any, ...arguments: any[]): any; + /** + * Compares strings case-insensitively. Returns 0, 1 or -1 if strings + * are equal, {a} is greater than {b} or {a} is lesser than {b}, + * respectively. + */ + function stricmp(a?: any, b?: any): any; + /** + * Convert byte index to UTF-32 and UTF-16 indicies. If {index} is not + * supplied, the length of the string is used. All indicies are zero-based. + * Returns two values: the UTF-32 and UTF-16 indicies respectively. + * + * Embedded NUL bytes are treated as terminating the string. Invalid + * UTF-8 bytes, and embedded surrogates are counted as one code + * point each. An {index} in the middle of a UTF-8 sequence is rounded + * upwards to the end of that sequence. + */ + function str_utfindex(str?: any, index?: any): any; + /** + * Convert UTF-32 or UTF-16 {index} to byte index. If {use_utf16} is not + * supplied, it defaults to false (use UTF-32). Returns the byte index. + * + * Invalid UTF-8 and NUL is treated like by |vim.str_byteindex()|. An {index} + * in the middle of a UTF-16 sequence is rounded upwards to the end of that + * sequence. + */ + function str_byteindex(str?: any, index?: any, use_utf16?: any): any; + /** + * Schedules {callback} to be invoked soon by the main event-loop. Useful + * to avoid |textlock| or other temporary restrictions. + */ + function schedule(callback?: any): any; + /** + * Defers calling {fn} until {timeout} ms passes. Use to do a one-shot timer + * that calls {fn}. + * + * Note: The {fn} is |schedule_wrap|ped automatically, so API functions are + * safe to call. + * + * @param fn - Callback to call once {timeout} expires + * @param timeout - Time in ms to wait before calling {fn} + * + * Returns: ~ + * |vim.loop|.new_timer() object + */ + function defer_fn(fn?: any, timeout?: any): any; + /** + * Wait for {time} in milliseconds until {callback} returns `true`. + * + * Executes {callback} immediately and at approximately {interval} + * milliseconds (default 200). Nvim still processes other events during + * this time. + * + * meters: ~ + * {time} Number of milliseconds to wait + * {callback} Optional callback. Waits until {callback} returns true + * {interval} (Approximate) number of milliseconds to wait between polls + * {fast_only} If true, only |api-fast| events will be processed. + * If called from while in an |api-fast| event, will + * automatically be set to `true`. + * + * rns: ~ + * If {callback} returns `true` during the {time}: + * `true, nil` + * + * If {callback} never returns `true` during the {time}: + * `false, -1` + * + * If {callback} is interrupted during the {time}: + * `false, -2` + * + * If {callback} errors, the error is raised. + * + * Examples: > + * + * + * ait for 100 ms, allowing other events to process + * wait(100, function() end) + * + * + * ait for 100 ms or until global variable set. + * wait(100, function() return vim.g.waiting_for_var end) + * + * + * ait for 1 second or until global variable set, checking every ~500 ms + * wait(1000, function() return vim.g.waiting_for_var end, 500) + * + * + * chedule a function to set a value in 100ms + * defer_fn(function() vim.g.timer_result = true end, 100) + * + * ould wait ten seconds if results blocked. Actually only waits 100 ms + * im.wait(10000, function() return vim.g.timer_result end) then + * int('Only waiting a little bit of time!') + * + * + */ + function wait( + time?: any, + callback?: any, + interval?: any, + fast_only?: any, + ): any; + /** + * Type index for use in |lua-special-tbl|. Specifying one of the values + * from |vim.types| allows typing the empty table (it is unclear whether + * empty Lua table represents empty list or empty array) and forcing + * integral numbers to be |Float|. See |lua-special-tbl| for more + * details. + */ + let type_idx: any; + /** + * Value index for tables representing |Float|s. A table representing + * floating-point value 1.0 looks like this: > + * { + * [vim.type_idx] = vim.types.float, + * [vim.val_idx] = 1.0, + * } + * See also |vim.type_idx| and |lua-special-tbl|. + */ + let val_idx: any; + /** + * Table with possible values for |vim.type_idx|. Contains two sets of + * key-value pairs: first maps possible values for |vim.type_idx| to + * human-readable strings, second maps human-readable type names to + * values for |vim.type_idx|. Currently contains pairs for `float`, + * `array` and `dictionary` types. + * + * Note: one must expect that values corresponding to `vim.types.float`, + * `vim.types.array` and `vim.types.dictionary` fall under only two + * following assumptions: + * 1. Value may serve both as a key and as a value in a table. Given the + * properties of Lua tables this basically means “value is not `nil`”. + * 2. For each value in `vim.types` table `vim.types[vim.types[value]]` + * is the same as `value`. + * No other restrictions are put on types, and it is not guaranteed that + * values corresponding to `vim.types.float`, `vim.types.array` and + * `vim.types.dictionary` will not change or that `vim.types` table will + * only contain values for these three types. + */ + const types: any; + const fn: fn; + const api: api; + function tbl_extend( + behavior: "error" | "keep" | "force", + ...tables: any[] + ): any; + const log: { + levels: { + DEBUG: 0; + INFO: 1; + WARN: 2; + ERROR: 3; + TRACE: 4; + OFF: 5; + }; + }; + const keymap: keymap; + const split: ( + s: string, + sep: string, + options?: { plain?: boolean; trimempty?: boolean }, + ) => string[]; +} diff --git a/src/types/highlight.d.ts b/src/types/highlight.d.ts new file mode 100644 index 0000000..d66640a --- /dev/null +++ b/src/types/highlight.d.ts @@ -0,0 +1,15 @@ +/** @noSelf **/ +interface highlight { + /** + * Highlights the yanked text. The fields of the optional dict {opts} + * control the highlight: + * - {higroup} highlight group for yanked region (default |hl-IncSearch|) + * - {timeout} time in ms before highlight is cleared (default `150`) + * - {on_macro} highlight when executing macro (default `false`) + * - {on_visual} highlight when yanking visual selection (default `true`) + * - {event} event structure (default |v:event|) + * + * light.range({bufnr}, {ns}, {higroup}, {start}, {finish}, {rtype}, {inclusive}) + */ + on_yank: (opts?: any) => any; +} diff --git a/src/types/keymap.d.ts b/src/types/keymap.d.ts new file mode 100644 index 0000000..556d270 --- /dev/null +++ b/src/types/keymap.d.ts @@ -0,0 +1,24 @@ +/** @noSelf **/ +interface keymap { + set: ( + this: void, + mode: MapMode | MapMode[], + lhs: string, + rhs: string | (() => any), + options?: MapOptions, + ) => void; +} + +type MapMode = "n" | "v" | "x" | "s" | "o" | "ic" | "i" | "l" | "c" | "t"; + +type MapOptions = { + buffer?: number | boolean; + remap?: boolean; + desc?: string; + callback?: () => any; + nowait?: boolean; + silent?: boolean; + script?: boolean; + expr?: boolean; + unique?: boolean; +}; diff --git a/src/types/lsp.d.ts b/src/types/lsp.d.ts new file mode 100644 index 0000000..7538b40 --- /dev/null +++ b/src/types/lsp.d.ts @@ -0,0 +1,1230 @@ +/** @noSelf **/ +interface lsp { + /** + * Implements the `textDocument/did…` notifications required to track a + * buffer for any language server. + * + * Without calling this, the server won't be notified of changes to a buffer. + * + * @param bufnr - (`integer`) Buffer handle, or 0 for current + * @param client_id - (`integer`) Client id + * + * @returns (`boolean`) success `true` if client was attached successfully; + * `false` otherwise + */ + buf_attach_client: (bufnr?: any, client_id?: any) => any; + /** + * Detaches client from the specified buffer. Note: While the server is + * notified that the text document (buffer) was closed, it is still able to + * send notifications should it ignore this notification. + * + * @param bufnr - (`integer`) Buffer handle, or 0 for current + * @param client_id - (`integer`) Client id + */ + buf_detach_client: (bufnr?: any, client_id?: any) => any; + /** + * Checks if a buffer is attached for a particular client. + * + * @param bufnr - (`integer`) Buffer handle, or 0 for current + * @param client_id - (`integer`) the client id + */ + buf_is_attached: (bufnr?: any, client_id?: any) => any; + /** + * Send a notification to a server + * + * @param bufnr - (`integer?`) The number of the buffer + * @param method - (`string`) Name of the request method + * @param params - (`any`) Arguments to send to the server + * + * @returns (`boolean`) success true if any client returns true; false otherwise + */ + buf_notify: (bufnr?: any, method?: any, params?: any) => any; + /** + * Sends an async request for all active clients attached to the buffer and + * executes the `handler` callback with the combined result. + * + * @param bufnr - (`integer`) Buffer handle, or 0 for current. + * @param method - (`string`) LSP method name + * @param params - (`table?`) Parameters to send to the server + * @param handler - (`function`) Handler called after all requests are + * completed. Server results are passed as a + * `client_id:result` map. + * + * @returns (`function`) cancel Function that cancels all requests. + */ + buf_request_all: ( + bufnr?: any, + method?: any, + params?: any, + handler?: any + ) => any; + /** + * Sends a request to all server and waits for the response of all of them. + * + * Calls |vim.lsp.buf_request_all()| but blocks Nvim while awaiting the + * result. Parameters are the same as |vim.lsp.buf_request_all()| but the + * result is different. Waits a maximum of {timeout_ms}. + * + * @param bufnr - (`integer`) Buffer handle, or 0 for current. + * @param method - (`string`) LSP method name + * @param params - (`table?`) Parameters to send to the server + * @param timeout_ms - (`integer?`, default: `1000`) Maximum time in + * milliseconds to wait for a result. + * + * Return (multiple): ~ + * (`table?`) result Map + * of client_id:request_result. + * (`string?`) err On timeout, cancel, or error, `err` is a string + * describing the failure reason, and `result` is nil. + */ + buf_request_sync: ( + bufnr?: any, + method?: any, + params?: any, + timeout_ms?: any + ) => any; + /** + * Checks whether a client is stopped. + * + * @param client_id - (`integer`) + * + * @returns (`boolean`) stopped true if client is stopped, false otherwise. + */ + client_is_stopped: (client_id?: any) => any; + /** + * Registry for client side commands. This is an extension point for plugins + * to handle custom commands which are not part of the core language server + * protocol specification. + * + * The registry is a table where the key is a unique command name, and the + * value is a function which is called if any LSP action (code action, code + * lenses, ...) triggers the command. + * + * If a LSP response contains a command for which no matching entry is + * available in this registry, the command will be executed via the LSP + * server using `workspace/executeCommand`. + * + * The first argument to the function will be the `Command`: Command title: + * String command: String arguments?: any[] + * + * The second argument is the `ctx` of |lsp-handler| + */ + commands: any; + /** + * Provides an interface between the built-in client and a `formatexpr` + * function. + * + * Currently only supports a single client. This can be set via `setlocal + * formatexpr=v:lua.vim.lsp.formatexpr()` but will typically or in + * `on_attach` via `vim.bo[bufnr].formatexpr = + * 'v:lua.vim.lsp.formatexpr(#{timeout_ms:250})'`. + * + * @param opts - (`table?`) A table with the following fields: + * @param timeout_ms - (`integer`, default: 500ms) The timeout period + * for the formatting request.. + */ + formatexpr: (opts?: any) => any; + /** + * Returns list of buffers attached to client_id. + * + * @param client_id - (`integer`) client id + * + * @returns (`integer[]`) buffers list of buffer ids + */ + get_buffers_by_client_id: (client_id?: any) => any; + /** + * Gets a client by id, or nil if the id is invalid. The returned client may + * not yet be fully initialized. + * + * @param client_id - (`integer`) client id + * + * @returns (`vim.lsp.Client?`) client rpc object + */ + get_client_by_id: (client_id?: any) => any; + /** + * Get active clients. + * + * @param filter - (`table?`) Key-value pairs used to filter the returned + * clients. + * @param id - ? (`integer`) Only return clients with the given id + * @param bufnr - ? (`integer`) Only return clients attached to this + * buffer + * @param name - ? (`string`) Only return clients with the given name + * @param method - ? (`string`) Only return clients supporting the + * given method + * + * @returns (`vim.lsp.Client[]`) List of |vim.lsp.Client| objects + */ + get_clients: (filter?: any) => any; + /** + * Gets the path of the logfile used by the LSP client. + * + * @returns (`string`) path to log file + */ + get_log_path: () => any; + /** + * Implements 'omnifunc' compatible LSP completion. + * + * @param findstart - (`integer`) 0 or 1, decides behavior + * @param base - (`integer`) findstart=0, text to match against + * + * @returns (`integer|table`) Decided by {findstart}: + * • findstart=0: column where the completion starts, or -2 or -3 + * • findstart=1: list of matches (actually just calls |complete()|) + * + * See also: ~ + * • |complete-functions| + * • |complete-items| + * • |CompleteDone| + */ + omnifunc: (findstart?: any, base?: any) => any; + /** + * Sets the global log level for LSP logging. + * + * Levels by name: "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "OFF" + * + * Level numbers begin with "TRACE" at 0 + * + * Use `lsp.log_levels` for reverse lookup. + * + * @param level - (`integer|string`) the case insensitive level name or number + * + * See also: ~ + * • |vim.lsp.log_levels| + */ + set_log_level: (level?: any) => any; + /** + * Create a new LSP client and start a language server or reuses an already + * running client if one is found matching `name` and `root_dir`. Attaches + * the current buffer to the client. + * + * Example: >lua + * vim.lsp.start({ + * name = 'my-server-name', + * cmd = {'name-of-language-server-executable'}, + * root_dir = vim.fs.dirname(vim.fs.find({'pyproject.toml', 'setup.py'}, { upward = true })[1]), + * }) + * + * + * See |vim.lsp.start_client()| for all available options. The most important + * are: + * • `name` arbitrary name for the LSP client. Should be unique per language + * server. + * • `cmd` command string[] or function, described at + * |vim.lsp.start_client()|. + * • `root_dir` path to the project root. By default this is used to decide + * if an existing client should be re-used. The example above uses + * |vim.fs.find()| and |vim.fs.dirname()| to detect the root by traversing + * the file system upwards starting from the current directory until either + * a `pyproject.toml` or `setup.py` file is found. + * • `workspace_folders` list of `{ uri:string, name: string }` tables + * specifying the project root folders used by the language server. If + * `nil` the property is derived from `root_dir` for convenience. + * + * Language servers use this information to discover metadata like the + * dependencies of your project and they tend to index the contents within + * the project folder. + * + * To ensure a language server is only started for languages it can handle, + * make sure to call |vim.lsp.start()| within a |FileType| autocmd. Either + * use |:au|, |nvim_create_autocmd()| or put the call in a + * `ftplugin/.lua` (See |ftplugin-name|) + * + * @param config - (`vim.lsp.ClientConfig`) Configuration for the server. See + * |vim.lsp.ClientConfig|. + * @param opts - (`table?`) Optional keyword arguments + * @param reuse_client - (`fun(client: vim.lsp.Client, config: + * table): boolean`) Predicate used to decide if a client + * should be re-used. Used on all running clients. The + * default implementation re-uses a client if name and + * root_dir matches. + * @param bufnr - (`integer`) Buffer handle to attach to if starting + * or re-using a client (0 for current). + * + * @returns (`integer?`) client_id + */ + start: (config?: any, opts?: any) => any; + /** + * Starts and initializes a client with the given configuration. + * + * @param config - (`vim.lsp.ClientConfig`) Configuration for the server. See + * |vim.lsp.ClientConfig|. + * + * @returns (`integer?`) client_id |vim.lsp.get_client_by_id()| Note: client may + * not be fully initialized. Use `on_init` to do any actions once the + * client has been initialized. + */ + start_client: (config?: any) => any; + /** + * Consumes the latest progress messages from all clients and formats them as + * a string. Empty if there are no clients or if no new messages + * + * @returns (`string`) + */ + status: () => any; + /** + * Stops a client(s). + * + * You can also use the `stop()` function on a |vim.lsp.Client| object. To + * stop all clients: >lua + * vim.lsp.stop_client(vim.lsp.get_clients()) + * + * + * By default asks the server to shutdown, unless stop was requested already + * for this client, then force-shutdown is attempted. + * + * @param client_id - (`integer|vim.lsp.Client`) id or |vim.lsp.Client| object, + * or list thereof + * @param force - (`boolean?`) shutdown forcefully + */ + stop_client: (client_id?: any, force?: any) => any; + /** + * Provides an interface between the built-in client and 'tagfunc'. + * + * When used with normal mode commands (e.g. |CTRL-]|) this will invoke the + * "textDocument/definition" LSP method to find the tag under the cursor. + * Otherwise, uses "workspace/symbol". If no results are returned from any + * LSP servers, falls back to using built-in tags. + * + * @param pattern - (`string`) Pattern used to find a workspace symbol + * @param flags - (`string`) See |tag-function| + * + * @returns (`table[]`) tags A list of matching tags + */ + tagfunc: (pattern?: any, flags?: any) => any; + /** + * Function to manage overriding defaults for LSP handlers. + * + * @param handler - (`lsp.Handler`) See |lsp-handler| + * @param override_config - (`table`) Table containing the keys to override + * @param handler - + */ + with: (handler?: any, override_config?: any) => any; + /** @noSelf **/ + buf: { + /** + * Add the folder at path to the workspace folders. If {path} is not + * provided, the user will be prompted for a path using |input()|. + * + * @param workspace_folder - (`string?`) + */ + add_workspace_folder: (workspace_folder?: any) => any; + /** + * Removes document highlights from current buffer. + */ + clear_references: () => any; + /** + * Selects a code action available at the current cursor position. + * + * @param options - (`table?`) A table with the following fields: + * @param context - ? (`lsp.CodeActionContext`) Corresponds to + * `CodeActionContext` of the LSP specification: + * @param diagnostics - ? (`table`) LSP `Diagnostic[]`. Inferred + * from the current position if not provided. + * @param only - ? (`table`) List of LSP `CodeActionKind`s used to + * filter the code actions. Most language servers support + * values like `refactor` or `quickfix`. + * @param triggerKind - ? (`integer`) The reason why code actions + * were requested. + * @param filter - ? (`fun(x: lsp.CodeAction|lsp.Command):boolean`) + * Predicate taking an `CodeAction` and returning a boolean. + * @param apply - ? (`boolean`) When set to `true`, and there is + * just one remaining action (after filtering), the action + * is applied without user query. + * @param range - ? (`{start: integer[], end: integer[]}`) Range for + * which code actions should be requested. If in visual mode + * this defaults to the active selection. Table must contain + * @param row,col - tuples using + * mark-like indexing. See |api-indexing| + * + * See also: ~ + * • https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_codeAction + * • vim.lsp.protocol.CodeActionTriggerKind + */ + code_action: (options?: any) => any; + /** + * Retrieves the completion items at the current cursor position. Can only be + * called in Insert mode. + * + * @param context - (`table`) (context support not yet implemented) Additional + * information about the context in which a completion was + * triggered (how it was triggered, and by which trigger + * character, if applicable) + * + * See also: ~ + * • vim.lsp.protocol.CompletionTriggerKind + */ + completion: (context?: any) => any; + /** + * Jumps to the declaration of the symbol under the cursor. + * + * Note: ~ + * • Many servers do not implement this method. Generally, see + * |vim.lsp.buf.definition()| instead. + * + * @param options - (`vim.lsp.LocationOpts?`) See |vim.lsp.LocationOpts|. + */ + declaration: (options?: any) => any; + /** + * Jumps to the definition of the symbol under the cursor. + * + * @param options - (`vim.lsp.LocationOpts?`) See |vim.lsp.LocationOpts|. + */ + definition: (options?: any) => any; + /** + * Send request to the server to resolve document highlights for the current + * text document position. This request can be triggered by a key mapping or + * by events such as `CursorHold`, e.g.: >vim + * autocmd CursorHold lua vim.lsp.buf.document_highlight() + * autocmd CursorHoldI lua vim.lsp.buf.document_highlight() + * autocmd CursorMoved lua vim.lsp.buf.clear_references() + * + * + * Note: Usage of |vim.lsp.buf.document_highlight()| requires the following + * highlight groups to be defined or you won't be able to see the actual + * highlights. |hl-LspReferenceText| |hl-LspReferenceRead| + * |hl-LspReferenceWrite| + */ + document_highlight: () => any; + /** + * Lists all symbols in the current buffer in the quickfix window. + * + * @param options - (`vim.lsp.ListOpts?`) See |vim.lsp.ListOpts|. + */ + document_symbol: (options?: any) => any; + /** + * Executes an LSP server command. + * + * @param command_params - (`lsp.ExecuteCommandParams`) + * + * See also: ~ + * • https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_executeCommand + */ + execute_command: (command_params?: any) => any; + /** + * Formats a buffer using the attached (and optionally filtered) language + * server clients. + * + * @param options - (`table?`) A table with the following fields: + * @param formatting_options - ? (`table`) Can be used to specify + * FormattingOptions. Some unspecified options will be + * automatically derived from the current Nvim options. See + * https://microsoft.github.io/language-server-protocol/specification/#formattingOptions + * @param timeout_ms - ? (`integer`, default: `1000`) Time in + * milliseconds to block for formatting requests. No effect + * if async=true. + * @param bufnr - ? (`integer`, default: current buffer) Restrict + * formatting to the clients attached to the given buffer. + * @param filter - ? (`fun(client: vim.lsp.Client): boolean?`) + * Predicate used to filter clients. Receives a client as + * argument and must return a boolean. Clients matching the + * predicate are included. Example: >lua + * -- Never request typescript-language-server for formatting + * vim.lsp.buf.format { + * filter = function(client) return client.name ~= "tsserver" end + * } + * + * • {async}? (`boolean`, default: false) If true the method + * won't block. Editing the buffer while formatting + * asynchronous can lead to unexpected changes. + * • {id}? (`integer`) Restrict formatting to the client with + * ID (client.id) matching this field. + * • {name}? (`string`) Restrict formatting to the client with + * name (client.name) matching this field. + * • {range}? (`{start:integer[],end:integer[]}`, default: + * current selection in visual mode, `nil` in other modes, + * formatting the full buffer) Range to format. Table must + * contain `start` and `end` keys with {row,col} tuples + * using (1,0) indexing. + */ + format: (options?: any) => any; + /** + * Displays hover information about the symbol under the cursor in a floating + * window. Calling the function twice will jump into the floating window. + */ + hover: () => any; + /** + * Lists all the implementations for the symbol under the cursor in the + * quickfix window. + * + * @param options - (`vim.lsp.LocationOpts?`) See |vim.lsp.LocationOpts|. + */ + implementation: (options?: any) => any; + /** + * Lists all the call sites of the symbol under the cursor in the |quickfix| + * window. If the symbol can resolve to multiple items, the user can pick one + * in the |inputlist()|. + */ + incoming_calls: () => any; + /** + * List workspace folders. + */ + list_workspace_folders: () => any; + /** + * Lists all the items that are called by the symbol under the cursor in the + * |quickfix| window. If the symbol can resolve to multiple items, the user + * can pick one in the |inputlist()|. + */ + outgoing_calls: () => any; + /** + * Lists all the references to the symbol under the cursor in the quickfix + * window. + * + * @param context - (`table?`) Context for the request + * @param options - (`vim.lsp.ListOpts?`) See |vim.lsp.ListOpts|. + * + * See also: ~ + * • https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_references + */ + references: (context?: any, options?: any) => any; + /** + * Remove the folder at path from the workspace folders. If {path} is not + * provided, the user will be prompted for a path using |input()|. + * + * @param workspace_folder - (`string?`) + */ + remove_workspace_folder: (workspace_folder?: any) => any; + /** + * Renames all references to the symbol under the cursor. + * + * @param new_name - (`string?`) If not provided, the user will be prompted for + * a new name using |vim.ui.input()|. + * @param options - (`table?`) Additional options: + * @param filter - ? (`fun(client: vim.lsp.Client): boolean?`) + * Predicate used to filter clients. Receives a client as + * argument and must return a boolean. Clients matching the + * predicate are included. + * @param name - ? (`string`) Restrict clients used for rename to + * ones where client.name matches this field. + * @param bufnr - ? (`integer`) (default: current buffer) + */ + rename: (new_name?: any, options?: any) => any; + /** + * Displays signature information about the symbol under the cursor in a + * floating window. + */ + signature_help: () => any; + /** + * Jumps to the definition of the type of the symbol under the cursor. + * + * @param options - (`vim.lsp.LocationOpts?`) See |vim.lsp.LocationOpts|. + */ + type_definition: (options?: any) => any; + /** + * Lists all symbols in the current workspace in the quickfix window. + * + * The list is filtered against {query}; if the argument is omitted from the + * call, the user is prompted to enter a string on the command line. An empty + * string means no filtering is done. + * + * @param query - (`string?`) optional + * @param options - (`vim.lsp.ListOpts?`) See |vim.lsp.ListOpts|. + */ + workspace_symbol: (query?: any, options?: any) => any; + }; + + /** @noSelf **/ + diagnostic: { + /** + * Get the diagnostic namespace associated with an LSP client + * |vim.diagnostic| for diagnostics + * + * @param client_id - (`integer`) The id of the LSP client + * @param is_pull - (`boolean?`) Whether the namespace is for a pull or push + * client. Defaults to push + */ + get_namespace: (client_id?: any, is_pull?: any) => any; + /** + * |lsp-handler| for the method "textDocument/diagnostic" + * + * See |vim.diagnostic.config()| for configuration options. Handler-specific + * configuration can be set using |vim.lsp.with()|: >lua + * vim.lsp.handlers["textDocument/diagnostic"] = vim.lsp.with( + * vim.lsp.diagnostic.on_diagnostic, { + * -- Enable underline, use default values + * underline = true, + * -- Enable virtual text, override spacing to 4 + * virtual_text = { + * spacing = 4, + * }, + * -- Use a function to dynamically turn signs off + * -- and on, using buffer local variables + * signs = function(namespace, bufnr) + * return vim.b[bufnr].show_signs == true + * end, + * -- Disable a feature + * update_in_insert = false, + * } + * ) + * + * + * @param result - (`lsp.DocumentDiagnosticReport`) + * @param ctx - (`lsp.HandlerContext`) + * @param config - (`vim.diagnostic.Opts`) Configuration table (see + * |vim.diagnostic.config()|). + */ + on_diagnostic: (arg__?: any, result?: any, ctx?: any, config?: any) => any; + /** + * |lsp-handler| for the method "textDocument/publishDiagnostics" + * + * See |vim.diagnostic.config()| for configuration options. Handler-specific + * configuration can be set using |vim.lsp.with()|: >lua + * vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with( + * vim.lsp.diagnostic.on_publish_diagnostics, { + * -- Enable underline, use default values + * underline = true, + * -- Enable virtual text, override spacing to 4 + * virtual_text = { + * spacing = 4, + * }, + * -- Use a function to dynamically turn signs off + * -- and on, using buffer local variables + * signs = function(namespace, bufnr) + * return vim.b[bufnr].show_signs == true + * end, + * -- Disable a feature + * update_in_insert = false, + * } + * ) + * + * + * @param result - (`lsp.PublishDiagnosticsParams`) + * @param ctx - (`lsp.HandlerContext`) + * @param config - (`vim.diagnostic.Opts?`) Configuration table (see + * |vim.diagnostic.config()|). + */ + on_publish_diagnostics: ( + arg__?: any, + result?: any, + ctx?: any, + config?: any + ) => any; + }; + + /** @noSelf **/ + handlers: { + /** + * |lsp-handler| for the method "textDocument/hover" >lua + * vim.lsp.handlers["textDocument/hover"] = vim.lsp.with( + * vim.lsp.handlers.hover, { + * -- Use a sharp border with `FloatBorder` highlights + * border = "single", + * -- add the title in hover float window + * title = "hover" + * } + * ) + * + * + * @param result - (`lsp.Hover`) + * @param ctx - (`lsp.HandlerContext`) + * @param config - (`table`) Configuration table. + * • border: (default=nil) + * • Add borders to the floating window + * • See |vim.lsp.util.open_floating_preview()| for more + * options. + */ + hover: (arg__?: any, result?: any, ctx?: any, config?: any) => any; + /** + * |lsp-handler| for the method "textDocument/signatureHelp". + * + * The active parameter is highlighted with |hl-LspSignatureActiveParameter|. >lua + * vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with( + * vim.lsp.handlers.signature_help, { + * -- Use a sharp border with `FloatBorder` highlights + * border = "single" + * } + * ) + * + * + * @param result - (`lsp.SignatureHelp`) Response from the language server + * @param ctx - (`lsp.HandlerContext`) Client context + * @param config - (`table`) Configuration table. + * • border: (default=nil) + * • Add borders to the floating window + * • See |vim.lsp.util.open_floating_preview()| for more + * options + */ + signature_help: (arg__?: any, result?: any, ctx?: any, config?: any) => any; + }; + + /** @noSelf **/ + log: { + /** + * Returns the log filename. + * + * @returns (`string`) log filename + */ + get_filename: () => any; + /** + * Gets the current log level. + * + * @returns (`integer`) current log level + */ + get_level: () => any; + /** + * Sets formatting function used to format logs + * + * @param handle - (`function`) function to apply to logging arguments, pass + * vim.inspect for multi-line formatting + */ + set_format_func: (handle?: any) => any; + /** + * Sets the current log level. + * + * @param level - (`string|integer`) One of `vim.lsp.log.levels` + */ + set_level: (level?: any) => any; + /** + * Checks whether the level is sufficient for logging. + * + * @param level - (`integer`) log level + * + * @returns (`bool`) true if would log, false if not + */ + should_log: (level?: any) => any; + }; + + /** @noSelf **/ + protocol: { + /** + * Gets a new ClientCapabilities object describing the LSP client + * capabilities. + * + * @returns (`lsp.ClientCapabilities`) + */ + make_client_capabilities: () => any; + /** + * LSP method names. + * + * See also: ~ + * • https://microsoft.github.io/language-server-protocol/specification/#metaModel + */ + Methods: any; + /** + * Creates a normalized object describing LSP server capabilities. + * + * @param server_capabilities - (`table`) Table of capabilities supported by + * the server + * + * @returns (`lsp.ServerCapabilities?`) Normalized table of capabilities + * + * + * :tw=78:ts=8:sw=4:sts=4:et:ft=help:norl: + */ + resolve_capabilities: (server_capabilities?: any) => any; + }; + + /** @noSelf **/ + rpc: { + /** + * Create a LSP RPC client factory that connects via TCP to the given host + * and port. + * + * Return a function that can be passed to the `cmd` field for + * |vim.lsp.start_client()| or |vim.lsp.start()|. + * + * @param host - (`string`) host to connect to + * @param port - (`integer`) port to connect to + * + * @returns (`fun(dispatchers: vim.lsp.rpc.Dispatchers): + * vim.lsp.rpc.PublicClient`) + */ + connect: (host?: any, port?: any) => any; + /** + * Create a LSP RPC client factory that connects via named pipes (Windows) or + * unix domain sockets (Unix) to the given pipe_path (file path on Unix and + * name on Windows). + * + * Return a function that can be passed to the `cmd` field for + * |vim.lsp.start_client()| or |vim.lsp.start()|. + * + * @param pipe_path - (`string`) file path of the domain socket (Unix) or name + * of the named pipe (Windows) to connect to + * + * @returns (`fun(dispatchers: vim.lsp.rpc.Dispatchers): + * vim.lsp.rpc.PublicClient`) + */ + domain_socket_connect: (pipe_path?: any) => any; + /** + * Constructs an error message from an LSP error object. + * + * @param err - (`table`) The error object + * + * @returns (`string`) error_message The formatted error message + */ + format_rpc_error: (err?: any) => any; + /** + * Sends a notification to the LSP server. + * + * @param method - (`string`) The invoked LSP method + * @param params - (`table?`) Parameters for the invoked LSP method + * + * @returns (`boolean`) `true` if notification could be sent, `false` if not + */ + notify: (method?: any, params?: any) => any; + /** + * Sends a request to the LSP server and runs {callback} upon response. + * + * @param method - (`string`) The invoked LSP method + * @param params - (`table?`) Parameters for the invoked LSP + * method + * @param callback - (`fun(err: lsp.ResponseError?, result: any)`) + * Callback to invoke + * @param notify_reply_callback - (`fun(message_id: integer)?`) Callback to + * invoke as soon as a request is no longer + * pending + * + * Return (multiple): ~ + * (`boolean`) success `true` if request could be sent, `false` if not + * (`integer?`) message_id if request could be sent, `nil` if not + */ + request: ( + method?: any, + params?: any, + callback?: any, + notify_reply_callback?: any + ) => any; + /** + * Creates an RPC response table `error` to be sent to the LSP response. + * + * @param code - (`integer`) RPC error code defined, see + * `vim.lsp.protocol.ErrorCodes` + * @param message - (`string?`) arbitrary message to send to server + * @param data - (`any?`) arbitrary data to send to server + * + * @returns (`lsp.ResponseError`) + * + * See also: ~ + * • lsp.ErrorCodes See `vim.lsp.protocol.ErrorCodes` + */ + rpc_response_error: (code?: any, message?: any, data?: any) => any; + /** + * Starts an LSP server process and create an LSP RPC client object to + * interact with it. Communication with the spawned process happens via + * stdio. For communication via TCP, spawn a process manually and use + * |vim.lsp.rpc.connect()| + * + * @param cmd - (`string[]`) Command to start the LSP server. + * @param dispatchers - (`table?`) Dispatchers for LSP message types. + * @param notification - (`fun(method: string, params: + * table)`) + * @param server_request - (`fun(method: string, params: + * table): any?, lsp.ResponseError?`) + * @param on_exit - (`fun(code: integer, signal: + * integer)`) + * @param on_error - (`fun(code: integer, err: any)`) + * @param extra_spawn_params - (`table?`) Additional context for the LSP server + * process. + * @param cwd - ? (`string`) Working directory for the + * LSP server process + * @param detached - ? (`boolean`) Detach the LSP server + * process from the current process + * @param env - ? (`table`) Additional + * environment variables for LSP server process. + * See |vim.system()| + * + * @returns (`vim.lsp.rpc.PublicClient?`) Client RPC object, with these methods: + * • `notify()` |vim.lsp.rpc.notify()| + * • `request()` |vim.lsp.rpc.request()| + * • `is_closing()` returns a boolean indicating if the RPC is closing. + * • `terminate()` terminates the RPC client. See + * |vim.lsp.rpc.PublicClient|. + */ + start: (cmd?: any, dispatchers?: any, extra_spawn_params?: any) => any; + }; + + /** @noSelf **/ + util: { + /** + * Applies a `TextDocumentEdit`, which is a list of changes to a single + * document. + * + * @param text_document_edit - (`table`) a `TextDocumentEdit` object + * @param index - (`integer`) Optional index of the edit, if from + * a list of edits (or nil, if not from a list) + * @param offset_encoding - (`string?`) + * + * See also: ~ + * • https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocumentEdit + */ + apply_text_document_edit: ( + text_document_edit?: any, + index?: any, + offset_encoding?: any + ) => any; + /** + * Applies a list of text edits to a buffer. + * + * @param text_edits - (`table`) list of `TextEdit` objects + * @param bufnr - (`integer`) Buffer id + * @param offset_encoding - (`string`) utf-8|utf-16|utf-32 + * + * See also: ~ + * • https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textEdit + */ + apply_text_edits: ( + text_edits?: any, + bufnr?: any, + offset_encoding?: any + ) => any; + /** + * Applies a `WorkspaceEdit`. + * + * @param workspace_edit - (`table`) `WorkspaceEdit` + * @param offset_encoding - (`string`) utf-8|utf-16|utf-32 (required) + * + * See also: ~ + * • https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_applyEdit + */ + apply_workspace_edit: (workspace_edit?: any, offset_encoding?: any) => any; + /** + * Removes document highlights from a buffer. + * + * @param bufnr - (`integer?`) Buffer id + */ + buf_clear_references: (bufnr?: any) => any; + /** + * Shows a list of document highlights for a certain buffer. + * + * @param bufnr - (`integer`) Buffer id + * @param references - (`table`) List of `DocumentHighlight` objects to + * highlight + * @param offset_encoding - (`string`) One of "utf-8", "utf-16", "utf-32". + * + * See also: ~ + * • https://microsoft.github.io/language-server-protocol/specification/#textDocumentContentChangeEvent + */ + buf_highlight_references: ( + bufnr?: any, + references?: any, + offset_encoding?: any + ) => any; + /** + * Returns the UTF-32 and UTF-16 offsets for a position in a certain buffer. + * + * @param buf - (`integer`) buffer number (0 for current) + * @param row - (`integer`) 0-indexed line + * @param col - (`integer`) 0-indexed byte offset in line + * @param offset_encoding - (`string`) utf-8|utf-16|utf-32 defaults to + * `offset_encoding` of first client of `buf` + * + * @returns (`integer`) `offset_encoding` index of the character in line {row} + * column {col} in buffer {buf} + */ + character_offset: ( + buf?: any, + row?: any, + col?: any, + offset_encoding?: any + ) => any; + /** + * Converts any of `MarkedString` | `MarkedString[]` | `MarkupContent` into a + * list of lines containing valid markdown. Useful to populate the hover + * window for `textDocument/hover`, for parsing the result of + * `textDocument/signatureHelp`, and potentially others. + * + * Note that if the input is of type `MarkupContent` and its kind is + * `plaintext`, then the corresponding value is returned without further + * modifications. + * + * @param input - (`lsp.MarkedString|lsp.MarkedString[]|lsp.MarkupContent`) + * @param contents - (`table?`) List of strings to extend with converted lines. + * Defaults to {}. + * + * @returns (`string[]`) extended with lines of converted markdown. + * + * See also: ~ + * • https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_hover + */ + convert_input_to_markdown_lines: (input?: any, contents?: any) => any; + /** + * Converts `textDocument/signatureHelp` response to markdown lines. + * + * @param signature_help - (`table`) Response of `textDocument/SignatureHelp` + * @param ft - (`string?`) filetype that will be use as the `lang` + * for the label markdown code block + * @param triggers - (`table?`) list of trigger characters from the lsp + * server. used to better determine parameter offsets + * + * Return (multiple): ~ + * (`table?`) table list of lines of converted markdown. + * (`table?`) table of active hl + * + * See also: ~ + * • https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_signatureHelp + */ + convert_signature_help_to_markdown_lines: ( + signature_help?: any, + ft?: any, + triggers?: any + ) => any; + /** + * Returns indentation size. + * + * @param bufnr - (`integer?`) Buffer handle, defaults to current + * + * @returns (`integer`) indentation size + * + * See also: ~ + * • 'shiftwidth' + */ + get_effective_tabstop: (bufnr?: any) => any; + /** + * Jumps to a location. + * + * @param location - (`table`) (`Location`|`LocationLink`) + * @param offset_encoding - (`string?`) utf-8|utf-16|utf-32 + * @param reuse_win - (`boolean?`) Jump to existing window if buffer is + * already open. + * + * @returns (`boolean`) `true` if the jump succeeded + */ + jump_to_location: ( + location?: any, + offset_encoding?: any, + reuse_win?: any + ) => any; + /** + * Returns the items with the byte position calculated correctly and in + * sorted order, for display in quickfix and location lists. + * + * The `user_data` field of each resulting item will contain the original + * `Location` or `LocationLink` it was computed from. + * + * The result can be passed to the {list} argument of |setqflist()| or + * |setloclist()|. + * + * @param locations - (`lsp.Location[]|lsp.LocationLink[]`) + * @param offset_encoding - (`string`) offset_encoding for locations + * utf-8|utf-16|utf-32 default to first client of + * buffer + * + * @returns (`table[]`) A list of objects with the following fields: + * • {filename} (`string`) + * • {lnum} (`integer`) 1-indexed line number + * • {col} (`integer`) 1-indexed column + * • {text} (`string`) + * • {user_data} (`lsp.Location|lsp.LocationLink`) + */ + locations_to_items: (locations?: any, offset_encoding?: any) => any; + /** + * Creates a table with sensible default options for a floating window. The + * table can be passed to |nvim_open_win()|. + * + * @param width - (`integer`) window width (in character cells) + * @param height - (`integer`) window height (in character cells) + * @param opts - (`table`) optional + * • offset_x (integer) offset to add to `col` + * • offset_y (integer) offset to add to `row` + * • border (string or table) override `border` + * • focusable (string or table) override `focusable` + * • zindex (string or table) override `zindex`, defaults to 50 + * • relative ("mouse"|"cursor") defaults to "cursor" + * • anchor_bias ("auto"|"above"|"below") defaults to "auto" + * • "auto": place window based on which side of the cursor + * has more lines + * • "above": place the window above the cursor unless there + * are not enough lines to display the full window height. + * • "below": place the window below the cursor unless there + * are not enough lines to display the full window height. + * + * @returns (`table`) Options + */ + make_floating_popup_options: (width?: any, height?: any, opts?: any) => any; + /** + * Creates a `DocumentFormattingParams` object for the current buffer and + * cursor position. + * + * @param options - (`table?`) with valid `FormattingOptions` entries + * + * @returns (`lsp.DocumentFormattingParams`) object + * + * See also: ~ + * • https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_formatting + */ + make_formatting_params: (options?: any) => any; + /** + * Using the given range in the current buffer, creates an object that is + * similar to |vim.lsp.util.make_range_params()|. + * + * @param start_pos - (`integer[]?`) {row,col} mark-indexed position. + * Defaults to the start of the last visual selection. + * @param end_pos - (`integer[]?`) {row,col} mark-indexed position. + * Defaults to the end of the last visual selection. + * @param bufnr - (`integer?`) buffer handle or 0 for current, + * defaults to current + * @param offset_encoding - (`"utf-8"|"utf-16"|"utf-32"?`) defaults to + * `offset_encoding` of first client of `bufnr` + * + * @returns (`table`) { textDocument = { uri = `current_file_uri` }, range = { + * start = `start_position`, end = `end_position` } } + */ + make_given_range_params: ( + start_pos?: any, + end_pos?: any, + bufnr?: any, + offset_encoding?: any + ) => any; + /** + * Creates a `TextDocumentPositionParams` object for the current buffer and + * cursor position. + * + * @param window - (`integer?`) window handle or 0 for current, + * defaults to current + * @param offset_encoding - (`string?`) utf-8|utf-16|utf-32|nil defaults to + * `offset_encoding` of first client of buffer of + * `window` + * + * @returns (`table`) `TextDocumentPositionParams` object + * + * See also: ~ + * • https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocumentPositionParams + */ + make_position_params: (window?: any, offset_encoding?: any) => any; + /** + * Using the current position in the current buffer, creates an object that + * can be used as a building block for several LSP requests, such as + * `textDocument/codeAction`, `textDocument/colorPresentation`, + * `textDocument/rangeFormatting`. + * + * @param window - (`integer?`) window handle or 0 for current, + * defaults to current + * @param offset_encoding - (`"utf-8"|"utf-16"|"utf-32"?`) defaults to + * `offset_encoding` of first client of buffer of + * `window` + * + * @returns (`table`) { textDocument = { uri = `current_file_uri` }, range = { + * start = `current_position`, end = `current_position` } } + */ + make_range_params: (window?: any, offset_encoding?: any) => any; + /** + * Creates a `TextDocumentIdentifier` object for the current buffer. + * + * @param bufnr - (`integer?`) Buffer handle, defaults to current + * + * @returns (`table`) `TextDocumentIdentifier` + * + * See also: ~ + * • https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocumentIdentifier + */ + make_text_document_params: (bufnr?: any) => any; + /** + * Create the workspace params + * + * @param added - (`table`) + * @param removed - (`table`) + */ + make_workspace_params: (added?: any, removed?: any) => any; + /** + * Shows contents in a floating window. + * + * @param contents - (`table`) of lines to show in window + * @param syntax - (`string`) of syntax to set for opened buffer + * @param opts - (`table?`) with optional fields (additional keys are + * filtered with |vim.lsp.util.make_floating_popup_options()| + * before they are passed on to |nvim_open_win()|) + * @param height - ? (`integer`) Height of floating window + * @param width - ? (`integer`) Width of floating window + * @param wrap - ? (`boolean`, default: `true`) Wrap long lines + * @param wrap_at - ? (`integer`) Character to wrap at for + * computing height when wrap is enabled + * @param max_width - ? (`integer`) Maximal width of floating + * window + * @param max_height - ? (`integer`) Maximal height of floating + * window + * @param focus_id - ? (`string`) If a popup with this id is + * opened, then focus it + * @param close_events - ? (`table`) List of events that closes the + * floating window + * @param focusable - ? (`boolean`, default: `true`) Make float + * focusable. + * @param focus - ? (`boolean`, default: `true`) If `true`, and if + * @param focusable - is also `true`, focus an existing floating + * @param focus_id - + * + * Return (multiple): ~ + * (`integer`) bufnr of newly created float window + * (`integer`) winid of newly created float window preview window + */ + open_floating_preview: (contents?: any, syntax?: any, opts?: any) => any; + /** + * Previews a location in a floating window + * + * behavior depends on type of location: + * • for Location, range is shown (e.g., function definition) + * • for LocationLink, targetRange is shown (e.g., body of function + * definition) + * + * @param location - (`table`) a single `Location` or `LocationLink` + * @param opts - (`table`) + * + * Return (multiple): ~ + * (`integer?`) buffer id of float window + * (`integer?`) window id of float window + */ + preview_location: (location?: any, opts?: any) => any; + /** + * Rename old_fname to new_fname + * + * Existing buffers are renamed as well, while maintaining their bufnr. + * + * It deletes existing buffers that conflict with the renamed file name only + * when + * • `opts` requests overwriting; or + * • the conflicting buffers are not loaded, so that deleting thme does not + * result in data loss. + * + * @param old_fname - (`string`) + * @param new_fname - (`string`) + * @param opts - (`table?`) Options: + * @param overwrite - ? (`boolean`) + * @param ignoreIfExists - ? (`boolean`) + */ + rename: (old_fname?: any, new_fname?: any, opts?: any) => any; + /** + * Shows document and optionally jumps to the location. + * + * @param location - (`table`) (`Location`|`LocationLink`) + * @param offset_encoding - (`string?`) utf-8|utf-16|utf-32 + * @param opts - (`table?`) options + * • reuse_win (boolean) Jump to existing window if + * buffer is already open. + * • focus (boolean) Whether to focus/jump to location + * if possible. Defaults to true. + * + * @returns (`boolean`) `true` if succeeded + */ + show_document: (location?: any, offset_encoding?: any, opts?: any) => any; + /** + * Converts markdown into syntax highlighted regions by stripping the code + * blocks and converting them into highlighted code. This will by default + * insert a blank line separator after those code block regions to improve + * readability. + * + * This method configures the given buffer and returns the lines to set. + * + * If you want to open a popup with fancy markdown, use + * `open_floating_preview` instead + * + * @param bufnr - (`integer`) + * @param contents - (`table`) of lines to show in window + * @param opts - (`table`) with optional fields + * • height of floating window + * • width of floating window + * • wrap_at character to wrap at for computing height + * • max_width maximal width of floating window + * • max_height maximal height of floating window + * • separator insert separator after code block + * + * @returns (`table`) stripped content + */ + stylize_markdown: (bufnr?: any, contents?: any, opts?: any) => any; + /** + * Converts symbols to quickfix list items. + * + * @param symbols - (`table`) DocumentSymbol[] or SymbolInformation[] + * @param bufnr - (`integer`) + */ + symbols_to_items: (symbols?: any, bufnr?: any) => any; + }; +} diff --git a/src/types/luassert.d.ts b/src/types/luassert.d.ts new file mode 100644 index 0000000..4d1aaa2 --- /dev/null +++ b/src/types/luassert.d.ts @@ -0,0 +1,46 @@ +/**@noResolution*/ +declare module "luassert.stub" { + function stub(module: any): any; +} + +declare type Stub = (module: any, functionName: string) => any; + +/**@noResolution*/ +declare module "luassert.mock" { + function mock(module: any): any; +} + +declare type Mock = (module: any, useStubs?: boolean) => any; + +/**@noResolution*/ +declare module "luassert.match" { + /**@noSelf*/ + interface Match { + is_same: (value: any) => any; + } + const exports: Match; + export = exports; +} + +/**@noResolution*/ +declare module "luassert.spy" { + /**@noSelf*/ + interface Spy { + new: (fn: () => any) => SpyValue; + on: (table: any, method: string) => any; + } + const exports: Spy; + export = exports; +} + +declare type SpyValue = Record; + +declare namespace assert { + export function stub(module: any): any; + export function spy(spy: SpyValue): any; + const are: any; +} + +declare function describe(description: string, suite: () => void): void; +declare function before_each(fn: () => void): void; +declare function it(description: string, test: () => void): void; diff --git a/src/types/plenary.d.ts b/src/types/plenary.d.ts new file mode 100644 index 0000000..0fcf987 --- /dev/null +++ b/src/types/plenary.d.ts @@ -0,0 +1,19 @@ +/** @noResolution */ +declare module "plenary.curl" { + const put: any; + const post: any; +} +declare interface Job { + start: () => void; + result: () => string[]; +} +/** @noResolution */ +declare module "plenary.job" { + let exports: { + new: (options: any) => Job; + }; + export = exports; +} + +/** @noResolution */ +declare module "plenary.busted" {} diff --git a/src/types/telescope.d.ts b/src/types/telescope.d.ts new file mode 100644 index 0000000..05150d2 --- /dev/null +++ b/src/types/telescope.d.ts @@ -0,0 +1,39 @@ +/** @noResolution */ +declare module "telescope" {} +declare interface Picker { + find: () => void; +} +/** @noResolution */ +declare module "telescope.pickers" { + const exports: Pickers; + export = exports; +} +/** @noSelf **/ +declare interface Pickers { + new: (a: any, b: any) => Picker; +} +/** @noResolution */ +declare module "telescope.finders" { + const new_table: (a: any) => any; +} +/** @noResolution */ +declare module "telescope.previewers" { + const new_buffer_previewer: (a: any) => any; +} + +declare interface SelectionAction { + replace: (a: any) => any; +} +/** @noResolution */ +declare module "telescope.actions" { + const select_default: SelectionAction; + const close: (a: number) => void; +} +/** @noResolution */ +declare module "telescope.actions.state" { + const get_selected_entry: (a: number) => any; +} +/** @noResolution */ +declare module "telescope.config" { + const values: any; +} diff --git a/tests/forem-nvim/setup_spec.fnl b/tests/forem-nvim/setup_spec.fnl deleted file mode 100644 index 812f075..0000000 --- a/tests/forem-nvim/setup_spec.fnl +++ /dev/null @@ -1,38 +0,0 @@ -(local stub (require :luassert.stub)) -(local spy (require :luassert.spy)) - -(describe :Setup - (fn [] - (var forem-nvim nil) - (before_each (fn [] - (set vim.env.FOREM_API_KEY :foo) - (set package.loaded.forem-nvim nil) - (set forem-nvim (require :forem-nvim)) - (forem-nvim.setup))) - (it "should show a notification when no api key is set" - (fn [] - ;; Clear the api key - (set vim.env.FOREM_API_KEY nil) - (stub vim :notify) - (forem-nvim.my_articles) - (let [assert-stub (assert.stub vim.notify)] - (assert-stub.was.called)))) - (it "should call the api to get the articles" - (fn [] - ;; To check if the api is called, we need to mock the api - ;; The first step is to clear the api from the package.loaded table - (set package.loaded.forem-nvim nil) - (tset package.loaded :forem-nvim.api nil) - ;; Then we can mock the api - (local mocked-api (require :forem-nvim.api)) - ;; We need to mock the function that we want to check - (set mocked-api.my-articles (spy.new (fn []))) - ;; Then we can require the package - (local forem-nvim-api-mocked (require :forem-nvim)) - (forem-nvim-api-mocked.setup) - (forem-nvim-api-mocked.my_articles) - ;; Finally we can check if the function was called - (let [api-spy (assert.spy mocked-api.my-articles)] - (api-spy.was.called)) - ;; We need to clear the api from the package.loaded table to reset the state - (tset package.loaded :forem-nvim.api nil))))) diff --git a/tests/forem-nvim/setup_spec.lua b/tests/forem-nvim/setup_spec.lua index 170cb2d..b747e8a 100644 --- a/tests/forem-nvim/setup_spec.lua +++ b/tests/forem-nvim/setup_spec.lua @@ -1,39 +1,80 @@ -local stub = require("luassert.stub") +--[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]] +local ____exports = {} local spy = require("luassert.spy") -local function _1_() - local forem_nvim = nil - local function _2_() - vim.env.FOREM_API_KEY = "foo" - package.loaded["forem-nvim"] = nil - forem_nvim = require("forem-nvim") - return forem_nvim.setup() - end - before_each(_2_) - local function _3_() - vim.env.FOREM_API_KEY = nil - stub(vim, "notify") - forem_nvim.my_articles() - local assert_stub = assert.stub(vim.notify) - return assert_stub.was.called() - end - it("should show a notification when no api key is set", _3_) - local function _4_() - package.loaded["forem-nvim"] = nil - package.loaded["forem-nvim.api"] = nil - local mocked_api = require("forem-nvim.api") - local function _5_() - end - mocked_api["my-articles"] = spy.new(_5_) - local forem_nvim_api_mocked = require("forem-nvim") - forem_nvim_api_mocked.setup() - forem_nvim_api_mocked.my_articles() - do - local api_spy = assert.spy(mocked_api["my-articles"]) - api_spy.was.called() - end - package.loaded["forem-nvim.api"] = nil - return nil - end - return it("should call the api to get the articles", _4_) +local match = require("luassert.match") +local stub = require("luassert.stub") +local mock = require("luassert.mock") +local article = require("forem-nvim.article") +local function mockInternal(module) + _G.package.loaded["forem-nvim"] = nil + _G.package.loaded[module] = nil + local mocked = require(module) + return mocked end -return describe("Setup", _1_) +describe( + "Forem.nvim", + function() + local foremNvim + before_each(function() + vim.env.FOREM_API_KEY = "foo" + _G.package.loaded["forem-nvim"] = nil + foremNvim = require("forem-nvim") + end) + it( + "should show a notification when no api key is set", + function() + vim.env.FOREM_API_KEY = nil + stub(vim, "notify") + foremNvim.my_articles() + assert.stub(vim.notify).was.called() + end + ) + it( + "should call the api to get the articles", + function() + local mockedApi = mockInternal("forem-nvim.api") + mockedApi.myArticles = spy.new(function() + end) + local foremNvimMocked = require("forem-nvim") + foremNvimMocked.my_articles() + assert.spy(mockedApi.myArticles).was.called() + _G.package.loaded["forem-nvim.api"] = nil + end + ) + it( + "should create a new article and open it", + function() + local ____vim_fn_0 = vim.fn + local input = ____vim_fn_0.input + vim.fn.input = spy.on( + {input = function(_prompt) return "Title" end}, + "input" + ) + stub(vim, "cmd") + local mockedApi = mockInternal("forem-nvim.api") + mockedApi.newArticle = spy.on( + {newArticle = function(title) return { + status = 201, + body = { + id = 1, + body_markdown = article.getTemplate(title) + } + } end}, + "newArticle" + ) + local mockedBuffer = mockInternal("forem-nvim.buffer") + mockedBuffer.openMyArticle = spy.new(function() + end) + local foremNvimMocked = require("forem-nvim") + foremNvimMocked.new_article() + assert.spy(mockedBuffer.openMyArticle).was_called_with(match.is_same({ + id = 1, + body_markdown = article.getTemplate("Title") + })) + vim.fn.input = input + _G.package.loaded["forem-nvim.api"] = nil + end + ) + end +) +return ____exports diff --git a/tests/forem-nvim/setup_spec.ts b/tests/forem-nvim/setup_spec.ts new file mode 100644 index 0000000..1c5d02d --- /dev/null +++ b/tests/forem-nvim/setup_spec.ts @@ -0,0 +1,86 @@ +const stub: Stub = require("luassert.stub"); +const mock: Mock = require("luassert.mock"); +import * as spy from "luassert.spy"; +import * as match from "luassert.match"; +const article = require("forem-nvim.article"); + +const mockInternal = (module: string): any => { + // To check if the api is called, we need to mock the api + // The first step is to clear the api from the package.loaded table + _G.package.loaded["forem-nvim"] = undefined; + _G.package.loaded[module] = undefined; + + // Then we mock the api + const mocked = require(module); + return mocked; +}; + +describe("Forem.nvim", () => { + let foremNvim: ForemNvim; + before_each(() => { + vim.env.FOREM_API_KEY = "foo"; + _G.package.loaded["forem-nvim"] = undefined; + foremNvim = require("forem-nvim"); + }); + + it("should show a notification when no api key is set", () => { + vim.env.FOREM_API_KEY = undefined; + stub(vim, "notify"); + foremNvim.my_articles(); + assert.stub(vim.notify).was.called(); + }); + + it("should call the api to get the articles", () => { + // To check if the api is called, we need to mock the api + // The first step is to clear the api from the package.loaded table + // _G.package.loaded["forem-nvim"] = undefined; + // _G.package.loaded["forem-nvim.api"] = undefined; + + // Then we mock the api + // const mockedApi = require("forem-nvim.api"); + // We need to mock the function that we want to check + // mockedApi.myArticles = spy.new(() => {}); + + const mockedApi = mockInternal("forem-nvim.api"); + mockedApi.myArticles = spy.new(() => {}); + // Now we can require the package that will require the mocked api + const foremNvimMocked: ForemNvim = require("forem-nvim"); + foremNvimMocked.my_articles(); + + // Finally we can check if the function was called + assert.spy(mockedApi.myArticles).was.called(); + + _G.package.loaded["forem-nvim.api"] = undefined; + }); + + it("should create a new article and open it", () => { + const { input } = vim.fn; + vim.fn.input = spy.on({ input: (_prompt: string) => "Title" }, "input"); + stub(vim, "cmd"); + + const mockedApi = mockInternal("forem-nvim.api"); + mockedApi.newArticle = spy.on( + { + newArticle: (title: string) => ({ + status: 201, + body: { id: 1, body_markdown: article.getTemplate(title) }, + }), + }, + "newArticle", + ); + const mockedBuffer = mockInternal("forem-nvim.buffer"); + mockedBuffer.openMyArticle = spy.new(() => {}); + + const foremNvimMocked: ForemNvim = require("forem-nvim"); + foremNvimMocked.new_article(); + + assert + .spy(mockedBuffer.openMyArticle) + .was_called_with( + match.is_same({ id: 1, body_markdown: article.getTemplate("Title") }), + ); + + vim.fn.input = input; + _G.package.loaded["forem-nvim.api"] = undefined; + }); +}); diff --git a/tests/minimal_init.fnl b/tests/minimal_init.fnl deleted file mode 100644 index 0d486a1..0000000 --- a/tests/minimal_init.fnl +++ /dev/null @@ -1,20 +0,0 @@ -(λ load-module [module source ?directory] - (local module-dir (or ?directory (.. :/tmp/ module))) - (local is-not-a-directory (= (vim.fn.isdirectory module-dir) 0)) - (when is-not-a-directory - (vim.fn.system [:git :clone source module-dir])) - module-dir) - -(local plenary-dir (or (os.getenv :PLENARY_DIR) :/tmp/plenary.nvim)) -(load-module :plenary.nvim "https://github.com/nvim-lua/plenary.nvim" - plenary-dir) - -(local telescope-dir - (load-module :telescope.nvim - "https://github.com/nvim-telescope/telescope.nvim")) - -(vim.opt.rtp:append ".") -(vim.opt.rtp:append plenary-dir) -(vim.opt.rtp:append telescope-dir) -(vim.cmd "runtime plugin/plenary.vim") -(require :plenary.busted) diff --git a/tests/minimal_init.lua b/tests/minimal_init.lua index 1c5b52c..568e415 100644 --- a/tests/minimal_init.lua +++ b/tests/minimal_init.lua @@ -1,19 +1,20 @@ -local function load_module(module, source, _3fdirectory) - _G.assert((nil ~= source), "Missing argument source on tests/minimal_init.fnl:1") - _G.assert((nil ~= module), "Missing argument module on tests/minimal_init.fnl:1") - local module_dir = (_3fdirectory or ("/tmp/" .. module)) - local is_not_a_directory = (vim.fn.isdirectory(module_dir) == 0) - if is_not_a_directory then - vim.fn.system({"git", "clone", source, module_dir}) - else - end - return module_dir +--[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]] +loadModule = function(module, source, directory) + local moduleDir = directory or "/tmp/" .. module + local directoryExists = vim.fn.isdirectory(moduleDir) + if directoryExists == 0 then + vim.fn.system({"git", "clone", source, moduleDir}) + end + return moduleDir end -local plenary_dir = (os.getenv("PLENARY_DIR") or "/tmp/plenary.nvim") -load_module("plenary.nvim", "https://github.com/nvim-lua/plenary.nvim", plenary_dir) -local telescope_dir = load_module("telescope.nvim", "https://github.com/nvim-telescope/telescope.nvim") -do end (vim.opt.rtp):append(".") -do end (vim.opt.rtp):append(plenary_dir) -do end (vim.opt.rtp):append(telescope_dir) +plenaryDir = os.getenv("PLENARY_DIR") or "/tmp/plenary.nvim" +loadModule("plenary.nvim", "https://github.com/nvim-lua/plenary.nvim", plenaryDir) +telescopeDir = loadModule("telescope.nvim", "https://github.com/nvim-telescope/telescope.nvim") +append = function(option, value) + option.append(option, value) +end +append(vim.opt.rtp, ".") +append(vim.opt.rtp, plenaryDir) +append(vim.opt.rtp, telescopeDir) vim.cmd("runtime plugin/plenary.vim") -return require("plenary.busted") +require("plenary.busted") diff --git a/tests/minimal_init.ts b/tests/minimal_init.ts new file mode 100644 index 0000000..0d009a8 --- /dev/null +++ b/tests/minimal_init.ts @@ -0,0 +1,32 @@ +const loadModule = (module: string, source: string, directory?: string) => { + const moduleDir = directory || `/tmp/${module}`; + const directoryExists = vim.fn.isdirectory(moduleDir); + + if (directoryExists === 0) { + vim.fn.system(["git", "clone", source, moduleDir]); + } + + return moduleDir; +}; + +const plenaryDir = os.getenv("PLENARY_DIR") || "/tmp/plenary.nvim"; +loadModule( + "plenary.nvim", + "https://github.com/nvim-lua/plenary.nvim", + plenaryDir, +); + +const telescopeDir = loadModule( + "telescope.nvim", + "https://github.com/nvim-telescope/telescope.nvim", +); + +const append = (option: any, value: any) => { + option.append(option, value); +}; + +append(vim.opt.rtp, "."); +append(vim.opt.rtp, plenaryDir); +append(vim.opt.rtp, telescopeDir); +vim.cmd("runtime plugin/plenary.vim"); +require("plenary.busted"); diff --git a/tests/tsconfig.json b/tests/tsconfig.json new file mode 100644 index 0000000..398707a --- /dev/null +++ b/tests/tsconfig.json @@ -0,0 +1,20 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "rootDir": ".", + "outDir": "." + }, + "include": [".", "../src/types"], + "exclude": [], + "tstl": { + "noResolvePaths": [ + "plenary.busted", + "forem-nvim", + "forem-nvim.article", + "forem-nvim.api", + "forem-nvim.buffer", + "luassert.stub", + "luassert.mock" + ] + } +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..fd85119 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,20 @@ +{ + "$schema": "https://raw.githubusercontent.com/TypeScriptToLua/TypeScriptToLua/master/tsconfig-schema.json", + "compilerOptions": { + "noImplicitReturns": true, + "noImplicitThis": true, + "target": "ESNext", + "rootDir": "src", + "outDir": "lua", + "lib": ["ESNext"], + "moduleResolution": "Node", + "types": ["lua-types/jit"], + "strict": true, + "noUncheckedIndexedAccess": true + }, + "exclude": ["./plugin", "./tests"], + "tstl": { + "luaTarget": "JIT", + "noImplicitSelf": true + } +}