1
1
package com.vladsch.clionarduinoplugin.generators.cmake
2
2
3
3
import com.intellij.openapi.diagnostic.Logger
4
- import com.vladsch.clionarduinoplugin.generators.cmake.ast.Argument
5
- import com.vladsch.clionarduinoplugin.generators.cmake.ast.BlankLine
6
- import com.vladsch.clionarduinoplugin.generators.cmake.ast.CMakeFile
7
- import com.vladsch.clionarduinoplugin.generators.cmake.ast.Command
8
- import com.vladsch.clionarduinoplugin.generators.cmake.ast.CommentedOutCommand
9
- import com.vladsch.clionarduinoplugin.generators.cmake.ast.LineComment
4
+ import com.vladsch.clionarduinoplugin.generators.cmake.ast.*
10
5
import com.vladsch.clionarduinoplugin.generators.cmake.commands.*
11
6
import com.vladsch.clionarduinoplugin.resources.TemplateResolver
12
7
import com.vladsch.clionarduinoplugin.resources.resolveRefs
@@ -210,10 +205,11 @@ abstract class CMakeListsTxtBuilder(
210
205
if (rawArgs.size > 1 ) cMakeVariableValues[rawArgs[0 ]] = rawArgs.slice(1 .. rawArgs.size - 1 )
211
206
else cMakeVariableValues[rawArgs[0 ]] = null
212
207
} else if (rawArgs.isNotEmpty() && (rawArgs[0 ] == " \$ {PROJECT_NAME}_SRCS" || rawArgs[0 ] == " \$ {PROJECT_NAME}_HDRS"
213
- || rawArgs[0 ] == " \$ {CMAKE_PROJECT_NAME}_SRCS" || rawArgs[0 ] == " \$ {CMAKE_PROJECT_NAME}_HDRS" )) {
208
+ || rawArgs[0 ] == " \$ {CMAKE_PROJECT_NAME}_SRCS" || rawArgs[0 ] == " \$ {CMAKE_PROJECT_NAME}_HDRS" )
209
+ ) {
214
210
// this is most likely a mistake, since the project name is in cMakeProjectNameMacro
215
211
val replaceMacroName = cMakeProjectNameMacro + rawArgs[0 ].substring(rawArgs[0 ].length - 5 )
216
- if (rawArgs.size > 1 ) cMakeVariableValues[replaceMacroName] = rawArgs.slice(1 .. rawArgs.size - 1 )
212
+ if (rawArgs.size > 1 ) cMakeVariableValues[replaceMacroName] = rawArgs.slice(1 .. rawArgs.size - 1 )
217
213
else cMakeVariableValues[replaceMacroName] = null
218
214
} else {
219
215
if (args.size > 1 ) cMakeVariableValues[useVarName ? : args[0 ]] = args.slice(1 .. args.size - 1 )
@@ -264,15 +260,24 @@ abstract class CMakeListsTxtBuilder(
264
260
// find the command or we can create a new one if we don't have it already or just make it into a text block
265
261
var commandType: CMakeCommandType ? = null
266
262
val rawArgs = ArrayList <String >()
267
- val rawArgLeadingSpaces = ArrayList <BasedSequence >()
263
+ val rawArgsLeadingSpaces = ArrayList <BasedSequence >()
268
264
269
265
for (arg in node.getChildren()) {
270
266
if (arg is Argument ) {
271
267
rawArgs.add(arg.text.toString())
272
- rawArgLeadingSpaces.add(arg.leadingSpaces)
268
+ if (arg.previous is LineEnding ) {
269
+ rawArgsLeadingSpaces.add(arg.leadingSpaces.prefixWithEOL())
270
+ } else {
271
+ rawArgsLeadingSpaces.add(arg.leadingSpaces)
272
+ }
273
273
}
274
274
}
275
275
276
+ val lastChild = node.lastChild
277
+ val addBeforeClosing =
278
+ if (lastChild is LineEnding ) node.baseSubSequence(lastChild.startOffset, node.closingMarker.startOffset)
279
+ else BasedSequence .NULL
280
+
276
281
val commandName = node.command.toString()
277
282
if (commandName == " set" ) {
278
283
// see if we have a matching set command
@@ -336,11 +341,10 @@ abstract class CMakeListsTxtBuilder(
336
341
if (commandName == " made_up_command" ) {
337
342
val tmp = 0
338
343
}
339
-
344
+
340
345
if (commandType != null ) {
341
- makeCommand = CMakeCommand (commandType, node.next is LineComment )
342
- // KLUDGE: blank line nodes steal EOL from next node's leadingSpaces, so need to add it here to compensate
343
- makeCommand.leadingSpaces = if (node.previous is BlankLine ) node.leadingSpaces.prefixWithEOL() else node.leadingSpaces
346
+ makeCommand = CMakeCommand (commandType, false , addBeforeClosing)
347
+ makeCommand.leadingSpaces = node.leadingSpaces
344
348
345
349
var i = 0
346
350
var j = 0
@@ -353,20 +357,19 @@ abstract class CMakeListsTxtBuilder(
353
357
val matcher = regEx.matcher(rawArgs[i++ ])
354
358
matcher.find()
355
359
for (g in 1 .. matcher.groupCount()) {
356
- makeCommand.setArg(j++ , matcher.group(g), rawArgLeadingSpaces [g])
360
+ makeCommand.setArg(j++ , matcher.group(g), rawArgsLeadingSpaces [g])
357
361
}
358
362
} else i++
359
363
}
360
364
361
365
while (i < rawArgs.size) {
362
- makeCommand.setArg(j++ , rawArgs[i], rawArgLeadingSpaces [i])
366
+ makeCommand.setArg(j++ , rawArgs[i], rawArgsLeadingSpaces [i])
363
367
i++
364
368
}
365
369
} else {
366
370
// unknown command
367
- makeCommand = CMakeUnknownCommand (commandName, rawArgs, rawArgLeadingSpaces, node.next is LineComment )
368
- // KLUDGE: blank line nodes steal EOL from next node's leadingSpaces, so need to add it here to compensate
369
- makeCommand.leadingSpaces = if (node.previous is BlankLine ) node.leadingSpaces.prefixWithEOL() else node.leadingSpaces
371
+ makeCommand = CMakeUnknownCommand (commandName, rawArgs, rawArgsLeadingSpaces, false , addBeforeClosing)
372
+ makeCommand.leadingSpaces = node.leadingSpaces
370
373
}
371
374
372
375
if (node is CommentedOutCommand ) {
@@ -377,9 +380,7 @@ abstract class CMakeListsTxtBuilder(
377
380
}
378
381
379
382
// create a text element
380
- // KLUDGE: blank line nodes steal EOL from next node's leadingSpaces, so need to add it here to compensate
381
- // however, if the previous is a LineComment and it has EOL then adding an extra one would double blank lines
382
- return CMakeText (node.chars.toString(), node is BlankLine && node.next !is BlankLine && node.next !is Command && ! (node.previous is LineComment && node.previous?.chars?.endsWithEOL()? : false ))
383
+ return CMakeText (node.chars.toString(), false )
383
384
}
384
385
385
386
fun elementOriginalText (element : CMakeElement ): String {
@@ -437,7 +438,7 @@ abstract class CMakeListsTxtBuilder(
437
438
}
438
439
439
440
fun removeElement (index : Int ) {
440
- if (index + 1 < myElements.size)
441
+ if (index + 1 < myElements.size)
441
442
myElements[index + 1 ].isAddEOL = myElements[index].isAddEOL
442
443
myElements.removeAt(index)
443
444
}
@@ -802,7 +803,7 @@ abstract class CMakeListsTxtBuilder(
802
803
val command = setCommand(commandType, args)
803
804
if (command == null ) {
804
805
// create a new one and find where to insert it
805
- val newCommand = CMakeCommand (commandType, args?.toList() ? : listOf (), null , true )
806
+ val newCommand = CMakeCommand (commandType, args?.toList() ? : listOf (), null , true , BasedSequence . NULL )
806
807
addCommand(newCommand)
807
808
return newCommand
808
809
}
@@ -842,13 +843,19 @@ abstract class CMakeListsTxtBuilder(
842
843
if (commandType.isNoDupeArgs) {
843
844
// add another one after this one if the arg value is different
844
845
if (! command.allArgsEqual(argList)) {
845
- newCommand = CMakeCommand (command.commandType, argList, command.getArgsLeadingSpaces(argList), true )
846
+ newCommand = CMakeCommand (
847
+ command.commandType,
848
+ argList,
849
+ command.getArgsLeadingSpaces(argList),
850
+ true ,
851
+ command.addBeforeClosing
852
+ )
846
853
newCommand.commentOut(false )
847
854
addElementAfter(command, newCommand)
848
855
}
849
856
} else {
850
857
// add another one after this one
851
- newCommand = CMakeCommand (command.commandType, argList, null , true )
858
+ newCommand = CMakeCommand (command.commandType, argList, null , true , BasedSequence . NULL )
852
859
addElementAfter(command, newCommand)
853
860
}
854
861
} else {
@@ -866,7 +873,7 @@ abstract class CMakeListsTxtBuilder(
866
873
replaceElement(command, newCommand)
867
874
} else {
868
875
// is a matched sub-type, need to replace completely
869
- newCommand = CMakeCommand (commandType, argList, null , false )
876
+ newCommand = CMakeCommand (commandType, argList, null , false , command.addBeforeClosing )
870
877
newCommand.commentOut(false )
871
878
newCommand.setArgsWithDefaults(argList, command.getArgsLeadingSpaces(argList))
872
879
@@ -1082,7 +1089,7 @@ abstract class CMakeListsTxtBuilder(
1082
1089
private val LOG = Logger .getInstance(" com.vladsch.clionarduinoplugin.generators" )
1083
1090
private val DEFAULT_OPTIONS = MutableDataSet ()
1084
1091
.set(CMakeParser .AUTO_CONFIG , true )
1085
- .set(CMakeParser .AST_LINE_END_EOL , false )
1092
+ .set(CMakeParser .AST_LINE_END_EOL , true )
1086
1093
.set(CMakeParser .AST_COMMENTS , true )
1087
1094
.set(CMakeParser .AST_BLANK_LINES , true )
1088
1095
.set(CMakeParser .AST_ARGUMENT_SEPARATORS , true )
0 commit comments