Add more robust comment extraction
This commit is contained in:
parent
789af2b5cc
commit
31bea0f032
2 changed files with 102 additions and 50 deletions
|
|
@ -691,7 +691,7 @@ proc newRecListTree(gState: State, name: string, node: TSNode): PNode =
|
||||||
let
|
let
|
||||||
fdecl = node[i].anyChildInTree("field_declaration_list")
|
fdecl = node[i].anyChildInTree("field_declaration_list")
|
||||||
edecl = node[i].anyChildInTree("enumerator_list")
|
edecl = node[i].anyChildInTree("enumerator_list")
|
||||||
commentNodes = gState.getNextCommentNodes(node[i])
|
commentNodes = gState.getCommentNodes(node[i])
|
||||||
|
|
||||||
# `tname` is name of nested struct / union / enum just
|
# `tname` is name of nested struct / union / enum just
|
||||||
# added, passed on as type name for field in `newIdentDefs()`
|
# added, passed on as type name for field in `newIdentDefs()`
|
||||||
|
|
@ -727,7 +727,7 @@ proc addTypeObject(gState: State, node: TSNode, typeDef: PNode = nil, fname = ""
|
||||||
# If `fname` is set, use it as the name when creating new PNode
|
# If `fname` is set, use it as the name when creating new PNode
|
||||||
# If `istype` is set, this is a typedef, else struct/union
|
# If `istype` is set, this is a typedef, else struct/union
|
||||||
decho("addTypeObject()")
|
decho("addTypeObject()")
|
||||||
let commentNodes = gState.getPrevCommentNodes(node.tsNodeParent())
|
let commentNodes = gState.getCommentNodes(node.tsNodeParent())
|
||||||
|
|
||||||
let
|
let
|
||||||
# Object has fields or not
|
# Object has fields or not
|
||||||
|
|
@ -853,7 +853,7 @@ proc addTypeObject(gState: State, node: TSNode, typeDef: PNode = nil, fname = ""
|
||||||
# Current node has fields
|
# Current node has fields
|
||||||
let
|
let
|
||||||
origname = gState.getNodeVal(node.getAtom())
|
origname = gState.getNodeVal(node.getAtom())
|
||||||
commentNodes = gState.getNextCommentNodes(node)
|
commentNodes = gState.getCommentNodes(node)
|
||||||
|
|
||||||
# Fix issue #185
|
# Fix issue #185
|
||||||
name =
|
name =
|
||||||
|
|
@ -898,7 +898,7 @@ proc addTypeTyped(gState: State, node: TSNode, ftname = "", offset = 0) =
|
||||||
decho("addTypeTyped()")
|
decho("addTypeTyped()")
|
||||||
let
|
let
|
||||||
start = getStartAtom(node)
|
start = getStartAtom(node)
|
||||||
commentNodes = gState.getPrevCommentNodes(node)
|
commentNodes = gState.getCommentNodes(node)
|
||||||
for i in start+1+offset ..< node.len:
|
for i in start+1+offset ..< node.len:
|
||||||
# Add a type of a specific type
|
# Add a type of a specific type
|
||||||
let
|
let
|
||||||
|
|
@ -1017,7 +1017,7 @@ proc addTypeArray(gState: State, node: TSNode) =
|
||||||
# node[start] = identifier = type name
|
# node[start] = identifier = type name
|
||||||
(tname, _, info) = gState.getNameInfo(node[start].getAtom(), nskType, parent = "addTypeArray")
|
(tname, _, info) = gState.getNameInfo(node[start].getAtom(), nskType, parent = "addTypeArray")
|
||||||
tident = gState.getIdent(tname, info, exported = false)
|
tident = gState.getIdent(tname, info, exported = false)
|
||||||
commentNodes = gState.getPrevCommentNodes(node)
|
commentNodes = gState.getCommentNodes(node)
|
||||||
|
|
||||||
# Could have multiple types, comma separated
|
# Could have multiple types, comma separated
|
||||||
for i in start+1 ..< node.len:
|
for i in start+1 ..< node.len:
|
||||||
|
|
@ -1405,7 +1405,7 @@ proc addEnum(gState: State, node: TSNode) =
|
||||||
# nkIdent(name) <- set the comment here
|
# nkIdent(name) <- set the comment here
|
||||||
# )
|
# )
|
||||||
# )
|
# )
|
||||||
defineNode[0][1].comment = gState.getCommentsStr(gState.getPrevCommentNodes(node))
|
defineNode[0][1].comment = gState.getCommentsStr(gState.getCommentNodes(node))
|
||||||
gState.enumSection.add defineNode
|
gState.enumSection.add defineNode
|
||||||
|
|
||||||
# Create const for fields
|
# Create const for fields
|
||||||
|
|
@ -1422,7 +1422,7 @@ proc addEnum(gState: State, node: TSNode) =
|
||||||
|
|
||||||
let
|
let
|
||||||
atom = en.getAtom()
|
atom = en.getAtom()
|
||||||
commentNodes = gState.getNextCommentNodes(en)
|
commentNodes = gState.getCommentNodes(en)
|
||||||
fname = gState.getIdentifier(gState.getNodeVal(atom), nskEnumField)
|
fname = gState.getIdentifier(gState.getNodeVal(atom), nskEnumField)
|
||||||
|
|
||||||
if fname.nBl and gState.addNewIdentifer(fname):
|
if fname.nBl and gState.addNewIdentifer(fname):
|
||||||
|
|
@ -1640,7 +1640,7 @@ proc addDecl(gState: State, node: TSNode) =
|
||||||
|
|
||||||
let
|
let
|
||||||
start = getStartAtom(node)
|
start = getStartAtom(node)
|
||||||
commentNodes = gState.getPrevCommentNodes(node)
|
commentNodes = gState.getCommentNodes(node)
|
||||||
|
|
||||||
for i in start+1 ..< node.len:
|
for i in start+1 ..< node.len:
|
||||||
if not node[i].firstChildInTree("function_declarator").isNil:
|
if not node[i].firstChildInTree("function_declarator").isNil:
|
||||||
|
|
@ -1665,7 +1665,7 @@ proc addDef(gState: State, node: TSNode) =
|
||||||
|
|
||||||
let
|
let
|
||||||
start = getStartAtom(node)
|
start = getStartAtom(node)
|
||||||
commentNodes = gState.getPrevCommentNodes(node)
|
commentNodes = gState.getCommentNodes(node)
|
||||||
|
|
||||||
if node[start+1].getName() == "function_declarator":
|
if node[start+1].getName() == "function_declarator":
|
||||||
if gState.isIncludeHeader():
|
if gState.isIncludeHeader():
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
import dynlib, macros, os, sequtils, sets, strformat, strutils, tables, times
|
import dynlib, macros, os, sequtils, sets, strformat, strutils, tables, times
|
||||||
import algorithm
|
|
||||||
|
|
||||||
import regex
|
import regex
|
||||||
|
|
||||||
|
|
@ -572,7 +571,7 @@ proc getPreprocessor*(gState: State, fullpath: string): string =
|
||||||
|
|
||||||
# Include content only from file
|
# Include content only from file
|
||||||
for line in execAction(cmd).output.splitLines():
|
for line in execAction(cmd).output.splitLines():
|
||||||
if line.strip() != "":
|
# We want to keep blank lines here for comment processing
|
||||||
if line.len > 1 and line[0 .. 1] == "# ":
|
if line.len > 1 and line[0 .. 1] == "# ":
|
||||||
start = false
|
start = false
|
||||||
let
|
let
|
||||||
|
|
@ -644,32 +643,85 @@ proc getCommentsStr*(gState: State, commentNodes: seq[TSNode]): string =
|
||||||
result &= "\n " & gState.getNodeVal(commentNode).
|
result &= "\n " & gState.getNodeVal(commentNode).
|
||||||
replace(re" *(//|/\*\*|\*\*/|/\*|\*/|\*)", "").replace("\n", "\n ").strip()
|
replace(re" *(//|/\*\*|\*\*/|/\*|\*/|\*)", "").replace("\n", "\n ").strip()
|
||||||
|
|
||||||
proc getPrevCommentNodes*(gState: State, node: TSNode, maxSearch=1): seq[TSNode] =
|
proc getCommentNodes*(gState: State, node: TSNode, maxSearch=1): seq[TSNode] =
|
||||||
## Here we want to go until the node we get is not a comment
|
## Get a set of comment nodes in order of priority. Will search up to ``maxSearch``
|
||||||
## for cases with multiple ``//`` comments instead of one ``/* */``
|
## nodes before and after the current node
|
||||||
## section
|
##
|
||||||
|
## Priority is (closest line number) > comment before > comment after.
|
||||||
|
## This priority might need to be changed based on the project, but
|
||||||
|
## for now it is good enough
|
||||||
|
|
||||||
|
# Skip this if we don't want comments
|
||||||
if gState.nocomments:
|
if gState.nocomments:
|
||||||
return
|
return
|
||||||
|
|
||||||
var sibling = node.tsNodePrevNamedSibling()
|
let (line, _) = gState.getLineCol(node)
|
||||||
var i = 0
|
|
||||||
|
|
||||||
# Search for the starting comment up to maxSearch nodes away
|
# Keep track of both directions from a node
|
||||||
while not sibling.isNil and i < maxSearch:
|
var
|
||||||
# Once a comment is found, find all of the comments right next to
|
prevSibling = node.tsNodePrevNamedSibling()
|
||||||
# it so that we can get multiple // style comments
|
nextSibling = node.tsNodeNextNamedSibling()
|
||||||
while not sibling.isNil and sibling.getName() == "comment":
|
nilNode: TSNode
|
||||||
result.add(sibling)
|
|
||||||
sibling = sibling.tsNodePrevNamedSibling()
|
|
||||||
|
|
||||||
if sibling.isNil:
|
var
|
||||||
|
i = 0
|
||||||
|
prevSiblingDistance, nextSiblingDistance: int
|
||||||
|
lowestDistance: int
|
||||||
|
commentsFound = false
|
||||||
|
|
||||||
|
while not commentsFound and i < maxSearch:
|
||||||
|
|
||||||
|
# Distance from the current node will tell us approximately if the
|
||||||
|
# comment belongs to the node. The closer it is in terms of line
|
||||||
|
# numbers, the more we can be sure it's the comment we want
|
||||||
|
if not prevSibling.isNil:
|
||||||
|
prevSiblingDistance = abs(gState.getLineCol(prevSibling)[0] - line)
|
||||||
|
if not nextSibling.isNil:
|
||||||
|
nextSiblingDistance = abs(gState.getLineCol(nextSibling)[0] - line)
|
||||||
|
|
||||||
|
lowestDistance = min(prevSiblingDistance, nextSiblingDistance)
|
||||||
|
|
||||||
|
if prevSiblingDistance > maxSearch:
|
||||||
|
# If the line is out of range, skip searching
|
||||||
|
prevSibling = nilNode # Can't do `= nil`
|
||||||
|
|
||||||
|
if nextSiblingDistance > maxSearch:
|
||||||
|
# If the line is out of range, skip searching
|
||||||
|
prevSibling = nilNode
|
||||||
|
|
||||||
|
while (
|
||||||
|
not prevSibling.isNil and
|
||||||
|
prevSibling.getName() == "comment" and
|
||||||
|
prevSiblingDistance == lowestDistance
|
||||||
|
):
|
||||||
|
# Put the previous nodes in reverse order so the comments
|
||||||
|
# make logical sense
|
||||||
|
result.insert(prevSibling, 0)
|
||||||
|
prevSibling = prevSibling.tsNodePrevNamedSibling()
|
||||||
|
commentsFound = true
|
||||||
|
|
||||||
|
if commentsFound:
|
||||||
break
|
break
|
||||||
|
|
||||||
sibling = sibling.tsNodePrevNamedSibling()
|
while (
|
||||||
i += 1
|
not nextSibling.isNil and
|
||||||
|
nextSibling.getName() == "comment" and
|
||||||
|
nextSiblingDistance == lowestDistance
|
||||||
|
):
|
||||||
|
result.add(nextSibling)
|
||||||
|
nextSibling = nextSibling.tsNodeNextNamedSibling()
|
||||||
|
commentsFound = true
|
||||||
|
|
||||||
# reverse the comments because we got them in reverse order
|
if commentsFound:
|
||||||
result.reverse
|
break
|
||||||
|
|
||||||
|
# Go to next sibling pair
|
||||||
|
if not prevSibling.isNil:
|
||||||
|
prevSibling = prevSibling.tsNodePrevNamedSibling()
|
||||||
|
if not nextSibling.isNil:
|
||||||
|
nextSibling = nextSibling.tsNodeNextNamedSibling()
|
||||||
|
|
||||||
|
i += 1
|
||||||
|
|
||||||
proc getNextCommentNodes*(gState: State, node: TSNode, maxSearch=1): seq[TSNode] =
|
proc getNextCommentNodes*(gState: State, node: TSNode, maxSearch=1): seq[TSNode] =
|
||||||
## Searches the next nodes up to maxSearch nodes away for a comment
|
## Searches the next nodes up to maxSearch nodes away for a comment
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue