From 780fd6c1de1eec59fc2cac2b7a6ef225908e64b9 Mon Sep 17 00:00:00 2001 From: Irina Fediaeva Date: Sat, 10 Jun 2023 19:10:57 +0300 Subject: [PATCH] PY-61107: Fix rename in Django url segments There are two options for injections in Django routes: DjangoUrlPathFile and DjangoRegexpFile (new), which prevents platform mechanisms from finding references for renaming UrlPathSegments which we've added with Django Endpoint plugin support (PY-49823). Now we add element manipulators to handle content changes and do rename. GitOrigin-RevId: 249d733d82814ab27cf78537b95db28fcdba0ada --- .../codeInsight/regexp/DjangoRegexpFile.kt | 23 +++++++++++++++++++ .../regexp/PythonRegexpParserDefinition.java | 7 ++++-- 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 python/src/com/jetbrains/python/codeInsight/regexp/DjangoRegexpFile.kt diff --git a/python/src/com/jetbrains/python/codeInsight/regexp/DjangoRegexpFile.kt b/python/src/com/jetbrains/python/codeInsight/regexp/DjangoRegexpFile.kt new file mode 100644 index 000000000000..8f86f2333fbe --- /dev/null +++ b/python/src/com/jetbrains/python/codeInsight/regexp/DjangoRegexpFile.kt @@ -0,0 +1,23 @@ +// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package com.jetbrains.python.codeInsight.regexp + +import com.intellij.openapi.fileEditor.FileDocumentManager +import com.intellij.openapi.util.TextRange +import com.intellij.psi.* +import com.intellij.util.IncorrectOperationException +import org.intellij.lang.regexp.RegExpFile + +class DjangoRegexpFile(viewProvider: FileViewProvider) : RegExpFile(viewProvider, PythonRegexpLanguage.INSTANCE), ContributedReferenceHost { + override fun getReferences(): Array = PsiReferenceService.getService().getContributedReferences(this) +} + + +class DjangoRegexpUrlPathFileManipulator : AbstractElementManipulator() { + @Throws(IncorrectOperationException::class) + override fun handleContentChange(file: DjangoRegexpFile, range: TextRange, newContent: String): DjangoRegexpFile { + val document = FileDocumentManager.getInstance().getDocument(file.virtualFile) + document!!.replaceString(range.startOffset, range.endOffset, newContent) + PsiDocumentManager.getInstance(file.project).commitDocument(document) + return file + } +} diff --git a/python/src/com/jetbrains/python/codeInsight/regexp/PythonRegexpParserDefinition.java b/python/src/com/jetbrains/python/codeInsight/regexp/PythonRegexpParserDefinition.java index f6650c488a64..e1225a004794 100644 --- a/python/src/com/jetbrains/python/codeInsight/regexp/PythonRegexpParserDefinition.java +++ b/python/src/com/jetbrains/python/codeInsight/regexp/PythonRegexpParserDefinition.java @@ -7,7 +7,10 @@ import com.intellij.openapi.project.Project; import com.intellij.psi.FileViewProvider; import com.intellij.psi.PsiFile; import com.intellij.psi.tree.IFileElementType; -import org.intellij.lang.regexp.*; +import org.intellij.lang.regexp.RegExpCapability; +import org.intellij.lang.regexp.RegExpLexer; +import org.intellij.lang.regexp.RegExpParser; +import org.intellij.lang.regexp.RegExpParserDefinition; import org.jetbrains.annotations.NotNull; import java.util.EnumSet; @@ -40,6 +43,6 @@ public class PythonRegexpParserDefinition extends RegExpParserDefinition { @Override public @NotNull PsiFile createFile(@NotNull FileViewProvider viewProvider) { - return new RegExpFile(viewProvider, PythonRegexpLanguage.INSTANCE); + return new DjangoRegexpFile(viewProvider); } }