Skip to content

Commit

Permalink
Merge pull request #36 from CFiggers:dev
Browse files Browse the repository at this point in the history
v0.0.10
  • Loading branch information
CFiggers authored Dec 24, 2024
2 parents 78471ef + d499073 commit 9cb0a6e
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 28 deletions.
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,31 @@
All notable changes to this project will be documented in this file.
Format for entires is <version-string> - release date.

## 0.0.10 - 2024-12-22

- Logging
- Rotate log files and overwrite eventually to avoid indefinite log file size
- Adjusted some log levels
- New methods
- `enableDebug` and `disableDebug` - Allow clients to set `(dyn :debug)` while running
- `setLogLevel` and `setLogToFileLevel` - Allow clients to change debug level to console and file

## 0.0.9 - 2024-12-07

- Bugfixes
- Decode percent encoding in URIs before saving to or lookup from `state`
- Typo: ":documnts" rather than ":documents", causing redundant keys in `state` when diagnostics are pull (vs push)
- Don't exit loop when handle-message returns an `:error` result, instead report it and reenter loop gracefully
- Misc
- Formatting tweaks
- New "janet/tellJoke" method (testing for future custom LSP RPC calls)

## 0.0.8 - 2024-11-24

- Bug Fixes
- Additional jpm defs (by @strangepete)
- New `eval-env`s should set `*out*` to `stderr`

## 0.0.7 - 2024-08-11

- Core loop
Expand Down
2 changes: 1 addition & 1 deletion project.janet
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(declare-project
:name "janet-lsp"
:description "A Language Server (LSP) for the Janet Programming Language"
:version "0.0.6"
:version "0.0.10"
:dependencies ["https://github.com/janet-lang/spork.git"
"https://github.com/CFiggers/jayson.git"
"https://github.com/ianthehenry/judge.git"
Expand Down
4 changes: 2 additions & 2 deletions src/doc.janet
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@
"Get the documentation for a symbol in a given environment."
[sym env]
(assert env "my-doc*: env is nil")
(logging/info (string/format "env is: %m" env) [:hover] 1)
(logging/info (string/format "my-doc* tried: %m" (env sym)) [:hover] 1)
(logging/info (string/format "env is: %m" env) [:hover] 3)
(logging/info (string/format "my-doc* tried: %m" (env sym)) [:hover] 3)
(if-let [x (env sym)]
(make-module-entry x)
(if (has-value? '[break def do fn if quasiquote quote
Expand Down
4 changes: 2 additions & 2 deletions src/eval.janet
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

(defn eval-buffer [str &opt filename]
(logging/info (string/format "`eval-buffer` received filename: `%s`" (or filename "none")) [:evaluation] 1)
(logging/info (string/format "`eval-buffer` received str: `%s`" str) [:evaluation] 2)
(logging/info (string/format "`eval-buffer` received str: `%s`" str) [:evaluation] 3)

(default filename "eval.janet")
(var state (string str))
Expand Down Expand Up @@ -92,7 +92,7 @@
:location [0 0]})))
returnval) :e fresh-env))
(def eval-fiber-return (resume eval-fiber))
(logging/info (string/format "`eval-buffer` is returning: %m" eval-fiber-return) [:evaluation] 2)
(logging/info (string/format "`eval-buffer` is returning: %m" eval-fiber-return) [:evaluation] 3)
[eval-fiber-return fresh-env])

# tests
Expand Down
52 changes: 38 additions & 14 deletions src/logging.janet
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
(import spork/rpc)

(defn log [output categories &opt level]

(unless (dyn :debug) (break))

(unless (dyn :client)
Expand All @@ -18,15 +17,41 @@
(print (:print (dyn :client) output)))

(when (dyn :debug)
# Always log to file
(try (spit "janetlsp.log.txt" (string output "\n") :a)
([_]))

# Ensure log file exists
(unless (os/stat "janetlsp.log")
(spit "janetlsp.log" ""))

# Log to file, all categories but only if this log's level is <= the specified level
(when (or (nil? level) # There is no level specified on this log
(<= level (dyn :log-to-file-level)))
(try
(do
(def logfiles (filter |(string/has-prefix? "janetlsp.log" $) (os/dir ".")))
(when (> (get (os/stat "janetlsp.log") :size) 5000000)
(when (and (has-value? logfiles "janetlsp.log") (has-value? logfiles "janetlsp.log.1"))
(when (and (has-value? logfiles "janetlsp.log.1") (has-value? logfiles "janetlsp.log.2"))
(when (and (has-value? logfiles "janetlsp.log.2") (has-value? logfiles "janetlsp.log.3"))
(when (and (has-value? logfiles "janetlsp.log.3") (has-value? logfiles "janetlsp.log.4"))
(when (and (has-value? logfiles "janetlsp.log.4") (has-value? logfiles "janetlsp.log.5"))
(os/rm "janetlsp.log.5"))
(os/rename "janetlsp.log.4" "janetlsp.log.5"))
(os/rename "janetlsp.log.3" "janetlsp.log.4"))
(os/rename "janetlsp.log.2" "janetlsp.log.3"))
(os/rename "janetlsp.log.1" "janetlsp.log.2"))
(os/rename "janetlsp.log" "janetlsp.log.1")
(spit "janetlsp.log" ""))
(spit "janetlsp.log" (string output "\n") :a))
([e]
(file/write stderr (string/format "error while trying to write to log file: %q\n" e)))))

# Log to console, only specified categories and if this log's level is >= the specified level
(when (and
# Category Match
# Category Match
(or (empty? (dyn :log-categories)) # No log categories are specified
(empty? categories) # OR, this log doesn't specify a categories (default to sending it)
(any? (map |(has-value? (dyn :log-categories) $) categories))) # Any of this log's categories is in the target categories
# Level Match
# Level Match
(or (nil? level) # There is no level specified on this log
(<= level (dyn :log-level)))) # OR, this log's level is <= the specified level

Expand All @@ -40,7 +65,6 @@
(nil? categories)
(has-value? (dyn :log-categories) categories)))


(eprintf "is level nil? %m" (nil? level))
(eprintf "is level high enough? %m" (<= level (dyn :log-level)))
(eprintf "second condition %m" (or (nil? level)
Expand All @@ -51,12 +75,12 @@

(defmacro info [output categories &opt level id]
(with-syms [$output $categories $level $id]
~(let [,$output (case (type ,output) :string ,output (string/format "%m" ,output))
,$categories ,categories
,$level ,level
,$id ,id]
(,log (string/format "[INFO%s:%s] %s" (if ,$id (string ":" ,$id) "") (first ,$categories) ,$output)
,$categories ,$level))))
~(let [,$output (case (type ,output) :string ,output (string/format "%m" ,output))
,$categories ,categories
,$level ,level
,$id ,id]
(,log (string/format "[INFO%s:%s] %s" (if ,$id (string ":" ,$id) "") (first ,$categories) ,$output)
,$categories ,$level))))

(defmacro message [output categories &opt level id]
(with-syms [$output $categories $level $id]
Expand All @@ -65,7 +89,7 @@
,$level ,level
,$id ,id]
(,log (string/format "[MESSAGE%s:%s] %s" (if ,$id (string ":" ,$id) "") (first ,$categories) ,$output)
,$categories ,$level))))
,$categories ,$level))))

(defmacro err [output categories &opt level id]
(with-syms [$output $categories $level $id]
Expand Down
52 changes: 43 additions & 9 deletions src/main.janet
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

(use judge)

(def version "0.0.9")
(def version "0.0.10")
(def commit
(with [proc (os/spawn ["git" "rev-parse" "--short" "HEAD"] :xp {:out :pipe})]
(let [[out] (ev/gather
Expand All @@ -38,7 +38,7 @@
(if (string/has-prefix? "file:" uri)
(string/slice uri 5) uri)))]

(logging/info (string/format "`eval-buffer` returned: %m" diagnostics) [:evaluation])
(logging/info (string/format "`eval-buffer` returned: %m" diagnostics) [:evaluation] 3)

(each res diagnostics
(match res
Expand All @@ -49,8 +49,8 @@
:end {:line (max 0 (dec line)) :character col}}
:message message})))

(logging/info (string/format "`run-diagnostics` is returning these errors: %m" items) [:evaluation])
(logging/info (string/format "`run-diagnostics` is returning this eval-context: %m" env) [:evaluation] 1)
(logging/info (string/format "`run-diagnostics` is returning these errors: %m" items) [:evaluation] 2)
(logging/info (string/format "`run-diagnostics` is returning this eval-context: %m" env) [:evaluation] 3)
[items env]))

(def uri-percent-encoding-peg
Expand Down Expand Up @@ -154,7 +154,7 @@
~(let [,$name ,name
,$eval-env ,eval-env
s (get-in ,$eval-env [,$name :value] ,$name)]
(,logging/log (string/format "binding-to-lsp-item: s is %m" s) [:completion] 2)
(,logging/log (string/format "binding-to-lsp-item: s is %m" s) [:completion] 3)
{:label ,$name :kind
(case (type s)
:symbol 12 :boolean 6
Expand Down Expand Up @@ -343,12 +343,40 @@
[:noresponse state])

(defn on-janet-tell-joke [state params]
# (eprint "What's brown and sticky? A stick!")
(let [message {:question "What's brown and sticky?"
:answer "A stick!"}]
(logging/message message [:joke])
[:ok state message]))

(defn on-enable-debug [state params]
(let [message {:message "Enabled :debug"}]
(setdyn :debug true)
(try (spit "janetlsp.log" "")
([_] (logging/err "Tried to write to janetlsp.log, but couldn't" [:core])))
(logging/message message [:debug])
[:ok state message]))

(defn on-disable-debug [state params]
(let [message {:message "Disabled :debug"}]
(setdyn :debug false)
(setdyn :log-level 2)
(logging/message message [:debug])
[:ok state message]))

(defn do-set-log-level [state params kind]
(let [new-level-string (params "level")
new-level ({"off" 0 "messages" 1 "verbose" 2 "veryverbose" 3} new-level-string)
message {:message (string/format "Set %s to %s" kind new-level-string)}]
(logging/message message [:loglevel])
(setdyn kind new-level)
[:noresponse state]))

(defmacro on-set-log-level [state params]
~(,do-set-log-level ,state ,params :log-level))

(defmacro on-set-file-log-level [state params]
~(,do-set-log-level ,state ,params :log-to-file-level))

(defn handle-message [message state]
(let [id (get message "id")
method (get message "method")
Expand All @@ -370,6 +398,10 @@
"textDocument/definition" (on-document-definition state params)
"janet/serverInfo" (on-janet-serverinfo state params)
"janet/tellJoke" (on-janet-tell-joke state params)
"enableDebug" (on-enable-debug state params)
"disableDebug" (on-disable-debug state params)
"setLogLevel" (on-set-log-level state params)
"setLogToFileLevel" (on-set-file-log-level state params)
"shutdown" (on-shutdown state params)
"exit" (on-exit state params)
"$/setTrace" (on-set-trace state params)
Expand Down Expand Up @@ -446,7 +478,7 @@
(defn start-language-server []
(print "Starting LSP " version "-" commit)
(when (dyn :debug)
(try (spit "janetlsp.log.txt" "")
(try (spit "janetlsp.log" "")
([_] (logging/err "Tried to write to janetlsp.log txt, but couldn't" [:core]))))

(merge-module root-env jpm-defs nil true)
Expand Down Expand Up @@ -488,7 +520,8 @@
[[--dont-search-jpm-tree -j] (flag) "Whether to search `jpm_tree` for modules."
--stdio (flag) "Use STDIO."
[--debug -d] (flag) "Print debug messages."
[--log-level -l] (optional :int++ 0) "What level of logging to display. Defaults to 0."
[--log-level -l] (optional :int++ 1) "What level of logging to display. Defaults to 1."
[--log-to-file-level -f] (optional :int++ 2) "What level of logging to write to the log file. Defaults to 2."
[--log-category -L] (tuple :string) "Enable logging by category. For multiple categories, repeat the flag."
[--console -c] (flag) "Start a debug console instead of starting the Language Server."
[--debug-port -p] (optional :int++) "What port to start or connect to the debug console on. Defaults to 8037."]
Expand All @@ -505,7 +538,8 @@
(setdyn :opts opts)
(when debug (setdyn :debug true)) #(setdyn :debug true)
(setdyn :log-level log-level) #(setdyn :log-level 2)
(setdyn :log-categories @[:core ;(map keyword log-category)]) #(setdyn :log-categories [:core :priority :diagnostics])
(setdyn :log-to-file-level log-to-file-level) #(setdyn :log-level 3)
(setdyn :log-categories @[:core ;(map keyword log-category)]) #(setdyn :log-categories [:core :priority :loglevel])
(setdyn :out stderr)
(put root-env :out stderr)

Expand Down

0 comments on commit 9cb0a6e

Please sign in to comment.