|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 ghostflyby |
| 3 | + * SPDX-FileCopyrightText: 2026 ghostflyby |
| 4 | + * SPDX-License-Identifier: LGPL-3.0-or-later |
| 5 | + * |
| 6 | + * This file is part of IntelliJ-Plugins by ghostflyby |
| 7 | + * |
| 8 | + * IntelliJ-Plugins by ghostflyby is free software; you can redistribute it and/or |
| 9 | + * modify it under the terms of the GNU Lesser General Public |
| 10 | + * License as published by the Free Software Foundation; either |
| 11 | + * version 3.0 of the License, or (at your option) any later version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | + * Lesser General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Lesser General Public |
| 19 | + * License along with this library; if not, see |
| 20 | + * <https://www.gnu.org/licenses/>. |
| 21 | + */ |
| 22 | + |
| 23 | +package dev.ghostflyby.skills |
| 24 | + |
| 25 | +import com.intellij.openapi.application.QueryExecutorBase |
| 26 | +import com.intellij.psi.PsiDirectory |
| 27 | +import com.intellij.psi.PsiElement |
| 28 | +import com.intellij.psi.PsiReference |
| 29 | +import com.intellij.psi.PsiReferenceBase |
| 30 | +import com.intellij.psi.search.searches.ReferencesSearch |
| 31 | +import com.intellij.psi.util.parentOfType |
| 32 | +import com.intellij.util.Processor |
| 33 | +import org.jetbrains.yaml.YAMLElementGenerator |
| 34 | +import org.jetbrains.yaml.psi.YAMLKeyValue |
| 35 | +import org.jetbrains.yaml.psi.YAMLScalar |
| 36 | + |
| 37 | +internal class SkillDirRenameSearchExecutor : QueryExecutorBase<PsiReference, ReferencesSearch.SearchParameters>() { |
| 38 | + |
| 39 | + override fun processQuery( |
| 40 | + queryParameters: ReferencesSearch.SearchParameters, |
| 41 | + consumer: Processor<in PsiReference>, |
| 42 | + ) { |
| 43 | + val element = queryParameters.elementToSearch |
| 44 | + val dir = element as? PsiDirectory ?: return |
| 45 | + val skillFile = dir.skillMarkdownFile ?: return |
| 46 | + val scalar = skillFile.skillNameScalar() ?: return |
| 47 | + val reference = SkillDirectoryNameReference(scalar, dir) |
| 48 | + if (reference.isReferenceTo(dir)) { |
| 49 | + consumer.process(reference) |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +internal class SkillDirectoryNameReference( |
| 55 | + element: YAMLScalar, |
| 56 | + private val directory: PsiDirectory, |
| 57 | +) : PsiReferenceBase<YAMLScalar>(element, false) { |
| 58 | + override fun resolve(): PsiElement = directory |
| 59 | + |
| 60 | + override fun handleElementRename(newElementName: String): PsiElement? { |
| 61 | + val scalar = directory.skillMarkdownFile?.skillNameScalar() ?: return null |
| 62 | + val gen = YAMLElementGenerator.getInstance(directory.project) |
| 63 | + val kv = scalar.parentOfType<YAMLKeyValue>() ?: return null |
| 64 | + val newKv = gen.createYamlKeyValue(kv.keyText, newElementName) |
| 65 | + val newValue = newKv.value ?: return null |
| 66 | + kv.setValue(newValue) |
| 67 | + return kv.value |
| 68 | + } |
| 69 | +} |
0 commit comments