Skip to content

Commit

Permalink
fixed warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimaoth committed Oct 8, 2023
1 parent a53a1ea commit 76dfa8c
Show file tree
Hide file tree
Showing 26 changed files with 81 additions and 97 deletions.
20 changes: 11 additions & 9 deletions src/absytree.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ else:
static:
echo "Compiling for unknown"

import std/[parseopt, options, os, sets]
import std/[parseopt, options, macros]

import compilation_config, custom_logger, scripting_api

Expand Down Expand Up @@ -115,14 +115,14 @@ var renderedLastFrame = false
proc runApp(): Future[void] {.async.} =
var ed = await newEditor(backend.get, rend)

addTimer 1000, false, proc(fd: AsyncFD): bool =
addTimer 2, false, proc(fd: AsyncFD): bool =
return false

var frameIndex = 0
var frameTime = 0.0

let minPollPerFrameMs = 1.0
let maxPollPerFrameMs = 10.0
let maxPollPerFrameMs = 5.0
var pollBudgetMs = 0.0
while not ed.closeRequested:
defer:
Expand All @@ -135,7 +135,7 @@ proc runApp(): Future[void] {.async.} =
let eventCounter = rend.processEvents()
let eventTime = eventTimer.elapsed.ms

var layoutTime, updateTime, renderTime: float
var updateTime, renderTime: float
block:
let delta = ed.frameTimer.elapsed.ms
ed.frameTimer = startTimer()
Expand Down Expand Up @@ -177,20 +177,22 @@ proc runApp(): Future[void] {.async.} =
poll(2)
else:
try:
pollBudgetMs += max(minPollPerFrameMs, maxPollPerFrameMs - totalTimer.elapsed.ms)
pollBudgetMs += clamp(15 - totalTimer.elapsed.ms, minPollPerFrameMs, maxPollPerFrameMs)
var totalPollTime = 0.0
while pollBudgetMs > maxPollPerFrameMs:
let start = startTimer()
poll(maxPollPerFrameMs.int)
pollBudgetMs -= start.elapsed.ms
totalPollTime = totalPollTime + start.elapsed.ms
except CatchableError:
# log(lvlError, fmt"Failed to poll async dispatcher: {getCurrentExceptionMsg()}: {getCurrentException().getStackTrace()}")
discard
let pollTime = pollTimer.elapsed.ms

let timeToSleep = 8 - totalTimer.elapsed.ms
if timeToSleep > 1:
# debugf"sleep for {timeToSleep.int}ms"
sleep(timeToSleep.int)
# let timeToSleep = 8 - totalTimer.elapsed.ms
# if timeToSleep > 1:
# # debugf"sleep for {timeToSleep.int}ms"
# sleep(timeToSleep.int)

let totalTime = totalTimer.elapsed.ms
if eventCounter > 0 or totalTime > 20:
Expand Down
5 changes: 2 additions & 3 deletions src/absytree_js.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ logCategory "main-js"

logger.enableConsoleLogger()

import std/[strformat, dom, macros, sets]
import util, app, timer, platform/widget_builders, platform/platform, platform/browser_platform, text/text_document, event, theme, custom_async
import std/[strformat, dom, macros]
import util, app, timer, platform/widget_builders, platform/platform, platform/browser_platform, text/text_document, event, custom_async
import language/language_server
from scripting_api import Backend

Expand All @@ -22,7 +22,6 @@ var initializedEditor = false
var hasRequestedRerender = false
var isRenderInProgress = false

var frameTime = 0.0
var frameIndex = 0

var advanceFrame = false
Expand Down
1 change: 0 additions & 1 deletion src/app.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1959,7 +1959,6 @@ proc handleAction(self: App, action: string, arg: string): bool =
return true

template createNimScriptContextConstructorAndGenerateBindings*(): untyped =
import std/[macros, macrocache]
import ast_document, model_document, text/text_editor, selector_popup, lsp_client
when not defined(js):
proc createAddins(): VmAddins =
Expand Down
2 changes: 0 additions & 2 deletions src/array_set.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import std/[sequtils]

func `in`*[T](x: T, xs: openArray[T]): bool = xs.find(x) != -1

func incl*[T](xs: var seq[T], x: T) =
Expand Down
2 changes: 1 addition & 1 deletion src/ast/base_language_wasm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ proc compileRemainingFunctions(self: BaseLanguageWasmCompiler) =
self.compileFunction(function[0], function[1])

proc compileToBinary*(self: BaseLanguageWasmCompiler, node: AstNode): seq[uint8] =
let funcIdx = self.getOrCreateWasmFunc(node, exportName="test".some)
discard self.getOrCreateWasmFunc(node, exportName="test".some)
self.compileRemainingFunctions()

let binary = self.builder.generateBinary()
Expand Down
3 changes: 2 additions & 1 deletion src/ast_document.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2028,7 +2028,8 @@ proc getItemAtPixelPosition(self: AstDocumentEditor, posWindow: Vec2): Option[in
# return index.some

method handleScroll*(self: AstDocumentEditor, scroll: Vec2, mousePosWindow: Vec2) =
let scrollAmount = scroll.y * self.configProvider.getValue("ast.scroll-speed", 20.0)
discard
# let scrollAmount = scroll.y * self.configProvider.getValue("ast.scroll-speed", 20.0)

# todo
# if not self.lastCompletionsWidget.isNil and self.lastCompletionsWidget.lastBounds.contains(mousePosWindow):
Expand Down
2 changes: 1 addition & 1 deletion src/comb_sort.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import std/[algorithm, random]
import std/[algorithm]

export algorithm

Expand Down
2 changes: 1 addition & 1 deletion src/custom_logger.nim
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ proc enableFileLogger*(self: CustomLogger) =
proc enableConsoleLogger*(self: CustomLogger) =
self.consoleLogger = logging.newConsoleLogger(self.levelThreshold, self.fmtStr, flushThreshold=logging.lvlAll).some

let isTerminal = when declared(isatty): isatty(stdout) else: false
let isTerminal {.used.} = when declared(isatty): isatty(stdout) else: false

method log(self: CustomLogger, level: logging.Level, args: varargs[string, `$`]) =
if self.fileLogger.getSome(l):
Expand Down
4 changes: 2 additions & 2 deletions src/language/language_server_nimsuggest.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import std/[strutils, options, json, os, tables, macros, strformat, sugar]
import std/[strutils, options, json, tables, macros, strformat, sugar]
import scripting_api except DocumentEditor, TextDocumentEditor, AstDocumentEditor
import language_server_base, util
import custom_logger, custom_async, async_http_client, websocket
Expand All @@ -7,7 +7,7 @@ import platform/filesystem
logCategory "ls-nimsuggest"

when not defined(js):
import std/[asyncdispatch, osproc, asyncnet, tempfiles]
import std/[asyncdispatch, os, osproc, asyncnet, tempfiles]
else:
type Port* = distinct uint16

Expand Down
2 changes: 1 addition & 1 deletion src/language_server_absytree_commands.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import std/[strutils, options, tables]
import std/[options, tables]
import scripting_api except DocumentEditor, TextDocumentEditor, AstDocumentEditor
import language/language_server_base, util
import custom_logger, custom_async, dispatch_tables
Expand Down
7 changes: 4 additions & 3 deletions src/lsp_client.nim
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import std/[json, strutils, strformat, tables, sets, os, options, macros, uri, sequtils, sugar]
import custom_logger, custom_async
import std/[json, strutils, strformat, macros]
import custom_logger
import scripting/expose

logCategory "lsp"

var logVerbose = false

when not defined(js):
import myjsonutils, util, async_process, lsp_types
import std/[tables, sets, os, options, uri, sequtils, sugar]
import myjsonutils, util, async_process, lsp_types, custom_async

export lsp_types

Expand Down
15 changes: 5 additions & 10 deletions src/platform/browser_platform.nim
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import std/[strformat, tables, dom, unicode, strutils, sugar, json]
import platform, custom_logger, rect_utils, input, event, lrucache, theme, timer
import std/[tables, dom, unicode, strutils, sugar]
import platform, custom_logger, rect_utils, input, event, lrucache, timer
import vmath
import chroma as chroma
import ui/node

when defined(uiNodeDebugData):
import std/json

export platform

type
Expand Down Expand Up @@ -347,8 +350,6 @@ method render*(self: BrowserPlatform) =
self.boundsStack.add rect(vec2(), self.size)
defer: discard self.boundsStack.pop()

var buffer = ""

var element: Element = if self.content.children.len > 0: self.content.children[0].Element else: nil
let wasNil = element.isNil
self.builder.drawNode(self, element, self.builder.root)
Expand All @@ -372,12 +373,6 @@ proc createOrReplaceElement(element: var Element, name: cstring, nameUpper: cstr
element = dif
element.class = "widget"

proc updateRelativePosition(element: var Element, bounds: Rect) =
element.style.left = ($bounds.x.int).cstring
element.style.top = ($bounds.y.int).cstring
element.style.width = ($bounds.w.int).cstring
element.style.height = ($bounds.h.int).cstring

proc myToHtmlHex(c: Color): cstring =
result = "rgba(".cstring
result += round(c.r * 255).int
Expand Down
6 changes: 3 additions & 3 deletions src/platform/gui_platform.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import std/[tables, strutils, options, sets]
import platform, util, platform/filesystem, timer
import platform, util, platform/filesystem
import custom_logger, input, event, monitors, lrucache, id, rect_utils, theme
import chroma, vmath, windy, boxy, boxy/textures, opengl, pixie/[contexts, fonts]
import ui/node
Expand Down Expand Up @@ -43,7 +43,7 @@ type

proc toInput(rune: Rune): int64
proc toInput(button: Button): int64
proc centerWindowOnMonitor(window: Window, monitor: int)
proc centerWindowOnMonitor*(window: Window, monitor: int)
proc getFont*(self: GuiPlatform, font: string, fontSize: float32): Font
proc getFont*(self: GuiPlatform, fontSize: float32, style: set[FontStyle]): Font
proc getFont*(self: GuiPlatform, fontSize: float32, flags: UINodeFlags): Font
Expand Down Expand Up @@ -304,7 +304,7 @@ proc toInput(button: Button): int64 =
of NumpadDivide: ord '/'
else: 0

proc centerWindowOnMonitor(window: Window, monitor: int) =
proc centerWindowOnMonitor*(window: Window, monitor: int) =
let monitorPos = getMonitorRect(monitor)

let left = float(monitorPos.left)
Expand Down
22 changes: 9 additions & 13 deletions src/platform/terminal_platform.nim
Original file line number Diff line number Diff line change
Expand Up @@ -282,19 +282,15 @@ method render*(self: TerminalPlatform) =
if self.builder.root.lastSizeChange == self.builder.frameIndex:
self.redrawEverything = true

let t1 = startTimer()
self.builder.drawNode(self, self.builder.root, force = self.redrawEverything)
let t1Ms = t1.elapsed.ms

# This can fail if the terminal was resized during rendering, but in that case we'll just rerender next frame
let t2 = startTimer()
try:
self.buffer.display()
self.redrawEverything = false
except CatchableError:
log(lvlError, fmt"Failed to display buffer: {getCurrentExceptionMsg()}")
self.redrawEverything = true
let t2Ms = t2.elapsed.ms

proc setForegroundColor(self: TerminalPlatform, color: chroma.Color) =
if self.trueColorSupport:
Expand Down Expand Up @@ -324,17 +320,17 @@ proc fillRect(self: TerminalPlatform, bounds: Rect, color: chroma.Color) =
self.buffer.fillBackground(bounds.x.int, bounds.y.int, bounds.xw.int - 1, bounds.yh.int - 1)
self.buffer.setBackgroundColor(bgNone)

proc drawRect(self: TerminalPlatform, bounds: Rect, color: chroma.Color) =
let mask = if self.masks.len > 0:
self.masks[self.masks.high]
else:
rect(vec2(0, 0), self.size)
# proc drawRect(self: TerminalPlatform, bounds: Rect, color: chroma.Color) =
# let mask = if self.masks.len > 0:
# self.masks[self.masks.high]
# else:
# rect(vec2(0, 0), self.size)

let bounds = bounds and mask
# let bounds = bounds and mask

self.setBackgroundColor(color)
self.buffer.drawRect(bounds.x.int, bounds.y.int, bounds.xw.int - 1, bounds.yh.int - 1)
self.buffer.setBackgroundColor(bgNone)
# self.setBackgroundColor(color)
# self.buffer.drawRect(bounds.x.int, bounds.y.int, bounds.xw.int - 1, bounds.yh.int - 1)
# self.buffer.setBackgroundColor(bgNone)

proc writeLine(self: TerminalPlatform, pos: Vec2, text: string) =
let mask = if self.masks.len > 0:
Expand Down
57 changes: 28 additions & 29 deletions src/platform/widget_builder_ast_document.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import std/[strformat, tables, sugar]
import util, app, app_interface, config_provider, document_editor, ast_document, ast, node_layout, compiler, text/text_document, custom_logger, platform, theme
import util, app, config_provider, document_editor, ast_document, ast, node_layout, compiler, text/text_document, custom_logger, platform, theme
import widget_builders_base, widget_library, ui/node
import vmath, bumpy, chroma

Expand Down Expand Up @@ -36,42 +36,42 @@ import vmath, bumpy, chroma
# typeWidget.anchor.max.x = 1
# widget.add(typeWidget)

proc updateBaseIndexAndScrollOffset(self: AstDocumentEditor, app: App, height: float) =
let totalLineHeight = app.platform.totalLineHeight
self.previousBaseIndex = self.previousBaseIndex.clamp(0..self.document.rootNode.len)
# proc updateBaseIndexAndScrollOffset(self: AstDocumentEditor, app: App, height: float) =
# let totalLineHeight = app.platform.totalLineHeight
# self.previousBaseIndex = self.previousBaseIndex.clamp(0..self.document.rootNode.len)

let selectedNodeId = self.node.id
# let selectedNodeId = self.node.id

var replacements = initTable[Id, VisualNode]()
# var replacements = initTable[Id, VisualNode]()

let indent = getOption[float32](app, "ast.indent", 20)
let inlineBlocks = getOption[bool](app, "ast.inline-blocks", false)
let verticalDivision = getOption[bool](app, "ast.vertical-division", false)
# let indent = getOption[float32](app, "ast.indent", 20)
# let inlineBlocks = getOption[bool](app, "ast.inline-blocks", false)
# let verticalDivision = getOption[bool](app, "ast.vertical-division", false)

# Adjust scroll offset and base index so that the first node on screen is the base
while self.scrollOffset < 0 and self.previousBaseIndex + 1 < self.document.rootNode.len:
let input = ctx.getOrCreateNodeLayoutInput NodeLayoutInput(node: self.document.rootNode[self.previousBaseIndex], selectedNode: selectedNodeId, replacements: replacements, revision: config.revision, measureText: (t) => self.app.platform.measureText(t), indent: indent, renderDivisionVertically: verticalDivision, inlineBlocks: inlineBlocks)
let layout = ctx.computeNodeLayout(input)
# # Adjust scroll offset and base index so that the first node on screen is the base
# while self.scrollOffset < 0 and self.previousBaseIndex + 1 < self.document.rootNode.len:
# let input = ctx.getOrCreateNodeLayoutInput NodeLayoutInput(node: self.document.rootNode[self.previousBaseIndex], selectedNode: selectedNodeId, replacements: replacements, revision: config.revision, measureText: (t) => self.app.platform.measureText(t), indent: indent, renderDivisionVertically: verticalDivision, inlineBlocks: inlineBlocks)
# let layout = ctx.computeNodeLayout(input)

if self.scrollOffset + layout.bounds.h + totalLineHeight >= height:
break
# if self.scrollOffset + layout.bounds.h + totalLineHeight >= height:
# break

self.previousBaseIndex += 1
self.scrollOffset += layout.bounds.h + totalLineHeight
# self.previousBaseIndex += 1
# self.scrollOffset += layout.bounds.h + totalLineHeight

# Adjust scroll offset and base index so that the first node on screen is the base
while self.scrollOffset > height and self.previousBaseIndex > 0:
let input = ctx.getOrCreateNodeLayoutInput NodeLayoutInput(node: self.document.rootNode[self.previousBaseIndex - 1], selectedNode: selectedNodeId, replacements: replacements, revision: config.revision, measureText: (t) => self.app.platform.measureText(t), indent: indent, renderDivisionVertically: verticalDivision, inlineBlocks: inlineBlocks)
let layout = ctx.computeNodeLayout(input)
# # Adjust scroll offset and base index so that the first node on screen is the base
# while self.scrollOffset > height and self.previousBaseIndex > 0:
# let input = ctx.getOrCreateNodeLayoutInput NodeLayoutInput(node: self.document.rootNode[self.previousBaseIndex - 1], selectedNode: selectedNodeId, replacements: replacements, revision: config.revision, measureText: (t) => self.app.platform.measureText(t), indent: indent, renderDivisionVertically: verticalDivision, inlineBlocks: inlineBlocks)
# let layout = ctx.computeNodeLayout(input)

if self.scrollOffset - layout.bounds.h <= 0:
break
# if self.scrollOffset - layout.bounds.h <= 0:
# break

self.previousBaseIndex -= 1
self.scrollOffset -= layout.bounds.h + totalLineHeight
# self.previousBaseIndex -= 1
# self.scrollOffset -= layout.bounds.h + totalLineHeight

proc renderVisualNode*(self: AstDocumentEditor, builder: UINodeBuilder, app: App, node: VisualNode, selected: AstNode, bounds: Rect, offset: Vec2) =
let charWidth = app.platform.charWidth
# let charWidth = app.platform.charWidth

# echo "renderVisualNode ", node

Expand Down Expand Up @@ -148,8 +148,8 @@ proc renderVisualNode*(self: AstDocumentEditor, builder: UINodeBuilder, app: App
# widget.insert(0, panel)

proc renderVisualNodeLayout*(self: AstDocumentEditor, builder: UINodeBuilder, app: App, node: AstNode, bounds: Rect, layout: NodeLayout, offset: Vec2, y: float) =
let totalLineHeight = app.platform.totalLineHeight
let charWidth = app.platform.charWidth
# let totalLineHeight = app.platform.totalLineHeight
# let charWidth = app.platform.charWidth

self.lastLayouts.add (layout, offset)

Expand Down Expand Up @@ -375,7 +375,6 @@ proc createAstUI*(self: AstDocumentEditor, builder: UINodeBuilder, app: App, con

var selectedNode = self.node

let totalLineHeight = builder.textHeight
let indent = getOption[float32](app, "ast.indent", 20)
let inlineBlocks = getOption[bool](app, "ast.inline-blocks", false)
let verticalDivision = getOption[bool](app, "ast.vertical-division", false)
Expand Down
2 changes: 1 addition & 1 deletion src/platform/widget_builder_model_document.nim
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ proc createCompletions(self: ModelDocumentEditor, builder: UINodeBuilder, app: A

let backgroundColor = app.theme.color("panel.background", color(30/255, 30/255, 30/255))
let selectedBackgroundColor = app.theme.color("list.activeSelectionBackground", color(200/255, 200/255, 200/255))
let docsColor = app.theme.color("editor.foreground", color(1, 1, 1))
# let docsColor = app.theme.color("editor.foreground", color(1, 1, 1))
let nameColor = app.theme.tokenColor(@["entity.name.label", "entity.name"], color(1, 1, 1))
let scopeColor = app.theme.color("string", color(175/255, 1, 175/255))

Expand Down
Loading

0 comments on commit 76dfa8c

Please sign in to comment.