From af5df4e858d867ccbc921125334e6b7f25b4a67e Mon Sep 17 00:00:00 2001 From: Andrey Vlasovskikh Date: Tue, 15 Oct 2019 19:28:09 +0300 Subject: [PATCH] PY-33254 Allow both / and \ as file path separators in string literals in Python console GitOrigin-RevId: 289dbb39f0313c089c2cb95a3bf8c3b25b89be96 --- .../codeInsight/PyConsoleFileReferenceContributor.kt | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/python/src/com/jetbrains/python/codeInsight/PyConsoleFileReferenceContributor.kt b/python/src/com/jetbrains/python/codeInsight/PyConsoleFileReferenceContributor.kt index 3ca39c022cc5..690f104ee5f7 100644 --- a/python/src/com/jetbrains/python/codeInsight/PyConsoleFileReferenceContributor.kt +++ b/python/src/com/jetbrains/python/codeInsight/PyConsoleFileReferenceContributor.kt @@ -2,6 +2,7 @@ package com.jetbrains.python.codeInsight import com.intellij.openapi.module.ModuleUtilCore +import com.intellij.openapi.util.SystemInfo import com.intellij.openapi.vfs.StandardFileSystems import com.intellij.patterns.PatternCondition import com.intellij.patterns.PlatformPatterns.psiElement @@ -11,7 +12,6 @@ import com.jetbrains.python.PythonRuntimeService import com.jetbrains.python.console.PyConsoleOptions import com.jetbrains.python.console.PydevConsoleRunnerFactory import com.jetbrains.python.psi.PyStringLiteralExpression -import java.io.File /** * Contributes file path references for Python string literals in the Python console. @@ -30,10 +30,13 @@ class PyConsoleFileReferenceContributor : PsiReferenceContributor() { * Matches string literals in a Python console that have at least one path separator in them. */ object StringWithPathSeparatorInConsole : PatternCondition("stringWithSeparatorInConsolePattern") { + val separators = listOfNotNull("/", if (SystemInfo.isWindows) "\\" else null) + override fun accepts(expr: PyStringLiteralExpression, context: ProcessingContext?): Boolean { val containingFile = expr.containingFile ?: return false if (!PythonRuntimeService.getInstance().isInPydevConsole(containingFile)) return false - return File.separator in expr.stringValue + val stringValue = expr.stringValue + return separators.any { it in stringValue } } }