@@ -2,9 +2,11 @@ package com.vladsch.clionarduinoplugin.generators.cmake
2
2
3
3
import com.intellij.openapi.diagnostic.Logger
4
4
import com.vladsch.clionarduinoplugin.generators.cmake.ast.Argument
5
+ import com.vladsch.clionarduinoplugin.generators.cmake.ast.BlankLine
5
6
import com.vladsch.clionarduinoplugin.generators.cmake.ast.CMakeFile
6
7
import com.vladsch.clionarduinoplugin.generators.cmake.ast.Command
7
8
import com.vladsch.clionarduinoplugin.generators.cmake.ast.CommentedOutCommand
9
+ import com.vladsch.clionarduinoplugin.generators.cmake.ast.LineComment
8
10
import com.vladsch.clionarduinoplugin.generators.cmake.commands.*
9
11
import com.vladsch.clionarduinoplugin.resources.TemplateResolver
10
12
import com.vladsch.clionarduinoplugin.resources.resolveRefs
@@ -203,10 +205,16 @@ abstract class CMakeListsTxtBuilder(
203
205
useVarName = rawArgs[0 ].removePrefix(" \$ {" ).removeSuffix(" }" )
204
206
}
205
207
206
- if (rawArgs.isNotEmpty() && (rawArgs[0 ] == " \$ {PROJECT_NAME }_SRCS" || rawArgs[0 ] == " \$ {PROJECT_NAME }_HDRS" )) {
208
+ if (rawArgs.isNotEmpty() && (rawArgs[0 ] == " ${cMakeProjectNameMacro } _SRCS" || rawArgs[0 ] == " ${cMakeProjectNameMacro } _HDRS" )) {
207
209
// Don't expand any macros. Just leave as is
208
210
if (rawArgs.size > 1 ) cMakeVariableValues[rawArgs[0 ]] = rawArgs.slice(1 .. rawArgs.size - 1 )
209
211
else cMakeVariableValues[rawArgs[0 ]] = null
212
+ } 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" )) {
214
+ // this is most likely a mistake, since the project name is in cMakeProjectNameMacro
215
+ val replaceMacroName = cMakeProjectNameMacro + rawArgs[0 ].substring(rawArgs[0 ].length - 5 )
216
+ if (rawArgs.size > 1 ) cMakeVariableValues[replaceMacroName] = rawArgs.slice(1 .. rawArgs.size - 1 )
217
+ else cMakeVariableValues[replaceMacroName] = null
210
218
} else {
211
219
if (args.size > 1 ) cMakeVariableValues[useVarName ? : args[0 ]] = args.slice(1 .. args.size - 1 )
212
220
else if (args.isNotEmpty()) cMakeVariableValues[useVarName ? : args[0 ]] = null
@@ -256,10 +264,12 @@ abstract class CMakeListsTxtBuilder(
256
264
// 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
257
265
var commandType: CMakeCommandType ? = null
258
266
val rawArgs = ArrayList <String >()
267
+ val rawArgLeadingSpaces = ArrayList <BasedSequence >()
259
268
260
269
for (arg in node.getChildren()) {
261
270
if (arg is Argument ) {
262
271
rawArgs.add(arg.text.toString())
272
+ rawArgLeadingSpaces.add(arg.leadingSpaces)
263
273
}
264
274
}
265
275
@@ -322,8 +332,15 @@ abstract class CMakeListsTxtBuilder(
322
332
323
333
val makeCommand: CMakeCommandBase
324
334
335
+ // DEBUG: remove lines
336
+ if (commandName == " made_up_command" ) {
337
+ val tmp = 0
338
+ }
339
+
325
340
if (commandType != null ) {
326
- makeCommand = CMakeCommand (commandType, false )
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
327
344
328
345
var i = 0
329
346
var j = 0
@@ -336,17 +353,20 @@ abstract class CMakeListsTxtBuilder(
336
353
val matcher = regEx.matcher(rawArgs[i++ ])
337
354
matcher.find()
338
355
for (g in 1 .. matcher.groupCount()) {
339
- makeCommand.setArg(j++ , matcher.group(g))
356
+ makeCommand.setArg(j++ , matcher.group(g), rawArgLeadingSpaces[g] )
340
357
}
341
358
} else i++
342
359
}
343
360
344
361
while (i < rawArgs.size) {
345
- makeCommand.setArg(j++ , rawArgs[i++ ])
362
+ makeCommand.setArg(j++ , rawArgs[i], rawArgLeadingSpaces[i])
363
+ i++
346
364
}
347
365
} else {
348
366
// unknown command
349
- makeCommand = CMakeUnknownCommand (commandName, rawArgs, false )
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
350
370
}
351
371
352
372
if (node is CommentedOutCommand ) {
@@ -357,10 +377,9 @@ abstract class CMakeListsTxtBuilder(
357
377
}
358
378
359
379
// create a text element
360
- if (node.chars.toLowerCase().startsWith(" if" )) {
361
- val tmp = 0 ;
362
- }
363
- return CMakeText (node.chars.toString(), false )
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 ))
364
383
}
365
384
366
385
fun elementOriginalText (element : CMakeElement ): String {
@@ -404,15 +423,22 @@ abstract class CMakeListsTxtBuilder(
404
423
405
424
fun setElement (index : Int , element : CMakeElement ) {
406
425
if (index < myElements.size) {
407
- element.isAddEOL = myElements[index].isAddEOL
426
+ val indexElement = myElements[index]
427
+ element.isAddEOL = indexElement.isAddEOL
428
+
429
+ if (element is CMakeCommandBase && indexElement is CMakeCommandBase ) {
430
+ element.leadingSpaces = indexElement.leadingSpaces
431
+ }
408
432
} else {
409
433
element.isAddEOL = true
410
434
}
435
+
411
436
myElements[index] = element
412
437
}
413
438
414
439
fun removeElement (index : Int ) {
415
- if (index + 1 < myElements.size) myElements[index + 1 ].isAddEOL = myElements[index].isAddEOL
440
+ if (index + 1 < myElements.size)
441
+ myElements[index + 1 ].isAddEOL = myElements[index].isAddEOL
416
442
myElements.removeAt(index)
417
443
}
418
444
@@ -733,10 +759,6 @@ abstract class CMakeListsTxtBuilder(
733
759
}
734
760
}
735
761
736
- fun setCommand (name : String , vararg args : String ): CMakeCommand ? {
737
- return setCommand(getCommandType(name), Arrays .asList(* args))
738
- }
739
-
740
762
fun setCommand (name : String , args : Collection <String >): CMakeCommand ? {
741
763
return setCommand(getCommandType(name), args)
742
764
}
@@ -780,7 +802,7 @@ abstract class CMakeListsTxtBuilder(
780
802
val command = setCommand(commandType, args)
781
803
if (command == null ) {
782
804
// create a new one and find where to insert it
783
- val newCommand = CMakeCommand (commandType, args?.toList() ? : listOf (), true )
805
+ val newCommand = CMakeCommand (commandType, args?.toList() ? : listOf (), null , true )
784
806
addCommand(newCommand)
785
807
return newCommand
786
808
}
@@ -810,23 +832,23 @@ abstract class CMakeListsTxtBuilder(
810
832
// original command, replace it with new
811
833
newCommand = CMakeCommand (command)
812
834
newCommand.commentOut(false )
813
- newCommand.setArgsWithDefaults(argList)
835
+ newCommand.setArgsWithDefaults(argList, command.getArgsLeadingSpaces(argList) )
814
836
replaceElement(command, newCommand)
815
837
} else {
816
- newCommand!! .setArgsWithDefaults(argList)
838
+ newCommand!! .setArgsWithDefaults(argList, null )
817
839
}
818
840
} else {
819
841
if (commandType.isMultiple) {
820
842
if (commandType.isNoDupeArgs) {
821
843
// add another one after this one if the arg value is different
822
844
if (! command.allArgsEqual(argList)) {
823
- newCommand = CMakeCommand (command.commandType, argList, true )
845
+ newCommand = CMakeCommand (command.commandType, argList, command.getArgsLeadingSpaces(argList), true )
824
846
newCommand.commentOut(false )
825
847
addElementAfter(command, newCommand)
826
848
}
827
849
} else {
828
850
// add another one after this one
829
- newCommand = CMakeCommand (command.commandType, argList, true )
851
+ newCommand = CMakeCommand (command.commandType, argList, null , true )
830
852
addElementAfter(command, newCommand)
831
853
}
832
854
} else {
@@ -840,18 +862,18 @@ abstract class CMakeListsTxtBuilder(
840
862
if (command.isOfType(commandType)) {
841
863
newCommand = CMakeCommand (command)
842
864
newCommand.commentOut(false )
843
- newCommand.setArgsWithDefaults(argList)
865
+ newCommand.setArgsWithDefaults(argList, command.getArgsLeadingSpaces(argList) )
844
866
replaceElement(command, newCommand)
845
867
} else {
846
868
// is a matched sub-type, need to replace completely
847
- newCommand = CMakeCommand (commandType, argList, false )
869
+ newCommand = CMakeCommand (commandType, argList, null , false )
848
870
newCommand.commentOut(false )
849
- newCommand.setArgsWithDefaults(argList)
871
+ newCommand.setArgsWithDefaults(argList, command.getArgsLeadingSpaces(argList) )
850
872
851
873
replaceElement(command, newCommand)
852
874
}
853
875
} else {
854
- newCommand!! .setArgsWithDefaults(argList)
876
+ newCommand!! .setArgsWithDefaults(argList, null )
855
877
}
856
878
}
857
879
}
@@ -1060,7 +1082,7 @@ abstract class CMakeListsTxtBuilder(
1060
1082
private val LOG = Logger .getInstance(" com.vladsch.clionarduinoplugin.generators" )
1061
1083
private val DEFAULT_OPTIONS = MutableDataSet ()
1062
1084
.set(CMakeParser .AUTO_CONFIG , true )
1063
- .set(CMakeParser .AST_LINE_END_EOL , true )
1085
+ .set(CMakeParser .AST_LINE_END_EOL , false )
1064
1086
.set(CMakeParser .AST_COMMENTS , true )
1065
1087
.set(CMakeParser .AST_BLANK_LINES , true )
1066
1088
.set(CMakeParser .AST_ARGUMENT_SEPARATORS , true )
0 commit comments