Zum Hauptinhalt springen

Automatische Code-Formatierung in Cursor

tipp

Cursor bietet leistungsstarke Code-Formatierungsfunktionen, die dabei helfen, einen einheitlichen Code-Stil in Ihren Projekten zu gewährleisten.

Formatierungstools einrichten

Integrierter Formatierer

Cursor verfügt über einen integrierten Formatierer, der mehrere Sprachen unterstützt. So verwenden Sie ihn:

  1. Öffnen Sie die Befehlspalette (Strg/Cmd + Umschalt + P)
  2. Geben Sie "Format Document" ein
  3. Drücken Sie Enter

Beliebte Formatierer

# Prettier installieren
npm install --save-dev prettier

# Konfigurationsdatei erstellen
echo {} > .prettierrc.json

Konfigurationsdateien

Prettier-Konfiguration

.prettierrc.json
{
"semi": true,
"trailingComma": "es5",
"singleQuote": true,
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"bracketSpacing": true,
"arrowParens": "avoid"
}

ESLint-Konfiguration

.eslintrc.json
{
"env": {
"browser": true,
"es2021": true
},
"extends": [
"eslint:recommended",
"prettier"
],
"rules": {
"indent": ["error", 2],
"linebreak-style": ["error", "unix"],
"quotes": ["error", "single"],
"semi": ["error", "always"]
}
}

Formatierung beim Speichern

Automatische Formatierung aktivieren

.vscode/settings.json
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}

Sprachspezifische Einstellungen

{
"[python]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "ms-python.python"
},
"[java]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "redhat.java"
}
}

Tastenkombinationen

Standardmäßige Formatierungstastenkombinationen:

AktionWindows/LinuxmacOS
Dokument formatierenUmschalt + Alt + FUmschalt + Option + F
Auswahl formatierenStrg + K Strg + FCmd + K Cmd + F

Erweiterte Konfiguration

Dateien ignorieren

.prettierignore
# Build-Ausgabe
dist/
build/

# Abhängigkeiten
node_modules/

# Generierte Dateien
*.generated.*

ESLint-Integration

.eslintrc.json
{
"extends": [
"eslint:recommended",
"plugin:prettier/recommended"
],
"plugins": ["prettier"],
"rules": {
"prettier/prettier": "error"
}
}

Benutzerdefinierte Formatierungsregeln

Stilrichtlinien durchsetzen

.prettierrc.json
{
"overrides": [
{
"files": "*.component.ts",
"options": {
"printWidth": 120,
"singleQuote": true
}
},
{
"files": "*.spec.ts",
"options": {
"printWidth": 80,
"semi": true
}
}
]
}

Projektspezifische Regeln

.editorconfig
root = true

[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
indent_style = space
indent_size = 2

[*.{js,ts}]
quote_type = single

[*.py]
indent_size = 4

Fehlerbehebung

Häufige Probleme

  1. Formatierung funktioniert nicht

    • Formatierer-Installation prüfen
    • Konfigurationsdateien überprüfen
    • Sicherstellen, dass die Datei nicht ignoriert wird
  2. Konflikte zwischen Tools

    .vscode/settings.json
    {
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
    },
    "eslint.format.enable": false
    }
  3. Leistungsprobleme

    {
    "editor.formatOnSaveTimeout": 5000,
    "editor.formatOnPaste": false
    }

Best Practices

1. Versionskontrolle

.gitignore
# Formatierungskonfigurationen nicht ignorieren
!.prettierrc
!.eslintrc
!.editorconfig

# Cache-Dateien ignorieren
.prettier-cache
.eslintcache

2. Team-Konsistenz

.prettierrc.json
{
"semi": true,
"singleQuote": true,
"trailingComma": "es5",
"printWidth": 100,
"tabWidth": 2
}

3. CI/CD-Integration

.github/workflows/format.yml
name: Code-Formatierung prüfen
on: [push, pull_request]

jobs:
format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Prettier installieren
run: npm install --global prettier
- name: Formatierung prüfen
run: prettier --check "**/*.{js,ts,jsx,tsx,json,css,md}"

Sprachspezifische Konfiguration

JavaScript/TypeScript

.prettierrc.json
{
"semi": true,
"singleQuote": true,
"trailingComma": "es5",
"bracketSpacing": true,
"arrowParens": "avoid"
}

Python

pyproject.toml
[tool.black]
line-length = 88
target-version = ['py38']
include = '\.pyi?$'
exclude = '''
/(
\.git
| \.hg
| \.mypy_cache
| \.tox
| \.venv
| _build
| buck-out
| build
| dist
)/
'''

Java

pom.xml
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<java>
<googleJavaFormat>
<version>1.15.0</version>
<style>GOOGLE</style>
</googleJavaFormat>
</java>
</configuration>
</plugin>

Erweiterte Techniken

Pre-commit Hooks

package.json
{
"scripts": {
"format": "prettier --write \"**/*.{js,ts,jsx,tsx,json,css,md}\"",
"lint": "eslint --fix \"**/*.{js,ts,jsx,tsx}\""
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.{js,ts,jsx,tsx}": [
"eslint --fix",
"prettier --write"
],
"*.{json,css,md}": [
"prettier --write"
]
}
}

Benutzerdefinierte Plugins

.prettierrc.js
module.exports = {
plugins: [
require('prettier-plugin-tailwindcss'),
require('prettier-plugin-organize-imports')
],
semi: true,
singleQuote: true,
organizeImportsSkipDestructiveCodeActions: true
};

Formatierungsregeln teilen

package.json
{
"name": "company-formatting-config",
"version": "1.0.0",
"prettier": {
"semi": true,
"singleQuote": true,
"trailingComma": "es5"
},
"eslintConfig": {
"extends": [
"eslint:recommended",
"plugin:prettier/recommended"
]
}
}

Verwandte Ressourcen