Skip to main content
DSwea.1
Associate III
April 23, 2022
Question

TouchGFX uses spaces in .cproject; STM32CubeIDE uses Tabs

  • April 23, 2022
  • 4 replies
  • 1459 views

When TouchGFX generates code to a project's .cproject, it uses spaces. When STM32CubeIDE saves to .cproject, spaces are replaced by Tabs. This causes Git (SourceTree) to label every line in an otherwise identical project file as changed. The actions of the two environments need to be coordinated such that both use the same whitespace characters (preferably spaces).

4 replies

Piranha
Principal III
April 23, 2022

C'mon, STM32CubeIDE (Eclipse) is an IDE and should be capable of dealing with both.

https://stackoverflow.com/questions/407929/how-do-i-change-eclipse-to-use-spaces-instead-of-tabs

DSwea.1
DSwea.1Author
Associate III
April 24, 2022

Thanks for the suggestion. I'm aware that Eclipse can be configured to use spaces, and I've been setting my code style preferences for C/C++, Java, xml, etc. for many years to use spaces (in both Eclipse proper, and in a number of manufacturer IDEs based on Eclipse). However, in STM32CubeIDE, the .cproject file (an XML file), the IDE does not apply Tabs vs Spaces settings when saving the .cproject file. Indeed, an XML editor is not installed in the IDE by default. I don't care if it's tabs or spaces---I'm simply asking that ST coordinate its tools so that one need not deal with this issue.

ferro
Lead
March 3, 2025

https://community.st.com/t5/stm32cubeide-mcus/cube-1-16-1-and-gfxdesigner-4-24-1-generates-different-cproject/m-p/778202/highlight/true#M34644

 

This python script fixes the .cproject

#
# Tested on Windows, Cube 1.17, Gfx 4.24.1
# This script processes a `.cproject` file with the following modifications:
# 1. Replaces every two-space indentation with a tab.
# 2. Uses CRLF (`\r\n`) as the newline format.
# 3. Merges lines 2 and 3 into line 2 with no extra characters between them.
# 4. Ensures the last line is exactly `</cproject>`, with no EOF or trailing newline.
#

import os

def process_cproject_file(filename):
 with open(filename, 'r', encoding='utf-8') as file:
 content = file.read()
 
 # Force CRLF as the newline type
 newline_type = '\r\n'
 
 # Split into lines while preserving newline characters
 lines = content.splitlines()
 
 # Replace leading spaces with corresponding number of tabs
 processed_lines = []
 for line in lines:
 leading_spaces = len(line) - len(line.lstrip())
 tabs = '\t' * (leading_spaces // 2) # Each two spaces become one tab
 processed_lines.append(tabs + line.lstrip())
 
 # Combine lines 2 and 3 into line 2 with no extra space
 if len(processed_lines) > 2:
 processed_lines[1] = processed_lines[1].rstrip() + processed_lines[2].lstrip()
 del processed_lines[2]
 
 # Ensure last element is exactly '</cproject>' with no extra newlines or EOF
 processed_lines[-1] = '</cproject>'
 
 # Write back changes using CRLF and ensure no trailing newline
 with open(filename, 'w', encoding='utf-8', newline='') as file:
 file.write(newline_type.join(processed_lines))
 
if __name__ == "__main__":
 process_cproject_file(".cproject")

 

Lead II
March 3, 2025

This is a known issue:
https://community.st.com/t5/stm32-mcus-touchgfx-and-gui/touchgfx-designer-adds-redundant-line-ending-to-cproject-file/td-p/674111
Last version I tested it with was 4.24.1. Maybe it's fixed in the newer version? I haven't checked yet. Please check.

"Kudo posts if you have the same problem and kudo replies if the solution works.Click ""Accept as Solution"" if a reply solved your problem. If no solution was posted please answer with your own."