LSP document symbols

This commit is contained in:
2026-03-07 21:33:12 -08:00
parent 90e36d8faf
commit 92ced8dcd2
6 changed files with 75 additions and 33 deletions

View File

@@ -29,7 +29,6 @@ pfunc js_castObj : Array (String × JSObject) → JSObject := `(data) => {
return rval
}`
-- need case split
jsonToJObject : Json JSObject
jsonToJObject (JsonInt x) = js_castInt x
jsonToJObject (JsonNull) = js_null
@@ -173,6 +172,36 @@ errorToDiag err =
-- These shouldn't escape
errorToDiag (Postpone fc qn msg) = errorToDiag $ E fc "Postpone \{show qn} \{msg}"
getSymbols : M (List Json)
getSymbols = do
top <- getTop
let defs = listValues top.currentMod.modDefs
putStrLn "scan \{show $ length' defs} defs"
go Nil defs
where
getKind : Def Int
getKind Axiom = 12
getKind (TCon _ _) = 23
getKind (DCon _ _ _ _) = 10
getKind (Fn _) = 12
getKind (PrimTCon _) = 23
getKind (PrimFn _ _ _) = 12
getKind (PrimOp _) = 12
-- highlight where hx!
go : List Json List TopEntry M (List Json)
go acc Nil = pure $ reverse acc
go acc ((MkEntry fc name type def eflags) :: rest) =
let range = fcToRange fc in
let kind = getKind def in
let diag = JsonObj
$ ("name" , JsonStr (name.baseName))
:: ("range", range)
:: ("selectionRange", range)
:: ("kind", JsonInt kind)
-- detail
:: Nil
in go (diag :: acc) rest
getInfos : M (List Json)
getInfos = do
@@ -225,6 +254,24 @@ checkFile fn = unsafePerformIO $ do
modifyIORef state $ [ topContext := top ]
pure $ jsonToJObject $ JsonArray json
docSymbols : String JSObject
docSymbols fn = unsafePerformIO $ do
let (base, modName) = decomposeName fn
st <- readIORef state
putStrLn "Symbols for \{fn}"
if st.baseDir /= base
then resetState base
else pure MkUnit
repo <- lspFileSource
(Right (top, json)) <- (do
mod <- processModule emptyFC repo Nil modName
modifyTop [ currentMod := mod; ops := mod.modOps ]
getSymbols).runM st.topContext
| Left err => do
pure $ jsonToJObject $ JsonNull
modifyIORef state $ [ topContext := top ]
pure $ jsonToJObject $ JsonArray json
compileJS : String JSObject
compileJS fn = unsafePerformIO $ do
let (base, modName) = decomposeName fn
@@ -246,4 +293,4 @@ compileJS fn = unsafePerformIO $ do
#export updateFile checkFile hoverInfo codeActionInfo compileJS
#export updateFile checkFile hoverInfo codeActionInfo compileJS docSymbols