Skip to content

Commit

Permalink
fixed some issues with hover info
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimaoth committed Mar 10, 2024
1 parent 6a50271 commit 71b4317
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/servers/workspace_server.nim
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ proc translatePath(path: string): Option[string] =
return path.absolutePath.normalizedPath.some

proc callback(req: Request): Future[void] {.async.} =
debug req.reqMethod, " ", req.url
debugf"{req.reqMethod} {req.url}"

let (workspaceName, hostedFolders) = block:
{.gcsafe.}:
Expand Down
15 changes: 8 additions & 7 deletions src/text/language/language_server_lsp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ method getHover*(self: LanguageServerLSP, filename: string, location: Cursor): F

let parsedResponse = response.result

# important: the order of these checks is important
if parsedResponse.contents.asMarkedStringVariantSeq().getSome(markedStrings):
for markedString in markedStrings:
if markedString.asString().getSome(str):
Expand All @@ -186,20 +187,20 @@ method getHover*(self: LanguageServerLSP, filename: string, location: Cursor): F

return string.none

if parsedResponse.contents.asMarkupContent().getSome(markupContent):
return markupContent.value.some

if parsedResponse.contents.asMarkedStringVariant().getSome(markedString):
debugf"marked string variant: {markedString}"
if markedString.asString().getSome(str):
debugf"string: {str}"
return str.some
if markedString.asMarkedStringObject().getSome(str):
debugf"string object lang: {str.language}, value: {str.value}"
return str.value.some

return string.none
if markedString.asString().getSome(str):
debugf"string: {str}"
return str.some

if parsedResponse.contents.asMarkupContent().getSome(markupContent):
debugf"markup content: {markupContent}"
return markupContent.value.some
return string.none

return string.none

Expand Down
2 changes: 2 additions & 0 deletions src/text/language/lsp_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ macro variant(name: untyped, types: varargs[untyped]): untyped =

let procName = ident("as" & typeName.capitalizeAscii)
let ast = genAst(procName, name, t, isSeqLit):

proc procName*(arg: name): Option[t] =
try:
when isSeqLit:
Expand All @@ -29,6 +30,7 @@ macro variant(name: untyped, types: varargs[untyped]): untyped =
return arg.node.jsonTo(t, Joptions(allowMissingKeys: true, allowExtraKeys: false)).some
except CatchableError:
return t.none

proc procName*(arg: Option[name]): Option[t] =
if arg.isSome:
return procName(arg.get)
Expand Down
2 changes: 2 additions & 0 deletions src/text/text_editor.nim
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type TextDocumentEditor* = ref object of DocumentEditor
showHover*: bool # whether to show hover info in ui
hoverText*: string # the text to show in the hover info
hoverLocation*: Cursor # where to show the hover info
hoverScrollOffset*: float # the scroll offset inside the hover window

completionEventHandler: EventHandler
modeEventHandler: EventHandler
Expand Down Expand Up @@ -1523,6 +1524,7 @@ proc showHoverForAsync(self: TextDocumentEditor, cursor: Cursor): Future[void] {
let hoverInfo = await ls.getHover(self.document.fullPath, cursor)
if hoverInfo.getSome(hoverInfo):
self.showHover = true
self.hoverScrollOffset = 0
self.hoverText = hoverInfo
self.hoverLocation = cursor
else:
Expand Down
21 changes: 14 additions & 7 deletions src/ui/widget_builder_text_document.nim
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ proc renderLine*(
self.showHoverForDelayed (line.index, offset)

onEndHover:
# todo: hide after delay so the user has time to hover over the hover window for e.g. scrolling
self.hideHover()

if addBackgroundAsChildren:
Expand Down Expand Up @@ -500,8 +501,9 @@ proc createHover(self: TextDocumentEditor, builder: UINodeBuilder, app: App, cur
let docsColor = app.theme.color("editor.foreground", color(1, 1, 1))
let scopeColor = app.theme.color("string", color(175/255, 1, 175/255))

const numLinesToShow = 1
let (top, bottom) = (cursorBounds.yh.float, cursorBounds.yh.float + totalLineHeight * numLinesToShow)
let numLinesToShow = min(10, self.hoverText.countLines)
let (top, bottom) = (cursorBounds.yh.float, cursorBounds.yh.float + totalLineHeight * numLinesToShow.float)
let height = bottom - top

const docsWidth = 50.0
let totalWidth = charWidth * docsWidth
Expand All @@ -510,13 +512,18 @@ proc createHover(self: TextDocumentEditor, builder: UINodeBuilder, app: App, cur
clampedX = max(builder.root.w - totalWidth, 0)

var hoverPanel: UINode = nil
builder.panel(&{SizeToContentX, SizeToContentY, MaskContent}, x = clampedX, y = top, w = totalWidth, h = bottom - top, pivot = vec2(0, 0), userId = self.hoverId.newPrimaryId):
builder.panel(&{SizeToContentX, MaskContent, FillBackground, DrawBorder, MouseHover}, x = clampedX, y = top, h = height, pivot = vec2(0, 0), backgroundColor = backgroundColor, borderColor = scopeColor, userId = self.hoverId.newPrimaryId):
hoverPanel = currentNode
var textNode: UINode = nil
builder.panel(&{DrawText, FillBackground, DrawBorder, SizeToContentX, SizeToContentY}, x = 1, y = 1, text = self.hoverText, textColor = docsColor, backgroundColor = backgroundColor, borderColor = scopeColor)
textNode = currentNode
currentNode.boundsRaw.w = textNode.bounds.w + 2
currentNode.boundsRaw.h = textNode.bounds.h + 2
# todo: height
builder.panel(&{DrawText, SizeToContentX}, x = 0, y = self.hoverScrollOffset, h = 1000, text = self.hoverText, textColor = docsColor):
textNode = currentNode

onScroll:
let scrollSpeed = app.asConfigProvider.getValue("text.hover-scroll-speed", 20.0)
# todo: clamp bottom
self.hoverScrollOffset = clamp(self.hoverScrollOffset + delta.y * scrollSpeed, -1000, 0)
self.markDirty()

hoverPanel.rawY = cursorBounds.y
hoverPanel.pivot = vec2(0, 1)
Expand Down

0 comments on commit 71b4317

Please sign in to comment.