Skip to content

Commit

Permalink
wip: hover support
Browse files Browse the repository at this point in the history
  • Loading branch information
OceanOak committed Nov 8, 2024
1 parent 9ad53da commit 14cd2eb
Show file tree
Hide file tree
Showing 4 changed files with 2,154 additions and 3 deletions.
57 changes: 57 additions & 0 deletions packages/darklang/keywordDesc.dark
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module Darklang =
module KeywordDescription =
type KeywordDescription = { name: String; description: String }

let keywords () : List<KeywordDescription> =
[ KeywordDescription
{ name = "module"
description =
"Used to associate a name with a group of related types, values, and functions, to logically separate it from other code." }
KeywordDescription
{ name = "const"
description = "Keyword to specify a constant literal." }
KeywordDescription
{ name = "let"
description =
"Lets creates a name with an immutable value, and a scope in which that is defined." }
KeywordDescription
{ name = "type"
description = "Used to declare an alias, record, or an enumeration type." }
KeywordDescription
{ name = "of"
description = "of keyword description TODO" }
KeywordDescription
{ name = "with"
description = "with keyword description TODO" }
KeywordDescription
{ name = "if"
description = "Used in conditional expressions." }
KeywordDescription
{ name = "then"
description = "then keyword description TODO" }
KeywordDescription
{ name = "else"
description = "else keyword description TODO" }
KeywordDescription
{ name = "fun"
description = "fun keyword description TODO" }
KeywordDescription
{ name = "match"
description = "match keyword description TODO" }
KeywordDescription
{ name = "when"
description = "when keyword description TODO" }
KeywordDescription
{ name = "List"
description = "List keyword description TODO" }
KeywordDescription
{ name = "Dict"
description = "Dict keyword description TODO" }
KeywordDescription
{ name = "DB"
description = "Db keyword description TODO" } ]

let getKeywordDescription (name: String) : Stdlib.Option.Option<String> =
(keywords ())
|> Stdlib.List.findFirst (fun k -> k.name == name)
|> Stdlib.Option.map (fun k -> k.description)
64 changes: 62 additions & 2 deletions packages/darklang/languageTools/lsp-server/hover.dark
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,74 @@ module Darklang =
//get position
let position = params.position

//TODO: get hover information
/// Find the document in scope
let doc =
state.documentsInScope
|> Stdlib.Dict.get params.textDocument.uri
|> Builtin.unwrap

/// Get the parsed document
let parsed = doc |> (fun doc -> doc.parsed)
let parsedToPT = doc |> (fun doc -> doc.parsedToPT) // TODO: use this

/// Find the node at the hover position, and get the hover information
let info =
match parsed with
| Ok(SourceFile s) ->
let decls =
s.declarations
|> HoverInformation.collectNodeHoverInfoAtPos position []

// First try to find a declaration at the position, then try to find expressions
match decls with
| Some _ -> decls
| None ->
s.exprsToEval
|> Stdlib.List.filterMap (fun e ->
HoverInformation.collectExpressionHoverInfoAtPos e position [])
|> Stdlib.List.head

| _ -> Stdlib.Option.Option.None


/// Get the text, metadata, and description for the hover
let (hoveringOn, metadata, description) =
match info with
| Some i ->
let text =
doc
|> (fun doc -> doc.text)
|> (fun text ->
match HoverInformation.getTextAtRange text i.range with
| Some t -> t
| None -> "")

let metadata =
i.metadata |> Stdlib.Option.withDefault "No metadata available"

let description =
match i.description with
| Some d -> "\n\nDescription: \n\n" ++ d
| None -> "no description available"

(text, metadata, description)

| None -> ("", "No metadata available", "No description available") // TODO: remove this, we shouldn't show hover if there's no info


// Create a hover response with Markdown content
let hover =
LanguageServerProtocol.Hover.Hover
{ contents =
LanguageServerProtocol.MarkupContent
{ kind = LanguageServerProtocol.MarkupKind.Markdown
value = "got a **hover** response \n\n yay!" }
value =
(HoverInformation.createCodeSnippet hoveringOn)
++ "\n\n"
++ metadata
++ "\n\n"
++ description }

range = Stdlib.Option.Option.None }

let response =
Expand Down
Loading

0 comments on commit 14cd2eb

Please sign in to comment.