From 974e4603c088a28972f4e5f16caad3a32f43756e Mon Sep 17 00:00:00 2001 From: UNV Date: Wed, 4 Mar 2026 17:48:06 +0300 Subject: [PATCH] Replacing StringUtil.[un]escapeStringCharacters with more optimized StringEscapeUtil.[un]escape. Some refactoring. --- .../python/impl/buildout/BuildoutFacet.java | 8 +- .../PyConvertTripleQuotedStringIntention.java | 11 +- .../extension/BuildoutModuleExtension.java | 487 +++++++++--------- 3 files changed, 253 insertions(+), 253 deletions(-) diff --git a/python-impl/src/main/java/com/jetbrains/python/impl/buildout/BuildoutFacet.java b/python-impl/src/main/java/com/jetbrains/python/impl/buildout/BuildoutFacet.java index b268498e..32efe08c 100644 --- a/python-impl/src/main/java/com/jetbrains/python/impl/buildout/BuildoutFacet.java +++ b/python-impl/src/main/java/com/jetbrains/python/impl/buildout/BuildoutFacet.java @@ -19,8 +19,8 @@ /** * Facet for buildout support. * Knows which script in bin/ contains paths we want to add. - * User: dcheryasov - * Date: Jul 25, 2010 3:23:50 PM + * @author dcheryasov + * @since 2010-07-25 */ public class BuildoutFacet /*extends Facet implements PythonPathContributingFacet, LibraryContributingFacet */{ @@ -201,8 +201,8 @@ public static List extractFromScript(@NotNull VirtualFile script) throws if (line.endsWith(",")) { line = line.substring(0, line.length() - 1); } - if (line.startsWith("'") && line.endsWith("'")) { - result.add(StringUtil.unescapeStringCharacters(line.substring(1, line.length() - 1))); + if (StringEscapeUtil.isQuoted(line, '\'')) { + result.add(StringEscapeUtil.unescape(line, 1, line.length() - 1)); } index++; } diff --git a/python-impl/src/main/java/com/jetbrains/python/impl/codeInsight/intentions/PyConvertTripleQuotedStringIntention.java b/python-impl/src/main/java/com/jetbrains/python/impl/codeInsight/intentions/PyConvertTripleQuotedStringIntention.java index 07c0c8df..50ace3a1 100644 --- a/python-impl/src/main/java/com/jetbrains/python/impl/codeInsight/intentions/PyConvertTripleQuotedStringIntention.java +++ b/python-impl/src/main/java/com/jetbrains/python/impl/codeInsight/intentions/PyConvertTripleQuotedStringIntention.java @@ -17,6 +17,8 @@ import com.jetbrains.python.impl.psi.impl.PyStringLiteralExpressionImpl; import com.jetbrains.python.psi.*; +import consulo.annotation.access.RequiredReadAction; +import consulo.annotation.access.RequiredWriteAction; import consulo.codeEditor.Editor; import consulo.language.editor.intention.BaseIntentionAction; import consulo.language.psi.PsiElement; @@ -26,6 +28,7 @@ import consulo.localize.LocalizeValue; import consulo.project.Project; import consulo.python.impl.localize.PyLocalize; +import consulo.util.lang.StringEscapeUtil; import consulo.util.lang.StringUtil; import jakarta.annotation.Nonnull; @@ -55,6 +58,8 @@ public LocalizeValue getText() { return PyLocalize.intnTripleQuotedString(); } + @Override + @RequiredReadAction public boolean isAvailable(@Nonnull Project project, Editor editor, PsiFile file) { if (!(file instanceof PyFile)) { return false; @@ -82,6 +87,8 @@ public boolean isAvailable(@Nonnull Project project, Editor editor, PsiFile file return false; } + @Override + @RequiredWriteAction public void invoke(@Nonnull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { PyStringLiteralExpression string = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyStringLiteralExpression.class); @@ -142,9 +149,7 @@ else if (s.endsWith("'''") || s.endsWith("\"\"\"")) { subString = convertToValidSubString(trimmed.substring(0, trimmed.length() - 3), firstQuote); } else { - StringBuilder stringBuilder = new StringBuilder(); - stringBuilder = StringUtil.escapeStringCharacters(s.length(), s, String.valueOf(firstQuote), true, stringBuilder); - subString = stringBuilder.toString(); + subString = StringEscapeUtil.escape(s, firstQuote); } return subString; } diff --git a/python-impl/src/main/java/consulo/python/buildout/module/extension/BuildoutModuleExtension.java b/python-impl/src/main/java/consulo/python/buildout/module/extension/BuildoutModuleExtension.java index 177142d1..733d314a 100644 --- a/python-impl/src/main/java/consulo/python/buildout/module/extension/BuildoutModuleExtension.java +++ b/python-impl/src/main/java/consulo/python/buildout/module/extension/BuildoutModuleExtension.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package consulo.python.buildout.module.extension; import com.jetbrains.python.impl.PythonHelpersLocator; @@ -22,8 +21,8 @@ import com.jetbrains.python.impl.facet.PythonPathContributingFacet; import com.jetbrains.python.impl.run.PythonCommandLineState; import com.jetbrains.python.impl.sdk.PythonEnvUtil; +import consulo.annotation.access.RequiredReadAction; import consulo.application.util.LineTokenizer; -import consulo.application.util.SystemInfo; import consulo.ide.impl.idea.openapi.vfs.VfsUtil; import consulo.language.util.ModuleUtilCore; import consulo.logging.Logger; @@ -31,21 +30,21 @@ import consulo.module.ModuleManager; import consulo.module.content.layer.ModuleRootLayer; import consulo.module.content.layer.extension.ModuleExtensionBase; +import consulo.platform.Platform; import consulo.process.cmd.GeneralCommandLine; import consulo.process.cmd.ParametersList; import consulo.process.cmd.ParamsGroup; import consulo.project.Project; import consulo.project.ProjectManager; import consulo.util.io.FileUtil; +import consulo.util.lang.StringEscapeUtil; import consulo.util.lang.StringUtil; import consulo.virtualFileSystem.LocalFileSystem; import consulo.virtualFileSystem.VirtualFile; -import org.jetbrains.annotations.NonNls; - import jakarta.annotation.Nonnull; import jakarta.annotation.Nullable; + import java.io.File; -import java.io.FilenameFilter; import java.io.IOException; import java.util.*; import java.util.regex.Matcher; @@ -53,288 +52,284 @@ /** * @author VISTALL - * @since 20.10.13. + * @since 2013-10-20 */ public class BuildoutModuleExtension extends ModuleExtensionBase implements PythonPathContributingFacet, LibraryContributingFacet { - private static final Logger LOGGER = Logger.getInstance(BuildoutModuleExtension.class); + private static final Logger LOGGER = Logger.getInstance(BuildoutModuleExtension.class); - @Nonnull - public static List getExtraPathForAllOpenModules() { - List results = new ArrayList<>(); - for (Project project : ProjectManager.getInstance().getOpenProjects()) { - for (Module module : ModuleManager.getInstance(project).getModules()) { - BuildoutModuleExtension buildoutFacet = ModuleUtilCore.getExtension(module, BuildoutModuleExtension.class); - if (buildoutFacet != null) { - List paths = buildoutFacet.getPaths(); - if (paths != null) { - for (String path : paths) { - VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByPath(path); - if (file != null) { - results.add(file); - } + @Nonnull + public static List getExtraPathForAllOpenModules() { + List results = new ArrayList<>(); + for (Project project : ProjectManager.getInstance().getOpenProjects()) { + for (Module module : ModuleManager.getInstance(project).getModules()) { + BuildoutModuleExtension buildoutFacet = ModuleUtilCore.getExtension(module, BuildoutModuleExtension.class); + if (buildoutFacet != null) { + List paths = buildoutFacet.getPaths(); + if (paths != null) { + for (String path : paths) { + VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByPath(path); + if (file != null) { + results.add(file); + } + } + } + } } - } } - } + return results; } - return results; - } - - @NonNls - public static final String BUILDOUT_CFG = "buildout.cfg"; - @NonNls - public static final String SCRIPT_SUFFIX = "-script"; - private static final String BUILDOUT_LIB_NAME = "Buildout Eggs"; - protected String myScriptName; - protected List myPaths; - public BuildoutModuleExtension(@Nonnull String id, @Nonnull ModuleRootLayer module) { - super(id, module); - } + public static final String BUILDOUT_CFG = "buildout.cfg"; + public static final String SCRIPT_SUFFIX = "-script"; + private static final String BUILDOUT_LIB_NAME = "Buildout Eggs"; + protected String myScriptName; + protected List myPaths; - public static List getScripts(@Nullable BuildoutModuleExtension buildoutFacet, VirtualFile baseDir) { - File rootPath = null; - if (buildoutFacet != null) { - File configIOFile = buildoutFacet.getConfigFile(); - if (configIOFile != null) { - rootPath = configIOFile.getParentFile(); - } - } - if (rootPath == null || !rootPath.exists()) { - if (baseDir != null) { - rootPath = new File(baseDir.getPath()); - } + public BuildoutModuleExtension(@Nonnull String id, @Nonnull ModuleRootLayer module) { + super(id, module); } - if (rootPath != null) { - File[] scripts = new File(rootPath, "bin").listFiles(new FilenameFilter() { - @Override - public boolean accept(File dir, String name) { - if (SystemInfo.isWindows) { - return name.endsWith("-script.py"); - } - String ext = FileUtil.getExtension(name); - return ext.length() == 0 || FileUtil.namesEqual(ext, "py"); + + public static List getScripts(@Nullable BuildoutModuleExtension buildoutFacet, VirtualFile baseDir) { + File rootPath = null; + if (buildoutFacet != null) { + File configIOFile = buildoutFacet.getConfigFile(); + if (configIOFile != null) { + rootPath = configIOFile.getParentFile(); + } + } + if (rootPath == null || !rootPath.exists()) { + if (baseDir != null) { + rootPath = new File(baseDir.getPath()); + } } - }); - if (scripts != null) { - return Arrays.asList(scripts); - } + if (rootPath != null) { + File[] scripts = new File(rootPath, "bin").listFiles((dir, name) -> { + if (Platform.current().os().isWindows()) { + return name.endsWith("-script.py"); + } + String ext = FileUtil.getExtension(name); + return ext.length() == 0 || FileUtil.namesEqual(ext, "py"); + }); + if (scripts != null) { + return Arrays.asList(scripts); + } + } + return Collections.emptyList(); } - return Collections.emptyList(); - } - @Nullable - public static List extractBuildoutPaths(@Nonnull VirtualFile script) { - try { - List paths = extractFromScript(script); - if (paths == null) { - VirtualFile root = script.getParent().getParent(); - String partName = FileUtil.getNameWithoutExtension(script.getName()); - if (SystemInfo.isWindows && partName.endsWith(SCRIPT_SUFFIX)) { - partName = partName.substring(0, partName.length() - SCRIPT_SUFFIX.length()); + @Nullable + public static List extractBuildoutPaths(@Nonnull VirtualFile script) { + try { + List paths = extractFromScript(script); + if (paths == null) { + VirtualFile root = script.getParent().getParent(); + String partName = FileUtil.getNameWithoutExtension(script.getName()); + if (Platform.current().os().isWindows() && partName.endsWith(SCRIPT_SUFFIX)) { + partName = partName.substring(0, partName.length() - SCRIPT_SUFFIX.length()); + } + VirtualFile sitePy = root.findFileByRelativePath("parts/" + partName + "/site.py"); + if (sitePy != null) { + paths = extractFromSitePy(sitePy); + } + } + return paths; } - VirtualFile sitePy = root.findFileByRelativePath("parts/" + partName + "/site.py"); - if (sitePy != null) { - paths = extractFromSitePy(sitePy); + catch (IOException e) { + LOGGER.info(e); + return null; } - } - return paths; } - catch (IOException e) { - LOGGER.info(e); - return null; - } - } - /** - * Extracts paths from given script, assuming sys.path[0:0] assignment. - * - * @param script - * @return extracted paths, or null if extraction fails. - */ - @Nullable - public static List extractFromScript(@Nonnull VirtualFile script) throws IOException { - String text = VfsUtil.loadText(script); - Pattern pat = Pattern.compile("(?:^\\s*(['\"])(.*)(\\1),\\s*$)|(\\])", Pattern.MULTILINE); - String bait_string = "sys.path[0:0]"; - int pos = text.indexOf(bait_string); - List ret = null; - if (pos >= 0) { - pos += bait_string.length(); - Matcher scanner = pat.matcher(text); - while (scanner.find(pos)) { - String value = scanner.group(2); - if (value != null) { - if (ret == null) { - ret = new ArrayList(); - } - ret.add(value); - pos = scanner.end(); + /** + * Extracts paths from given script, assuming sys.path[0:0] assignment. + * + * @param script + * @return extracted paths, or null if extraction fails. + */ + @Nullable + public static List extractFromScript(@Nonnull VirtualFile script) throws IOException { + String text = VfsUtil.loadText(script); + Pattern pat = Pattern.compile("(?:^\\s*(['\"])(.*)(\\1),\\s*$)|(\\])", Pattern.MULTILINE); + String bait_string = "sys.path[0:0]"; + int pos = text.indexOf(bait_string); + List ret = null; + if (pos >= 0) { + pos += bait_string.length(); + Matcher scanner = pat.matcher(text); + while (scanner.find(pos)) { + String value = scanner.group(2); + if (value != null) { + if (ret == null) { + ret = new ArrayList<>(); + } + ret.add(value); + pos = scanner.end(); + } + else { + break; + } // we've matched the ']', it's group(4) + } } - else { - break; - } // we've matched the ']', it's group(4) - } + return ret; } - return ret; - } - /** - * Extracts paths from site.py generated by buildout 1.5+ - * - * @param vFile path to site.py - * @return extracted paths - */ - public static List extractFromSitePy(VirtualFile vFile) throws IOException { - List result = new ArrayList(); - String text = VfsUtil.loadText(vFile); - String[] lines = LineTokenizer.tokenize(text, false); - int index = 0; - while (index < lines.length && !lines[index].startsWith("def addsitepackages(")) { - index++; + /** + * Extracts paths from site.py generated by buildout 1.5+ + * + * @param vFile path to site.py + * @return extracted paths + */ + public static List extractFromSitePy(VirtualFile vFile) throws IOException { + List result = new ArrayList<>(); + String text = VfsUtil.loadText(vFile); + String[] lines = LineTokenizer.tokenize(text, false); + int index = 0; + while (index < lines.length && !lines[index].startsWith("def addsitepackages(")) { + index++; + } + while (index < lines.length && !lines[index].trim().startsWith("buildout_paths = [")) { + index++; + } + index++; + while (index < lines.length && !lines[index].trim().equals("]")) { + String line = lines[index].trim(); + if (line.endsWith(",")) { + line = line.substring(0, line.length() - 1); + } + if (StringEscapeUtil.isQuoted(line, '\'')) { + result.add(StringEscapeUtil.unescape(line, 1, line.length() - 1)); + } + index++; + } + return result; } - while (index < lines.length && !lines[index].trim().startsWith("buildout_paths = [")) { - index++; + + public static void attachLibrary(Module module) { + BuildoutModuleExtension facet = ModuleUtilCore.getExtension(module, BuildoutModuleExtension.class); + if (facet == null) { + return; + } + List paths = facet.getPaths(); + FacetLibraryConfigurator.attachLibrary(module, null, BUILDOUT_LIB_NAME, paths); } - index++; - while (index < lines.length && !lines[index].trim().equals("]")) { - String line = lines[index].trim(); - if (line.endsWith(",")) { - line = line.substring(0, line.length() - 1); - } - if (line.startsWith("'") && line.endsWith("'")) { - result.add(StringUtil.unescapeStringCharacters(line.substring(1, line.length() - 1))); - } - index++; + + public static void detachLibrary(Module module) { + FacetLibraryConfigurator.detachLibrary(module, BUILDOUT_LIB_NAME); } - return result; - } - public static void attachLibrary(Module module) { - BuildoutModuleExtension facet = ModuleUtilCore.getExtension(module, BuildoutModuleExtension.class); - if (facet == null) { - return; + @Nullable + public File getConfigFile() { + String scriptName = getScriptName(); + if (!StringUtil.isEmpty(scriptName)) { + return new File(new File(scriptName).getParentFile().getParentFile(), BUILDOUT_CFG); + } + return null; } - List paths = facet.getPaths(); - FacetLibraryConfigurator.attachLibrary(module, null, BUILDOUT_LIB_NAME, paths); - } - public static void detachLibrary(Module module) { - FacetLibraryConfigurator.detachLibrary(module, BUILDOUT_LIB_NAME); - } + public void patchCommandLineForBuildout(GeneralCommandLine commandLine) { + Map env = commandLine.getEnvironment(); + ParametersList params = commandLine.getParametersList(); + // alter execution script + ParamsGroup script_params = params.getParamsGroup(PythonCommandLineState.GROUP_SCRIPT); + assert script_params != null; + if (script_params.getParameters().size() > 0) { + String normal_script = script_params.getParameters().get(0); // expect DjangoUtil.MANAGE_FILE + String engulfer_path = PythonHelpersLocator.getHelperPath("pycharm/buildout_engulfer.py"); + env.put("PYCHARM_ENGULF_SCRIPT", getScriptName()); + script_params.getParametersList().replaceOrPrepend(normal_script, engulfer_path); + } + // add pycharm helpers to pythonpath so that fixGetpass is importable - @Nullable - public File getConfigFile() { - String scriptName = getScriptName(); - if (!StringUtil.isEmpty(scriptName)) { - return new File(new File(scriptName).getParentFile().getParentFile(), BUILDOUT_CFG); + PythonEnvUtil.addToPythonPath(env, PythonHelpersLocator.getHelpersRoot().getAbsolutePath()); + /* + // set prependable paths + List paths = facet.getAdditionalPythonPath(); + if (paths != null) { + path_value = PyUtil.joinWith(File.pathSeparator, paths); + env.put("PYCHARM_PREPEND_SYSPATH", path_value); + } + */ } - return null; - } - public void patchCommandLineForBuildout(GeneralCommandLine commandLine) { - Map env = commandLine.getEnvironment(); - ParametersList params = commandLine.getParametersList(); - // alter execution script - ParamsGroup script_params = params.getParamsGroup(PythonCommandLineState.GROUP_SCRIPT); - assert script_params != null; - if (script_params.getParameters().size() > 0) { - String normal_script = script_params.getParameters().get(0); // expect DjangoUtil.MANAGE_FILE - String engulfer_path = PythonHelpersLocator.getHelperPath("pycharm/buildout_engulfer.py"); - env.put("PYCHARM_ENGULF_SCRIPT", getScriptName()); - script_params.getParametersList().replaceOrPrepend(normal_script, engulfer_path); + public String getScriptName() { + return myScriptName; } - // add pycharm helpers to pythonpath so that fixGetpass is importable - PythonEnvUtil.addToPythonPath(env, PythonHelpersLocator.getHelpersRoot().getAbsolutePath()); - /* - // set prependable paths - List paths = facet.getAdditionalPythonPath(); - if (paths != null) { - path_value = PyUtil.joinWith(File.pathSeparator, paths); - env.put("PYCHARM_PREPEND_SYSPATH", path_value); + /** + * Generates a sys.path[0:0] = [...] with paths that buildout script wants. + * + * @param additionalPythonPath + * @return the statement, or null if there's no buildout facet. + */ + @Nullable + public String getPathPrependStatement(List additionalPythonPath) { + StringBuilder sb = new StringBuilder("sys.path[0:0]=["); + for (String s : additionalPythonPath) { + sb.append("'").append(s).append("',"); + // NOTE: we assume that quotes and spaces are escaped in paths back in the buildout script we extracted them from. + } + sb.append("]"); + return sb.toString(); } - */ - } - - public String getScriptName() { - return myScriptName; - } - /** - * Generates a sys.path[0:0] = [...] with paths that buildout script wants. - * - * @param additionalPythonPath - * @return the statement, or null if there's no buildout facet. - */ - @Nullable - public String getPathPrependStatement(List additionalPythonPath) { - StringBuilder sb = new StringBuilder("sys.path[0:0]=["); - for (String s : additionalPythonPath) { - sb.append("'").append(s).append("',"); - // NOTE: we assume that quotes and spaces are escaped in paths back in the buildout script we extracted them from. + public List getPaths() { + return myPaths; } - sb.append("]"); - return sb.toString(); - } - - public List getPaths() { - return myPaths; - } - /** - * Sets the paths to be prepended to pythonpath, taken from a buildout script. - * - * @param paths what to store; the list will be copied. - */ - void setPaths(@Nullable List paths) { - if (paths != null) { - myPaths = new ArrayList(paths.size()); - for (String s : paths) { - myPaths.add(s); - } - } - else { - myPaths = null; + /** + * Sets the paths to be prepended to pythonpath, taken from a buildout script. + * + * @param paths what to store; the list will be copied. + */ + void setPaths(@Nullable List paths) { + if (paths != null) { + myPaths = new ArrayList<>(paths.size()); + for (String s : paths) { + myPaths.add(s); + } + } + else { + myPaths = null; + } } - } - @Override - public List getAdditionalPythonPath() { - return myPaths; - } + @Override + public List getAdditionalPythonPath() { + return myPaths; + } - @Override - public boolean acceptRootAsTopLevelPackage() { - return false; - } + @Override + public boolean acceptRootAsTopLevelPackage() { + return false; + } - @Override - public void updateLibrary() { - updatePaths(); - attachLibrary(getModule()); - } + @Override + public void updateLibrary() { + updatePaths(); + attachLibrary(getModule()); + } - @Override - public void removeLibrary() { - detachLibrary(getModule()); - } + @Override + public void removeLibrary() { + detachLibrary(getModule()); + } - @Override - public void commit(@Nonnull BuildoutModuleExtension mutableModuleExtension) { - super.commit(mutableModuleExtension); - myScriptName = mutableModuleExtension.getScriptName(); - } + @Override + @RequiredReadAction + public void commit(@Nonnull BuildoutModuleExtension mutableModuleExtension) { + super.commit(mutableModuleExtension); + myScriptName = mutableModuleExtension.getScriptName(); + } - @Nullable - public VirtualFile getScript() { - return LocalFileSystem.getInstance().findFileByPath(getScriptName()); - } + @Nullable + public VirtualFile getScript() { + return LocalFileSystem.getInstance().findFileByPath(getScriptName()); + } - public void updatePaths() { - VirtualFile script = getScript(); - if (script != null) { - setPaths(extractBuildoutPaths(script)); + public void updatePaths() { + VirtualFile script = getScript(); + if (script != null) { + setPaths(extractBuildoutPaths(script)); + } } - } }