Skip to content

Commit 2764dac

Browse files
committed
trial eol fixes
1 parent da2a28c commit 2764dac

File tree

1 file changed

+47
-25
lines changed

1 file changed

+47
-25
lines changed

src/com/vladsch/clionarduinoplugin/generators/cmake/CMakeListsTxtBuilder.kt

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package com.vladsch.clionarduinoplugin.generators.cmake
22

33
import com.intellij.openapi.diagnostic.Logger
44
import com.vladsch.clionarduinoplugin.generators.cmake.ast.Argument
5+
import com.vladsch.clionarduinoplugin.generators.cmake.ast.BlankLine
56
import com.vladsch.clionarduinoplugin.generators.cmake.ast.CMakeFile
67
import com.vladsch.clionarduinoplugin.generators.cmake.ast.Command
78
import com.vladsch.clionarduinoplugin.generators.cmake.ast.CommentedOutCommand
9+
import com.vladsch.clionarduinoplugin.generators.cmake.ast.LineComment
810
import com.vladsch.clionarduinoplugin.generators.cmake.commands.*
911
import com.vladsch.clionarduinoplugin.resources.TemplateResolver
1012
import com.vladsch.clionarduinoplugin.resources.resolveRefs
@@ -203,10 +205,16 @@ abstract class CMakeListsTxtBuilder(
203205
useVarName = rawArgs[0].removePrefix("\${").removeSuffix("}")
204206
}
205207

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")) {
207209
// Don't expand any macros. Just leave as is
208210
if (rawArgs.size > 1) cMakeVariableValues[rawArgs[0]] = rawArgs.slice(1 .. rawArgs.size - 1)
209211
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
210218
} else {
211219
if (args.size > 1) cMakeVariableValues[useVarName ?: args[0]] = args.slice(1 .. args.size - 1)
212220
else if (args.isNotEmpty()) cMakeVariableValues[useVarName ?: args[0]] = null
@@ -256,10 +264,12 @@ abstract class CMakeListsTxtBuilder(
256264
// 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
257265
var commandType: CMakeCommandType? = null
258266
val rawArgs = ArrayList<String>()
267+
val rawArgLeadingSpaces = ArrayList<BasedSequence>()
259268

260269
for (arg in node.getChildren()) {
261270
if (arg is Argument) {
262271
rawArgs.add(arg.text.toString())
272+
rawArgLeadingSpaces.add(arg.leadingSpaces)
263273
}
264274
}
265275

@@ -322,8 +332,15 @@ abstract class CMakeListsTxtBuilder(
322332

323333
val makeCommand: CMakeCommandBase
324334

335+
// DEBUG: remove lines
336+
if (commandName == "made_up_command") {
337+
val tmp = 0
338+
}
339+
325340
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
327344

328345
var i = 0
329346
var j = 0
@@ -336,17 +353,20 @@ abstract class CMakeListsTxtBuilder(
336353
val matcher = regEx.matcher(rawArgs[i++])
337354
matcher.find()
338355
for (g in 1 .. matcher.groupCount()) {
339-
makeCommand.setArg(j++, matcher.group(g))
356+
makeCommand.setArg(j++, matcher.group(g), rawArgLeadingSpaces[g])
340357
}
341358
} else i++
342359
}
343360

344361
while (i < rawArgs.size) {
345-
makeCommand.setArg(j++, rawArgs[i++])
362+
makeCommand.setArg(j++, rawArgs[i], rawArgLeadingSpaces[i])
363+
i++
346364
}
347365
} else {
348366
// 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
350370
}
351371

352372
if (node is CommentedOutCommand) {
@@ -357,10 +377,9 @@ abstract class CMakeListsTxtBuilder(
357377
}
358378

359379
// 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))
364383
}
365384

366385
fun elementOriginalText(element: CMakeElement): String {
@@ -404,15 +423,22 @@ abstract class CMakeListsTxtBuilder(
404423

405424
fun setElement(index: Int, element: CMakeElement) {
406425
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+
}
408432
} else {
409433
element.isAddEOL = true
410434
}
435+
411436
myElements[index] = element
412437
}
413438

414439
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
416442
myElements.removeAt(index)
417443
}
418444

@@ -733,10 +759,6 @@ abstract class CMakeListsTxtBuilder(
733759
}
734760
}
735761

736-
fun setCommand(name: String, vararg args: String): CMakeCommand? {
737-
return setCommand(getCommandType(name), Arrays.asList(*args))
738-
}
739-
740762
fun setCommand(name: String, args: Collection<String>): CMakeCommand? {
741763
return setCommand(getCommandType(name), args)
742764
}
@@ -780,7 +802,7 @@ abstract class CMakeListsTxtBuilder(
780802
val command = setCommand(commandType, args)
781803
if (command == null) {
782804
// 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)
784806
addCommand(newCommand)
785807
return newCommand
786808
}
@@ -810,23 +832,23 @@ abstract class CMakeListsTxtBuilder(
810832
// original command, replace it with new
811833
newCommand = CMakeCommand(command)
812834
newCommand.commentOut(false)
813-
newCommand.setArgsWithDefaults(argList)
835+
newCommand.setArgsWithDefaults(argList, command.getArgsLeadingSpaces(argList))
814836
replaceElement(command, newCommand)
815837
} else {
816-
newCommand!!.setArgsWithDefaults(argList)
838+
newCommand!!.setArgsWithDefaults(argList, null)
817839
}
818840
} else {
819841
if (commandType.isMultiple) {
820842
if (commandType.isNoDupeArgs) {
821843
// add another one after this one if the arg value is different
822844
if (!command.allArgsEqual(argList)) {
823-
newCommand = CMakeCommand(command.commandType, argList, true)
845+
newCommand = CMakeCommand(command.commandType, argList, command.getArgsLeadingSpaces(argList), true)
824846
newCommand.commentOut(false)
825847
addElementAfter(command, newCommand)
826848
}
827849
} else {
828850
// add another one after this one
829-
newCommand = CMakeCommand(command.commandType, argList, true)
851+
newCommand = CMakeCommand(command.commandType, argList, null, true)
830852
addElementAfter(command, newCommand)
831853
}
832854
} else {
@@ -840,18 +862,18 @@ abstract class CMakeListsTxtBuilder(
840862
if (command.isOfType(commandType)) {
841863
newCommand = CMakeCommand(command)
842864
newCommand.commentOut(false)
843-
newCommand.setArgsWithDefaults(argList)
865+
newCommand.setArgsWithDefaults(argList, command.getArgsLeadingSpaces(argList))
844866
replaceElement(command, newCommand)
845867
} else {
846868
// is a matched sub-type, need to replace completely
847-
newCommand = CMakeCommand(commandType, argList, false)
869+
newCommand = CMakeCommand(commandType, argList, null, false)
848870
newCommand.commentOut(false)
849-
newCommand.setArgsWithDefaults(argList)
871+
newCommand.setArgsWithDefaults(argList, command.getArgsLeadingSpaces(argList))
850872

851873
replaceElement(command, newCommand)
852874
}
853875
} else {
854-
newCommand!!.setArgsWithDefaults(argList)
876+
newCommand!!.setArgsWithDefaults(argList, null)
855877
}
856878
}
857879
}
@@ -1060,7 +1082,7 @@ abstract class CMakeListsTxtBuilder(
10601082
private val LOG = Logger.getInstance("com.vladsch.clionarduinoplugin.generators")
10611083
private val DEFAULT_OPTIONS = MutableDataSet()
10621084
.set(CMakeParser.AUTO_CONFIG, true)
1063-
.set(CMakeParser.AST_LINE_END_EOL, true)
1085+
.set(CMakeParser.AST_LINE_END_EOL, false)
10641086
.set(CMakeParser.AST_COMMENTS, true)
10651087
.set(CMakeParser.AST_BLANK_LINES, true)
10661088
.set(CMakeParser.AST_ARGUMENT_SEPARATORS, true)

0 commit comments

Comments
 (0)