From 955e622c1a21e9cd6c699055624da710d9daa776 Mon Sep 17 00:00:00 2001 From: DanTrackpaw Date: Fri, 10 Apr 2026 13:19:30 +0200 Subject: [PATCH] Add custom KiCad autocomplete list settings --- .gitignore | 9 ++- docs/usage/eda_integration.md | 1 + public/kicad/footprints_custom.txt | 1 + public/kicad/symbols_custom.txt | 1 + src/Controller/KicadListEditorController.php | 26 +++++-- .../Part/EDA/KicadFieldAutocompleteType.php | 14 +++- src/Form/Settings/KicadListEditorType.php | 51 ++++++++++++-- src/Services/EDA/KicadListFileManager.php | 18 ++++- .../MiscSettings/KiCadEDASettings.php | 6 ++ .../settings/kicad_list_editor.html.twig | 15 ++++- .../KicadListEditorControllerTest.php | 67 +++++++++++++++++-- translations/messages.en.xlf | 46 ++++++++++++- 12 files changed, 231 insertions(+), 24 deletions(-) create mode 100644 public/kicad/footprints_custom.txt create mode 100644 public/kicad/symbols_custom.txt diff --git a/.gitignore b/.gitignore index dd5c43db..90f5c11a 100644 --- a/.gitignore +++ b/.gitignore @@ -50,4 +50,11 @@ phpstan.neon ###< phpstan/phpstan ### .claude/ -CLAUDE.md \ No newline at end of file +CLAUDE.md + +.codex +migrations/.codex +docker-data/ +scripts/ +db/ +docker-compose.yaml diff --git a/docs/usage/eda_integration.md b/docs/usage/eda_integration.md index b99ed4dd..92b1244d 100644 --- a/docs/usage/eda_integration.md +++ b/docs/usage/eda_integration.md @@ -67,6 +67,7 @@ You can define this on a per-part basis using the KiCad symbol and KiCad footpri For example, to configure the values for a BC547 transistor you would put `Transistor_BJT:BC547` in the part's KiCad symbol field to give it the right schematic symbol in Eeschema and `Package_TO_SOT_THT:TO-92` to give it the right footprint in Pcbnew. If you type in a character, you will get an autocomplete list of all symbols and footprints available in the KiCad standard library. You can also input your own value. +If you want to keep custom suggestions across updates, open the server settings page and use the "Autocomplete settings" page. There you can edit `public/kicad/footprints_custom.txt` and `public/kicad/symbols_custom.txt` and enable the "Use custom autocomplete lists" option to use those files instead of the autogenerated defaults. ### Parts and category visibility diff --git a/public/kicad/footprints_custom.txt b/public/kicad/footprints_custom.txt new file mode 100644 index 00000000..c3a29c90 --- /dev/null +++ b/public/kicad/footprints_custom.txt @@ -0,0 +1 @@ +# Custom KiCad autocomplete entries. One entry per line. diff --git a/public/kicad/symbols_custom.txt b/public/kicad/symbols_custom.txt new file mode 100644 index 00000000..c3a29c90 --- /dev/null +++ b/public/kicad/symbols_custom.txt @@ -0,0 +1 @@ +# Custom KiCad autocomplete entries. One entry per line. diff --git a/src/Controller/KicadListEditorController.php b/src/Controller/KicadListEditorController.php index ea9289db..85ca0a28 100644 --- a/src/Controller/KicadListEditorController.php +++ b/src/Controller/KicadListEditorController.php @@ -23,7 +23,10 @@ declare(strict_types=1); namespace App\Controller; use App\Form\Settings\KicadListEditorType; +use App\Settings\MiscSettings\KiCadEDASettings; use App\Services\EDA\KicadListFileManager; +use Jbtronics\SettingsBundle\Exception\SettingsNotValidException; +use Jbtronics\SettingsBundle\Manager\SettingsManagerInterface; use RuntimeException; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; @@ -34,14 +37,26 @@ use function Symfony\Component\Translation\t; final class KicadListEditorController extends AbstractController { + public function __construct( + private readonly SettingsManagerInterface $settingsManager, + ) { + } + #[Route('/settings/misc/kicad-lists', name: 'settings_kicad_lists')] public function __invoke(Request $request, KicadListFileManager $fileManager): Response { + $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); $this->denyAccessUnlessGranted('@config.change_system_settings'); + /** @var KiCadEDASettings $settings */ + $settings = $this->settingsManager->createTemporaryCopy(KiCadEDASettings::class); $form = $this->createForm(KicadListEditorType::class, [ - 'footprints' => $fileManager->getFootprintsContent(), - 'symbols' => $fileManager->getSymbolsContent(), + 'useCustomList' => $settings->useCustomList, + 'customFootprints' => $fileManager->getCustomFootprintsContent(), + 'customSymbols' => $fileManager->getCustomSymbolsContent(), + ], [ + 'default_footprints' => $fileManager->getFootprintsContent(), + 'default_symbols' => $fileManager->getSymbolsContent(), ]); $form->handleRequest($request); @@ -50,11 +65,14 @@ final class KicadListEditorController extends AbstractController $data = $form->getData(); try { - $fileManager->save($data['footprints'], $data['symbols']); + $fileManager->saveCustom($data['customFootprints'], $data['customSymbols']); + $settings->useCustomList = (bool) $data['useCustomList']; + $this->settingsManager->mergeTemporaryCopy($settings); + $this->settingsManager->save($settings); $this->addFlash('success', t('settings.flash.saved')); return $this->redirectToRoute('settings_kicad_lists'); - } catch (RuntimeException $exception) { + } catch (RuntimeException|SettingsNotValidException $exception) { $this->addFlash('error', $exception->getMessage()); } } diff --git a/src/Form/Part/EDA/KicadFieldAutocompleteType.php b/src/Form/Part/EDA/KicadFieldAutocompleteType.php index 50de81d0..8a7b0313 100644 --- a/src/Form/Part/EDA/KicadFieldAutocompleteType.php +++ b/src/Form/Part/EDA/KicadFieldAutocompleteType.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace App\Form\Part\EDA; use App\Form\Type\StaticFileAutocompleteType; +use App\Settings\MiscSettings\KiCadEDASettings; use Symfony\Component\Form\AbstractType; use Symfony\Component\OptionsResolver\Options; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -39,6 +40,13 @@ class KicadFieldAutocompleteType extends AbstractType //Do not use a leading slash here! otherwise it will not work under prefixed reverse proxies public const FOOTPRINT_PATH = 'kicad/footprints.txt'; public const SYMBOL_PATH = 'kicad/symbols.txt'; + public const CUSTOM_FOOTPRINT_PATH = 'kicad/footprints_custom.txt'; + public const CUSTOM_SYMBOL_PATH = 'kicad/symbols_custom.txt'; + + public function __construct( + private readonly KiCadEDASettings $kiCadEDASettings, + ) { + } public function configureOptions(OptionsResolver $resolver): void { @@ -47,8 +55,8 @@ class KicadFieldAutocompleteType extends AbstractType $resolver->setDefaults([ 'file' => fn(Options $options) => match ($options['type']) { - self::TYPE_FOOTPRINT => self::FOOTPRINT_PATH, - self::TYPE_SYMBOL => self::SYMBOL_PATH, + self::TYPE_FOOTPRINT => $this->kiCadEDASettings->useCustomList ? self::CUSTOM_FOOTPRINT_PATH : self::FOOTPRINT_PATH, + self::TYPE_SYMBOL => $this->kiCadEDASettings->useCustomList ? self::CUSTOM_SYMBOL_PATH : self::SYMBOL_PATH, default => throw new \InvalidArgumentException('Invalid type'), } ]); @@ -58,4 +66,4 @@ class KicadFieldAutocompleteType extends AbstractType { return StaticFileAutocompleteType::class; } -} \ No newline at end of file +} diff --git a/src/Form/Settings/KicadListEditorType.php b/src/Form/Settings/KicadListEditorType.php index 3c933aee..5cbb8df4 100644 --- a/src/Form/Settings/KicadListEditorType.php +++ b/src/Form/Settings/KicadListEditorType.php @@ -23,17 +23,24 @@ declare(strict_types=1); namespace App\Form\Settings; use Symfony\Component\Form\AbstractType; +use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\TextareaType; use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\OptionsResolver\OptionsResolver; final class KicadListEditorType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options): void { $builder - ->add('footprints', TextareaType::class, [ - 'label' => 'settings.misc.kicad_eda.editor.footprints', + ->add('useCustomList', CheckboxType::class, [ + 'label' => 'settings.misc.kicad_eda.use_custom_list', + 'help' => 'settings.misc.kicad_eda.use_custom_list.help', + 'required' => false, + ]) + ->add('customFootprints', TextareaType::class, [ + 'label' => 'settings.misc.kicad_eda.editor.custom_footprints', 'help' => 'settings.misc.kicad_eda.editor.footprints.help', 'attr' => [ 'rows' => 16, @@ -41,8 +48,21 @@ final class KicadListEditorType extends AbstractType 'class' => 'font-monospace', ], ]) - ->add('symbols', TextareaType::class, [ - 'label' => 'settings.misc.kicad_eda.editor.symbols', + ->add('defaultFootprints', TextareaType::class, [ + 'label' => 'settings.misc.kicad_eda.editor.default_footprints', + 'help' => 'settings.misc.kicad_eda.editor.default_files_help', + 'disabled' => true, + 'mapped' => false, + 'data' => $options['default_footprints'], + 'attr' => [ + 'rows' => 16, + 'spellcheck' => 'false', + 'class' => 'font-monospace', + 'readonly' => 'readonly', + ], + ]) + ->add('customSymbols', TextareaType::class, [ + 'label' => 'settings.misc.kicad_eda.editor.custom_symbols', 'help' => 'settings.misc.kicad_eda.editor.symbols.help', 'attr' => [ 'rows' => 16, @@ -50,8 +70,31 @@ final class KicadListEditorType extends AbstractType 'class' => 'font-monospace', ], ]) + ->add('defaultSymbols', TextareaType::class, [ + 'label' => 'settings.misc.kicad_eda.editor.default_symbols', + 'help' => 'settings.misc.kicad_eda.editor.default_files_help', + 'disabled' => true, + 'mapped' => false, + 'data' => $options['default_symbols'], + 'attr' => [ + 'rows' => 16, + 'spellcheck' => 'false', + 'class' => 'font-monospace', + 'readonly' => 'readonly', + ], + ]) ->add('save', SubmitType::class, [ 'label' => 'save', ]); } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefaults([ + 'default_footprints' => '', + 'default_symbols' => '', + ]); + $resolver->setAllowedTypes('default_footprints', 'string'); + $resolver->setAllowedTypes('default_symbols', 'string'); + } } diff --git a/src/Services/EDA/KicadListFileManager.php b/src/Services/EDA/KicadListFileManager.php index 50922510..a293aad5 100644 --- a/src/Services/EDA/KicadListFileManager.php +++ b/src/Services/EDA/KicadListFileManager.php @@ -29,6 +29,8 @@ final class KicadListFileManager { private const FOOTPRINTS_PATH = '/public/kicad/footprints.txt'; private const SYMBOLS_PATH = '/public/kicad/symbols.txt'; + private const CUSTOM_FOOTPRINTS_PATH = '/public/kicad/footprints_custom.txt'; + private const CUSTOM_SYMBOLS_PATH = '/public/kicad/symbols_custom.txt'; public function __construct( #[Autowire('%kernel.project_dir%')] @@ -41,15 +43,25 @@ final class KicadListFileManager return $this->readFile(self::FOOTPRINTS_PATH); } + public function getCustomFootprintsContent(): string + { + return $this->readFile(self::CUSTOM_FOOTPRINTS_PATH); + } + public function getSymbolsContent(): string { return $this->readFile(self::SYMBOLS_PATH); } - public function save(string $footprints, string $symbols): void + public function getCustomSymbolsContent(): string { - $this->writeFile(self::FOOTPRINTS_PATH, $this->normalizeContent($footprints)); - $this->writeFile(self::SYMBOLS_PATH, $this->normalizeContent($symbols)); + return $this->readFile(self::CUSTOM_SYMBOLS_PATH); + } + + public function saveCustom(string $footprints, string $symbols): void + { + $this->writeFile(self::CUSTOM_FOOTPRINTS_PATH, $this->normalizeContent($footprints)); + $this->writeFile(self::CUSTOM_SYMBOLS_PATH, $this->normalizeContent($symbols)); } private function readFile(string $path): string diff --git a/src/Settings/MiscSettings/KiCadEDASettings.php b/src/Settings/MiscSettings/KiCadEDASettings.php index cf31bd95..dd223007 100644 --- a/src/Settings/MiscSettings/KiCadEDASettings.php +++ b/src/Settings/MiscSettings/KiCadEDASettings.php @@ -62,4 +62,10 @@ class KiCadEDASettings )] public bool $defaultOrderdetailsVisibility = false; + + #[SettingsParameter( + label: new TM("settings.misc.kicad_eda.use_custom_list"), + description: new TM("settings.misc.kicad_eda.use_custom_list.help"), + )] + public bool $useCustomList = false; } diff --git a/templates/settings/kicad_list_editor.html.twig b/templates/settings/kicad_list_editor.html.twig index d83c31ed..33ff00ec 100644 --- a/templates/settings/kicad_list_editor.html.twig +++ b/templates/settings/kicad_list_editor.html.twig @@ -10,8 +10,19 @@

{{ form_start(form) }} - {{ form_row(form.footprints) }} - {{ form_row(form.symbols) }} + {{ form_row(form.useCustomList) }} + +
+
+ {{ form_row(form.customFootprints) }} + {{ form_row(form.customSymbols) }} +
+
+ {{ form_row(form.defaultFootprints) }} + {{ form_row(form.defaultSymbols) }} +
+
+ {{ form_row(form.save) }} {{ form_end(form) }} {% endblock %} diff --git a/tests/Controller/KicadListEditorControllerTest.php b/tests/Controller/KicadListEditorControllerTest.php index 98b091a2..0aa05aa1 100644 --- a/tests/Controller/KicadListEditorControllerTest.php +++ b/tests/Controller/KicadListEditorControllerTest.php @@ -23,6 +23,8 @@ declare(strict_types=1); namespace App\Tests\Controller; use App\Entity\UserSystem\User; +use App\Settings\MiscSettings\KiCadEDASettings; +use Jbtronics\SettingsBundle\Manager\SettingsManagerInterface; use PHPUnit\Framework\Attributes\Group; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; @@ -32,8 +34,13 @@ final class KicadListEditorControllerTest extends WebTestCase { private string $footprintsPath; private string $symbolsPath; + private string $customFootprintsPath; + private string $customSymbolsPath; private string $originalFootprints; private string $originalSymbols; + private string $originalCustomFootprints; + private string $originalCustomSymbols; + private bool $originalUseCustomList; protected function setUp(): void { @@ -42,14 +49,37 @@ final class KicadListEditorControllerTest extends WebTestCase $projectDir = dirname(__DIR__, 2); $this->footprintsPath = $projectDir . '/public/kicad/footprints.txt'; $this->symbolsPath = $projectDir . '/public/kicad/symbols.txt'; + $this->customFootprintsPath = $projectDir . '/public/kicad/footprints_custom.txt'; + $this->customSymbolsPath = $projectDir . '/public/kicad/symbols_custom.txt'; $this->originalFootprints = (string) file_get_contents($this->footprintsPath); $this->originalSymbols = (string) file_get_contents($this->symbolsPath); + $this->originalCustomFootprints = is_file($this->customFootprintsPath) ? (string) file_get_contents($this->customFootprintsPath) : ''; + $this->originalCustomSymbols = is_file($this->customSymbolsPath) ? (string) file_get_contents($this->customSymbolsPath) : ''; + + static::bootKernel(); + /** @var SettingsManagerInterface $settingsManager */ + $settingsManager = static::getContainer()->get(SettingsManagerInterface::class); + /** @var KiCadEDASettings $settings */ + $settings = $settingsManager->get(KiCadEDASettings::class); + $this->originalUseCustomList = $settings->useCustomList; + static::ensureKernelShutdown(); } protected function tearDown(): void { file_put_contents($this->footprintsPath, $this->originalFootprints); file_put_contents($this->symbolsPath, $this->originalSymbols); + file_put_contents($this->customFootprintsPath, $this->originalCustomFootprints); + file_put_contents($this->customSymbolsPath, $this->originalCustomSymbols); + + static::bootKernel(); + /** @var SettingsManagerInterface $settingsManager */ + $settingsManager = static::getContainer()->get(SettingsManagerInterface::class); + /** @var KiCadEDASettings $settings */ + $settings = $settingsManager->get(KiCadEDASettings::class); + $settings->useCustomList = $this->originalUseCustomList; + $settingsManager->save($settings); + static::ensureKernelShutdown(); parent::tearDown(); } @@ -73,21 +103,48 @@ final class KicadListEditorControllerTest extends WebTestCase $this->assertSelectorExists('form[name="kicad_list_editor"]'); } - public function testEditorSavesFiles(): void + public function testEditorShowsDefaultAndCustomFiles(): void + { + $client = static::createClient(); + $this->loginAsUser($client, 'admin'); + + file_put_contents($this->footprintsPath, "DefaultFootprint\n"); + file_put_contents($this->symbolsPath, "DefaultSymbol\n"); + file_put_contents($this->customFootprintsPath, "CustomFootprint\n"); + file_put_contents($this->customSymbolsPath, "CustomSymbol\n"); + + $crawler = $client->request('GET', '/en/settings/misc/kicad-lists'); + + $this->assertSame("CustomFootprint\n", $crawler->filter('#kicad_list_editor_customFootprints')->getNode(0)->nodeValue); + $this->assertSame("CustomSymbol\n", $crawler->filter('#kicad_list_editor_customSymbols')->getNode(0)->nodeValue); + $this->assertSame("DefaultFootprint\n", $crawler->filter('#kicad_list_editor_defaultFootprints')->getNode(0)->nodeValue); + $this->assertSame("DefaultSymbol\n", $crawler->filter('#kicad_list_editor_defaultSymbols')->getNode(0)->nodeValue); + } + + public function testEditorSavesCustomFilesAndSetting(): void { $client = static::createClient(); $this->loginAsUser($client, 'admin'); $crawler = $client->request('GET', '/en/settings/misc/kicad-lists'); $form = $crawler->filter('form[name="kicad_list_editor"]')->form(); - $form['kicad_list_editor[footprints]'] = "Package_DIP:DIP-8_W7.62mm\n"; - $form['kicad_list_editor[symbols]'] = "Device:R\n"; + $form['kicad_list_editor[customFootprints]'] = "Package_DIP:DIP-8_W7.62mm\n"; + $form['kicad_list_editor[customSymbols]'] = "Device:R\n"; + $form['kicad_list_editor[useCustomList]']->tick(); $client->submit($form); $this->assertResponseRedirects('/en/settings/misc/kicad-lists'); - $this->assertSame("Package_DIP:DIP-8_W7.62mm\n", (string) file_get_contents($this->footprintsPath)); - $this->assertSame("Device:R\n", (string) file_get_contents($this->symbolsPath)); + $this->assertSame("Package_DIP:DIP-8_W7.62mm\n", (string) file_get_contents($this->customFootprintsPath)); + $this->assertSame("Device:R\n", (string) file_get_contents($this->customSymbolsPath)); + $this->assertSame($this->originalFootprints, (string) file_get_contents($this->footprintsPath)); + $this->assertSame($this->originalSymbols, (string) file_get_contents($this->symbolsPath)); + + /** @var SettingsManagerInterface $settingsManager */ + $settingsManager = $client->getContainer()->get(SettingsManagerInterface::class); + /** @var KiCadEDASettings $settings */ + $settings = $settingsManager->reload(KiCadEDASettings::class); + $this->assertTrue($settings->useCustomList); } private function loginAsUser($client, string $username): void diff --git a/translations/messages.en.xlf b/translations/messages.en.xlf index 656c7ecf..177d7fd9 100644 --- a/translations/messages.en.xlf +++ b/translations/messages.en.xlf @@ -10038,13 +10038,13 @@ Please note, that you can not impersonate a disabled user. If you try you will g settings.misc.kicad_eda.editor.link - Edit autocomplete lists + Autocomplete settings settings.misc.kicad_eda.editor.description - Edit the contents of public/kicad/footprints.txt and public/kicad/symbols.txt. These files are used by the KiCad autocomplete fields in the admin UI. + Configure whether KiCad autocomplete uses the autogenerated default lists or your custom override files. The custom files are editable here, while the default files are shown read-only for reference. @@ -10071,6 +10071,48 @@ Please note, that you can not impersonate a disabled user. If you try you will g One entry per line. Used as autocomplete suggestions for KiCad symbol fields. + + + settings.misc.kicad_eda.use_custom_list + Use custom autocomplete lists + + + + + settings.misc.kicad_eda.use_custom_list.help + When enabled, KiCad autocomplete uses public/kicad/footprints_custom.txt and public/kicad/symbols_custom.txt instead of the autogenerated default files. + + + + + settings.misc.kicad_eda.editor.custom_footprints + Custom footprints list + + + + + settings.misc.kicad_eda.editor.custom_symbols + Custom symbols list + + + + + settings.misc.kicad_eda.editor.default_footprints + Default footprints list + + + + + settings.misc.kicad_eda.editor.default_symbols + Default symbols list + + + + + settings.misc.kicad_eda.editor.default_files_help + Autogenerated file shown for reference only. Changes must be made in the custom list. + + settings.behavior.sidebar