Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* ZigBrains
* Copyright (C) 2023-2025 FalsePattern
* All Rights Reserved
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, only version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.falsepattern.zigbrains.zig.intentions

import com.falsepattern.zigbrains.ZigBrainsBundle
import com.falsepattern.zigbrains.zig.psi.ZigParamDecl
import com.falsepattern.zigbrains.zig.psi.ZigTypes
import com.falsepattern.zigbrains.zig.psi.ZigVarDeclProto
import com.falsepattern.zigbrains.zig.psi.indentSize
import com.falsepattern.zigbrains.zig.psi.splitString
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.util.PsiUtilCore
import com.intellij.psi.util.elementType
import com.intellij.psi.util.endOffset
import com.intellij.psi.util.parentOfType
import com.intellij.psi.util.startOffset
import okhttp3.internal.delimiterOffset

class AssignToUnderscore: PsiElementBaseIntentionAction() {
init {
text = familyName
}
override fun getFamilyName() = ZigBrainsBundle.message("intention.family.name.assign-to-underscore")

// only available in var/param decls
override fun isAvailable(project: Project, editor: Editor?, element: PsiElement) =
editor != null && (element.parent is ZigVarDeclProto || element.parent is ZigParamDecl) && element.elementType == ZigTypes.IDENTIFIER

override fun invoke(project: Project, editor: Editor?, element: PsiElement) {
editor ?: return
val doc = editor.document
when ( element.parent ) {
is ZigVarDeclProto -> {
val end = element.endOffset
val name = element.text
val seq = doc.immutableCharSequence
// get end of current line
val nl = end + seq.subSequence( end, seq.length ).indexOfFirst { it == '\n' }
// get start of current line
val snl = doc.getLineStartOffset( doc.getLineNumber( end ) ) // start new line
// refcopy the indentation
val indent = seq.subSequence( snl, snl + seq.subSequence( snl, seq.length ).indexOfFirst { !it.isWhitespace() } )
// insert the assignment
doc.insertString( nl, "\n${indent}_ = $name;" )
}
is ZigParamDecl -> { // same as above, but we need to guess the indent and might need to expand `{}` into `{\n\i}`
error("TODO: NOT IMPLEMENTED YET")
}
}
}
}
8 changes: 8 additions & 0 deletions core/src/main/resources/META-INF/zigbrains-core.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@
<category>Zig</category>
</intentionAction>


<!-- QoL things -->
<intentionAction>
<language>Zig</language>
<className>com.falsepattern.zigbrains.zig.intentions.AssignToUnderscore</className>
<category>Zig</category>
</intentionAction>

<!-- Language injection -->
<lang.elementManipulator
forClass="com.falsepattern.zigbrains.zig.psi.ZigStringLiteral"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn foo( bar: Baz ) void {
_ = bar;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fn foo( bar: Baz ) void {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!--
~ ZigBrains
~ Copyright (C) 2023-2025 FalsePattern
~ All Rights Reserved
~
~ The above copyright notice and this permission notice shall be included
~ in all copies or substantial portions of the Software.
~
~ This program is free software: you can redistribute it and/or modify
~ it under the terms of the GNU Lesser General Public License as published by
~ the Free Software Foundation, only version 3 of the License.
~
~ This program is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
~ GNU Lesser General Public License for more details.
~
~ You should have received a copy of the GNU Lesser General Public License
~ along with this program. If not, see <https://www.gnu.org/licenses/>.
-->

<html>
<body>
Assigns the name under the cursor to <code>_</code>.
</body>
</html>
1 change: 1 addition & 0 deletions core/src/main/resources/zigbrains/Bundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ notification.title.direnv-error=Direnv error
notification.content.direnv-error=Could not import direnv: {0}
intention.family.name.make-string-multiline=Convert to multiline
intention.family.name.make-string-quoted=Convert to quoted
intention.family.name.assign-to-underscore=Assign to underscore
zig.file.description=ZigLang file
configurable.name.zig-color-settings-page=Zig
zig.color-settings.bad-char=Bad character
Expand Down