You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
refactor codeAsInSource to not require command + fix nbJs bug fixes (#163)
* refactor codeAsInSource to not require command
* fix infix
* add more fixes
* implement nb.sourceFiles
* fix typesection and add discard test
* fix for loops
* let try pietro's idea of taking the minimum
* fix parallel bug
* add template test
* fix type bug
* update changelog
* bump nimble version
* clean sources
* add Hugo as co-author
* add filename assert in Pos comparision
* assert -> doAssert. Improved error message and added an assert for the endPos as well
## Extracts the code in source from startPos to endPos with additional processing to get the entire code block.
96
-
let rawLines = source.split("\n")
97
-
var startLine =findStartLine(rawLines, command, startPos.line -1)
112
+
let rawLines = source.splitLines()
113
+
let rawStartLine = startPos.line -1
114
+
let rawStartCol = startPos.column -1
115
+
var startLine =findStartLine(rawLines, startPos)
98
116
var endLine =findEndLine(rawLines, command, startLine, endPos.line -1)
99
117
100
118
var lines = rawLines[startLine .. endLine]
101
119
102
-
let baseIndent =skipWhile(rawLines[startLine], {' '})
103
-
104
-
let startsOnCommandLine = lines[0].isCommandLine(command) # is it nbCode: code or nbCode: <enter> code
105
-
if startsOnCommandLine: # remove the command
106
-
var startColumn = startPos.column
107
-
# the "import"-part is not included in the startPos
108
-
let startsWithImport = lines[0].find("import")
109
-
if startsWithImport !=-1:
110
-
startColumn = startsWithImport
111
-
lines[0] = lines[0][startColumn ..^1].strip()
112
-
113
-
var codeText: string
114
-
if startLine == endLine and startsOnCommandLine: # single-line expression
115
-
# remove eventual unneccerary parenthesis
116
-
let line = rawLines[startLine] # includes command and eventual opening parethesises
117
-
var extractedLine = lines[0] # doesn't include command
118
-
if extractedLine.endsWith(")"):
119
-
# check if the ending ")" has a matching "(", otherwise remove it.
120
-
var nOpen: int
121
-
var i = startPos.column
122
-
# count the number of opening brackets before code starts.
123
-
while line[i-1] inWhitespaceor line[i-1] =='(':
124
-
if line[i-1] =='(':
125
-
nOpen +=1
126
-
i -=1
127
-
var nRemoved: int
128
-
while nRemoved < nOpen: # remove last char until we have removed correct number of parentesis
129
-
# We assume we are given correct Nim code and thus won't have to check what we remove, it should either be Whitespace or ')'
130
-
assert extractedLine[^1] inWhitespaceor extractedLine[^1] ==')', "Unexpected ending of string during parsing. Single line expression ended with character that wasn't whitespace of ')'."
131
-
if extractedLine[^1] ==')':
132
-
nRemoved +=1
133
-
extractedLine.setLen(extractedLine.len-1)
134
-
codeText = extractedLine
120
+
let startsOnCommandLine =block:
121
+
let preline = lines[0][0..< rawStartCol]
122
+
startLine == rawStartLine and (not preline.isEmptyOrWhitespace) and (not (preline.nimIdentNormalize.strip() in ["for", "type"]))
123
+
124
+
if startsOnCommandLine:
125
+
lines[0] = lines[0][rawStartCol ..^1].strip()
126
+
127
+
if startLine == endLine and startsOnCommandLine:
128
+
# single line expression
129
+
var line = lines[0] # doesn't include command, but includes opening parenthesis
130
+
while line.startsWith('(') and line.endsWith(')'):
131
+
line = line[1..^2].strip()
132
+
133
+
result= line
135
134
else: # multi-line expression
135
+
let baseIndent =skipWhile(rawLines[startLine], {' '})
#[ cases like this reports the third line instead of the second line:
161
-
nbCode:
162
-
let # this is the line we want
163
-
x = 1 # but this is the one we get
164
-
]#
165
-
dec startLine
166
-
167
-
let indent =skipWhile(lines[startLine], {' '})
168
-
let indentStr ="".repeat(indent)
169
-
170
-
if lines[endLine].count("\"\"\"") ==1: # only opening of triple quoted string found. Rest is below it.
171
-
inc endLine # bump it to not trigger the loop to immediately break
172
-
while endLine < lines.high and"\"\"\""notin lines[endLine]:
173
-
inc endLine
174
-
debugecho"Triple quote: ", lines[endLine]
175
-
176
-
while endLine < lines.high and (lines[endLine+1].startsWith(indentStr) or lines[endLine+1].isEmptyOrWhitespace):# and lines[endLine+1].strip().startsWith("#"):
177
-
# Ending Comments should be included as well, but they won't be included in the AST -> endLine doesn't take them into account.
178
-
# Block comments must be properly indented (including the content)
179
-
inc endLine
180
-
181
-
var codeLines = lines[startLine .. endLine]
182
-
183
-
var notIndentLines: seq[int] # these lines are not to be adjusted for indentation. Eg content of triple quoted strings.
184
-
var i: int
185
-
while i < codeLines.len:
186
-
if codeLines[i].count("\"\"\"") ==1:
187
-
# We must do the identification of triple quoted string separatly from the endLine bumping because the triple strings
188
-
# might not be the last expression in the code block.
189
-
inc i # bump it to not trigger the loop to immediately break on the initial """
190
-
notIndentLines.add i
191
-
while i < codeLines.len and"\"\"\""notin codeLines[i]:
192
-
inc i
193
-
notIndentLines.add i
194
-
inc i
195
-
196
-
let parsedLines =collect(newSeqOfCap(codeLines.len)):
else: # single line case, eg `nbCode: echo "Hello World"`
206
-
let line = lines[startLine]
207
-
var extractedLine = line[startPos.column ..^1].strip()
208
-
if extractedLine.strip().endsWith(")"):
209
-
# check if the ending ")" has a matching "(", otherwise remove it.
210
-
var nOpen: int
211
-
var i = startPos.column
212
-
# count the number of opening brackets before code starts.
213
-
while line[i-1] inWhitespaceor line[i-1] =='(':
214
-
if line[i-1] =='(':
215
-
nOpen +=1
216
-
i -=1
217
-
var nRemoved: int
218
-
while nRemoved < nOpen: # remove last char until we have removed correct number of parentesis
219
-
# We assume we are given correct Nim code and thus won't have to check what we remove, it should either be Whitespace or ')'
220
-
assert extractedLine[^1] inWhitespaceor extractedLine[^1] ==')', "Unexpected ending of string during parsing. Single line expression ended with character that wasn't whitespace of ')'."
0 commit comments