Skip to content

Commit

Permalink
scintilla 4.15 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko committed May 29, 2019
1 parent 05d8131 commit 3de8a8c
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 147 deletions.
51 changes: 2 additions & 49 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,55 +1,8 @@
# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows shortcuts
*.lnk

# =========================
# Operating System Files
# =========================

# OSX
# =========================

.DS_Store
.AppleDouble
.LSOverride

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk


release/
nimcache/
master/
template/
winapi/
scintilla/
*.exe
*.dll
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ how to test(version below 7.6):
* put nppnim.xml in NPPINSTDIR\plugins\config

how to test(version >= 7.6):
* put nppnim.dll in NPPINSTDIR\plugins\nppnim32
* put nppnim.dll in NPPINSTDIR\plugins\nppnim
* put nppnim.xml in NPPINSTDIR\plugins\config

beware:
Expand Down
18 changes: 1 addition & 17 deletions lexaccessor.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ const
slopSize = bufferSize div 8

type
EncodingType* = enum
enc8bit, encUnicode, encDBCS

WSType* = enum
wsSpace, wsTab, wsSpaceTab, wsInconsistent

Expand All @@ -29,8 +26,6 @@ type
buf: array[0..bufferSize, char]
startPos: int
endPos: int
codePage: int
encodingType: EncodingType
lenDoc: int
styleBuf: array[0..bufferSize, char]
validLen: int
Expand All @@ -42,8 +37,6 @@ proc initLexAccessor*(pAccess: IDocument): LexAccessor =
result.pAccess = pAccess
result.startPos = extremePosition
result.endPos = 0
result.codePage = pAccess.nvCodePage()
result.encodingType = enc8bit
result.lenDoc = pAccess.nvLength()
result.validLen = 0
result.startSeg = 0
Expand All @@ -52,12 +45,6 @@ proc initLexAccessor*(pAccess: IDocument): LexAccessor =
result.buf[0] = chr(0)
result.styleBuf[0] = chr(0)

case result.codePage
of 65001: result.encodingType = encUnicode
of 932, 936, 949, 950, 1361:
result.encodingType = encDBCS
else: result.encodingType = enc8bit

proc fill*(L: var LexAccessor, pos: int) =
L.startPos = pos - slopSize
if (L.startPos + bufferSize) > L.lenDoc:
Expand All @@ -81,9 +68,6 @@ proc safeGetCharAt*(L: var LexAccessor, pos: int, chDefault = chr(0)): char =
return chDefault
result = L.buf[pos - L.startPos]

proc encoding*(L: LexAccessor): EncodingType =
result = L.encodingType

proc match*(L: var LexAccessor, pos: int, s: cstring): bool =
var i = 0
while s[i] != chr(0):
Expand Down Expand Up @@ -132,7 +116,7 @@ proc setLineState*(L: LexAccessor, line: int, state: int) =

#Style setting
proc startAt*(L: var LexAccessor, start: int) =
L.pAccess.nvStartStyling(start, '\xFF')
L.pAccess.nvStartStyling(start)
L.startPosStyling = start

proc getStartSegment*(L: LexAccessor): int =
Expand Down
154 changes: 138 additions & 16 deletions nppnim.nim
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ proc getSciHandle(): SciHandle =
#sci.addText("Hello, Notepad++!")

proc helloDlg() {.cdecl.} =
discard messageBox(NULL, "Copyright(c) 2016, Andri Lim\nhttps://github.com/jangko/nppnim", "About", MB_OK)
discard messageBox(NULL, "Copyright(c) 2016-2019, Andri Lim\nhttps://github.com/jangko/nppnim", "About", MB_OK)

# Initialization of your plugin commands
# You should fill your plugins commands here
Expand Down Expand Up @@ -98,7 +98,7 @@ proc setInfo(nd: NppDataCopy) {.cdecl, exportc, dynlib.} =
commandMenuInit()

let
pluginName = WC("Nim Lexer")
pluginName = WC("Npp Nim")

proc getName(): ptr TCHAR {.cdecl, exportc, dynlib.} =
when defined(winUnicode):
Expand Down Expand Up @@ -146,28 +146,79 @@ const
NIM_DOC_BLOCK_COMMENT = 17
NIM_DOC_COMMENT = 18

let emptyString = "\0"
let lcName = [
"NIM_DEFAULT\0",
"NIM_KEYWORD\0",
"NIM_LINE_COMMENT\0",
"NIM_TYPE\0",
"NIM_NUMBER\0",
"NIM_STRING\0",
"NIM_BLOCK_COMMENT\0",
"NIM_PRAGMA\0",
"NIM_OPERATOR\0",
"NIM_CHAR\0",
"NIM_IDENT\0",
"NIM_MAGIC\0",
"NIM_BRACES\0",
"NIM_STAR\0",
"NIM_STRING_TRIPLE\0",
"NIM_RAW_STRING\0",
"NIM_CTYPE\0",
"NIM_DOC_BLOCK_COMMENT\0",
"NIM_DOC_COMMENT\0"]

let lcTag = [
"default\0",
"KEYWORD\0",
"lineComment\0",
"type\0",
"number\0",
"string\0",
"blockComment\0",
"pragma\0",
"operator\0",
"char\0",
"identifier\0",
"magic\0",
"braces\0",
"exportMarker\0",
"string\0",
"rawString\0",
"ctype\0",
"docBlockComment\0",
"docComment\0"]

const
numChars*: set[char] = {'0'..'9', 'a'..'z', 'A'..'Z'}
SymChars*: set[char] = {'a'..'z', 'A'..'Z', '0'..'9', '\x80'..'\xFF'}
SymStartChars*: set[char] = {'a'..'z', 'A'..'Z', '\x80'..'\xFF'}
OpChars*: set[char] = {'+', '-', '*', '/', '\\', '<', '>', '!', '?', '^', '.',
'|', '=', '%', '&', '$', '@', '~', ':', '\x80'..'\xFF'}

proc Version(x: pointer): int {.stdcall.} = lvOriginal
proc Release(x: pointer) {.stdcall.} = discard
proc PropertyNames(x: pointer): cstring {.stdcall.} = nil
proc PropertyType(x: pointer, name: cstring): int {.stdcall.} = -1
proc DescribeProperty(x: pointer, name: cstring): cstring {.stdcall.} = nil
proc PropertySet(x: pointer, key, val: cstring): int {.stdcall.} = -1
proc DescribeWordListSets(x: pointer): cstring {.stdcall.} = nil
proc WordListSet(x: pointer, n: int, wl: cstring): int {.stdcall.} = -1
const
lexer_ver {.strdefine.} = "release4"

proc Version(lex: Lexer): cint {.stdcall.} =
when lexer_ver == "original":
lvOriginal
else:
lvRelease4

proc Release(lex: Lexer) {.stdcall.} = discard
proc PropertyNames(lex: Lexer): cstring {.stdcall.} = nil
proc PropertyType(lex: Lexer, name: cstring): cint {.stdcall.} = -1
proc DescribeProperty(lex: Lexer, name: cstring): cstring {.stdcall.} = nil
proc PropertySet(lex: Lexer, key, val: cstring): Sci_Position {.stdcall.} = -1
proc DescribeWordListSets(lex: Lexer): cstring {.stdcall.} = nil
proc WordListSet(lex: Lexer, n: cint, wl: cstring): int {.stdcall.} = -1

include leximpl

proc Lex(x: pointer, startPos, docLen, initStyle: int, pAccess: IDocument) {.stdcall.} =
proc Lex(lex: Lexer, startPos: Sci_PositionU, docLen: Sci_Position, initStyle: cint, pAccess: IDocument) {.stdcall.} =
var
styler = initLexAccessor(pAccess)
sc = initStyleContext(startPos, docLen, initStyle, styler.addr)
sc = initStyleContext(startPos.int, docLen.int, initStyle.int, styler.addr)

while sc.more():
case sc.state
Expand Down Expand Up @@ -229,10 +280,10 @@ proc IsQuoteLine(L: LexAccessor, line: int): bool =
result = style == NIM_STRING_TRIPLE

#this algorithm is taken from scintilla python fold code
proc Fold(x: pointer, startPos, docLen: int, initStyle: int, pAccess: IDocument) {.stdcall.} =
proc Fold(lex: Lexer, startPos: Sci_PositionU, docLen: Sci_Position, initStyle: cint, pAccess: IDocument) {.stdcall.} =
var styler = initLexAccessor(pAccess)

let maxPos = startPos + docLen
let maxPos = startPos.int + docLen.int
let maxLines = if maxPos == styler.length(): styler.getLine(maxPos) else: styler.getLine(maxPos - 1) #Requested last line
let docLines = styler.getLine(styler.length()) # Available last line

Expand All @@ -242,7 +293,7 @@ proc Fold(x: pointer, startPos, docLen: int, initStyle: int, pAccess: IDocument)
# at least one line in all cases)
var
spaceFlags: WSTypes
lineCurrent = styler.getLine(startPos)
lineCurrent = styler.getLine(startPos.int)
indentCurrent = styler.indentAmount(lineCurrent, spaceFlags)

while lineCurrent > 0:
Expand Down Expand Up @@ -350,7 +401,61 @@ proc Fold(x: pointer, startPos, docLen: int, initStyle: int, pAccess: IDocument)
# header flag set; the loop above is crafted to take care of this case!
# styler.setLevel(lineCurrent, indentCurrent)

proc PrivateCall(x: pointer, operation: int, ud: pointer): pointer {.stdcall.} = nil
proc PrivateCall(lex: Lexer, operation: cint, ud: pointer): pointer {.stdcall.} = nil

# ILexer4
proc LineEndTypesSupported(lex: Lexer): cint {.stdcall.} =
SC_LINE_END_TYPE_DEFAULT

proc AllocateSubStyles(lex: Lexer, styleBase, numberStyles: cint): cint {.stdcall.} =
-1.cint

proc SubStylesStart(lex: Lexer, styleBase: cint): cint {.stdcall.} =
-1.cint

proc SubStylesLength(lex: Lexer, styleBase: cint): cint {.stdcall.} =
0.cint

proc StyleFromSubStyle(lex: Lexer, subStyle: cint): cint {.stdcall.} =
subStyle.cint

proc PrimaryStyleFromStyle(lex: Lexer, style: cint): cint {.stdcall.} =
style.cint

proc FreeSubStyles(lex: Lexer) {.stdcall.} =
discard

proc SetIdentifiers(lex: Lexer, style: cint, identifiers: cstring) {.stdcall.} =
discard

proc DistanceToSecondaryStyles(lex: Lexer): cint {.stdcall.} =
0

proc GetSubStyleBases(lex: Lexer): cstring {.stdcall.} =
var styleSubable = [0.char]
styleSubable[0].addr

proc NamedStyles(lex: Lexer): cint {.stdcall.} =
lcName.len.cint

proc NameOfStyle(lex: Lexer, style: cint): cstring {.stdcall.} =
if style < lex.NamedStyles():
lcName[style.int][0].unsafeAddr
else:
emptyString[0].unsafeAddr

proc TagsOfStyle(lex: Lexer, style: cint): cstring {.stdcall.} =
if style < lex.NamedStyles():
lcTag[style.int][0].unsafeAddr
else:
emptyString[0].unsafeAddr

proc DescriptionOfStyle(lex: Lexer, style: cint): cstring {.stdcall.} =
# TODO: use descriptive style instead of identifier
if style < lex.NamedStyles():
lcName[style.int][0].unsafeAddr
else:
emptyString[0].unsafeAddr

proc GetLexerCount(): int {.stdcall, exportc, dynlib.} = 1

Expand All @@ -377,6 +482,23 @@ proc lexFactory(): ptr ILexer {.stdcall.} =
vTable[8] = Lex
vTable[9] = Fold
vTable[10] = PrivateCall

# ILexer4
vTable[11] = LineEndTypesSupported
vTable[12] = AllocateSubStyles
vTable[13] = SubStylesStart
vTable[14] = SubStylesLength
vTable[15] = StyleFromSubStyle
vTable[16] = PrimaryStyleFromStyle
vTable[17] = FreeSubStyles
vTable[18] = SetIdentifiers
vTable[19] = DistanceToSecondaryStyles
vTable[20] = GetSubStyleBases
vTable[21] = NamedStyles
vTable[22] = NameOfStyle
vTable[23] = TagsOfStyle
vTable[24] = DescriptionOfStyle

lex.vTable = vTable.addr
result = lex.addr

Expand Down
Loading

0 comments on commit 3de8a8c

Please sign in to comment.