Skip to content

Commit

Permalink
Reload file on change, notifs on new window, goto end of notifs, fix …
Browse files Browse the repository at this point in the history
…theme on new window
  • Loading branch information
genotrance committed May 11, 2019
1 parent 8f9a364 commit ebc4c30
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 16 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ saveAs <fullpath> - save current buffer to new path
```

Drag and drop of files is supported. The current directory changes to the path of the
currently loaded file if `file:fileChdir` is set to `true`.
currently loaded file if `file:fileChdir` is set to `true`. Files are also automatically
reloaded if they changed while working on another file or application and there
are no unsaved modifications.

Changing directories:
```
Expand Down
38 changes: 31 additions & 7 deletions plugins/server/file.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import deques, os, sequtils, sets, strformat, strutils, tables
import deques, os, sequtils, sets, strformat, strutils, tables, times

import "../.."/src/pluginapi
import "../.."/wrappers/fuzzy
Expand All @@ -10,6 +10,7 @@ type
path: string
docptr: pointer
cursor: int
syncTime: Time
windows: HashSet[int]

Docs = ref object
Expand Down Expand Up @@ -128,7 +129,7 @@ proc switchDoc(plg: var Plugin, docid: int) =
currDoc = plg.getDocId()
currWindow = plg.getCbIntResult("getCurrentWindow", -1)

if docid < 0 or docid > docs.doclist.len-1 or currWindow < 0 or currDoc < 0 or docid == currDoc:
if docid < 0 or docid > docs.doclist.len-1 or currWindow < 0 or currDoc < 0 or (docid == currDoc and docid != 0):
return

docs.doclist[currDoc].cursor = plg.ctx.msg(plg.ctx, SCI_GETCURRENTPOS)
Expand All @@ -155,6 +156,11 @@ proc switchDoc(plg: var Plugin, docid: int) =
else:
docs.dirHistory.peekFirst().setCurrentDir()

if docid == 0:
let
length = plg.ctx.msg(plg.ctx, SCI_GETLENGTH)
discard plg.ctx.msg(plg.ctx, SCI_GOTOPOS, length)

discard plg.ctx.handleCommand(plg.ctx, "runHook postFileSwitch")

proc loadFileContents(plg: var Plugin, path: string) =
Expand Down Expand Up @@ -315,6 +321,7 @@ proc open(plg: var Plugin) {.feudCallback.} =
doc.windows.init()
doc.path = path
doc.docptr = plg.ctx.msg(plg.ctx, SCI_CREATEDOCUMENT, info.size.toPtr).toPtr
doc.syncTime = path.getLastModificationTime()

docs.doclist.add doc

Expand Down Expand Up @@ -351,6 +358,8 @@ proc save(plg: var Plugin) {.feudCallback.} =
f.close()
plg.ctx.notify(plg.ctx, &"Saved {doc.path}")

doc.syncTime = doc.path.getLastModificationTime()

discard plg.ctx.msg(plg.ctx, SCI_SETSAVEPOINT)
discard plg.ctx.handleCommand(plg.ctx, &"setTitle {doc.path}")
except:
Expand Down Expand Up @@ -485,14 +494,14 @@ proc reload(plg: var Plugin) {.feudCallback.} =
var
docs = plg.getDocs()
docid = plg.getDocId()
doc = docs.doclist[docid]

if docid > 0:
let
path = docs.doclist[docid].path
plg.loadFileContents(doc.path)

plg.loadFileContents(path)
doc.syncTime = doc.path.getLastModificationTime()

plg.ctx.notify(plg.ctx, &"Reloaded {path}")
plg.ctx.notify(plg.ctx, &"Reloaded {doc.path}")

proc reloadAll(plg: var Plugin) {.feudCallback.} =
var
Expand All @@ -504,6 +513,18 @@ proc reloadAll(plg: var Plugin) {.feudCallback.} =
plg.next()
plg.reload()

proc reloadIfChanged(plg: var Plugin) {.feudCallback.} =
var
docs = plg.getDocs()
docid = plg.getDocId()
doc = docs.doclist[docid]

if doc.path.fileExists() and doc.syncTime < doc.path.getLastModificationTime():
if plg.ctx.msg(plg.ctx, SCI_GETMODIFY) == 0:
plg.reload()
else:
plg.ctx.notify(plg.ctx, &"File '{doc.path.extractFilename()}' with unsaved modifications changed behind the scenes")

proc cd(plg: var Plugin) {.feudCallback.} =
var
docs = plg.getDocs()
Expand Down Expand Up @@ -558,4 +579,7 @@ feudPluginLoad:
docs.dirHistory = initDeque[string]()
docs.dirHistory.addLast(getCurrentDir())

discard plg.ctx.handleCommand(plg.ctx, "hook preCloseWindow unload")
discard plg.ctx.handleCommand(plg.ctx, "hook preCloseWindow unload")
discard plg.ctx.handleCommand(plg.ctx, "hook onWindowActivate reloadIfChanged")
discard plg.ctx.handleCommand(plg.ctx, "hook postFileSwitch reloadIfChanged")
discard plg.ctx.handleCommand(plg.ctx, "hook postNewWindow open 0")
13 changes: 5 additions & 8 deletions plugins/server/filetype.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os, parsexml, sequtils, streams, strutils, tables, xmltree
import os, parsexml, sequtils, streams, strformat, strutils, tables, xmltree

import "../../src"/pluginapi

Expand Down Expand Up @@ -130,23 +130,20 @@ proc setLexer(plg: var Plugin) {.feudCallback.} =
var
lang = plg.getLang()

plg.ctx.cmdParam = @[]
if not lang.isNil:
if lang.lexer == plg.getLexer():
return
if lang.lexer != plg.getLexer():
plg.ctx.notify(plg.ctx, &"Set language to {lang.name} for '{plg.ctx.cmdParam[0].extractFilename()}'")

plg.resetLexer()

discard plg.ctx.msg(plg.ctx, SCI_SETLEXER, lang.lexer)
for i in 0 .. lang.keywords.len-1:
discard plg.ctx.msg(plg.ctx, SCI_SETKEYWORDS, i, lang.keywords[i].cstring)

plg.ctx.notify(plg.ctx, "Set language to " & lang.name)

plg.ctx.cmdParam = @[lang.lexName]
else:
plg.resetLexer()

plg.ctx.cmdParam = @[]

feudPluginDepends(["window"])

feudPluginLoad:
Expand Down
1 change: 1 addition & 0 deletions plugins/server/window/frame.nim
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ proc frameCallback(hwnd: HWND, msg: UINT, wParam: WPARAM, lParam: LPARAM): LRESU
of WM_ACTIVATE:
hwnd.setFocus()
plg.setCurrentWindow(hwnd)
discard plg.ctx.handleCommand(plg.ctx, "runHook onWindowActivate")
of WM_CREATE:
var
pCreate = cast[ptr CREATESTRUCT](lParam)
Expand Down

0 comments on commit ebc4c30

Please sign in to comment.