Skip to content

Commit

Permalink
fix not being able to inject in-fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
cottand committed Jul 7, 2024
1 parent 6d88fd6 commit d8086c8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class NixStringLiteralEscaper(host: AbstractNixString) : LiteralTextEscaper<PsiL
override fun getOffsetInHost(offsetInDecoded: Int, rangeInsideHost: TextRange): Int {
val offsets = outSourceOffsets ?: throw IllegalStateException("#decode was not called")
val result = if (offsetInDecoded < offsets.size) offsets[offsetInDecoded] else -1
return result.coerceIn(2..rangeInsideHost.length) + rangeInsideHost.startOffset
return result.coerceIn(0..rangeInsideHost.length) + rangeInsideHost.startOffset
}

companion object {
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/nixos/idea/psi/NixStringManipulator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ class NixStringManipulator : AbstractElementManipulator<NixString>() {
element: NixString,
range: TextRange,
newContent: String
): NixString {
return element
): NixString? {
val escaped = newContent
val replacement = range.replace(element.text, escaped)
return element.updateText(replacement) as? NixString
}

override fun getRangeInElement(element: NixString): TextRange = when {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/nixos/idea/psi/impl/AbstractNixString.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.intellij.lang.ASTNode
import com.intellij.openapi.diagnostic.Logger
import com.intellij.psi.PsiLanguageInjectionHost
import com.intellij.psi.impl.source.tree.LeafElement
import com.intellij.psi.impl.source.tree.LeafPsiElement
import com.intellij.util.IncorrectOperationException
import org.nixos.idea.psi.NixIndString
import org.nixos.idea.psi.NixString
Expand All @@ -20,7 +21,8 @@ abstract class AbstractNixString(private val astNode: ASTNode) : PsiLanguageInje
LOG.info("not a nix ind string")
return this
}
(astNode.firstChildNode as LeafElement).replaceWithText(s)
(astNode.firstChildNode.treeNext.firstChildNode as? LeafPsiElement)
?.replaceWithText(s)
return this
}

Expand Down

0 comments on commit d8086c8

Please sign in to comment.