use of com.jetbrains.python.psi.PyStringLiteralExpression in project intellij-community by JetBrains.
the class PyStringLiteralTest method testEscaperOffsetInLongUnicodeEscape.
public void testEscaperOffsetInLongUnicodeEscape() {
final PyStringLiteralExpression expr = createLiteralFromText("u'XXX a\\U0001F600\\U0001F600b YYY'");
final LiteralTextEscaper<? extends PsiLanguageInjectionHost> escaper = expr.createLiteralTextEscaper();
final TextRange range = TextRange.create(6, 28);
assertEquals(6, escaper.getOffsetInHost(0, range));
assertEquals(7, escaper.getOffsetInHost(1, range));
// Each \\U0001F600 is represented as a surrogate pair, hence 2 characters-wide step in decoded text
assertEquals(17, escaper.getOffsetInHost(3, range));
assertEquals(27, escaper.getOffsetInHost(5, range));
assertEquals(28, escaper.getOffsetInHost(6, range));
assertEquals(-1, escaper.getOffsetInHost(7, range));
}
use of com.jetbrains.python.psi.PyStringLiteralExpression in project intellij-community by JetBrains.
the class PyCompletionConfidence method shouldSkipAutopopup.
@NotNull
@Override
public ThreeState shouldSkipAutopopup(@NotNull PsiElement contextElement, @NotNull PsiFile psiFile, int offset) {
ASTNode node = contextElement.getNode();
if (node != null) {
if (node.getElementType() == PyTokenTypes.FLOAT_LITERAL) {
return ThreeState.YES;
}
if (PyTokenTypes.STRING_NODES.contains(node.getElementType())) {
final PsiElement parent = contextElement.getParent();
if (parent instanceof PyStringLiteralExpression) {
final List<TextRange> ranges = ((PyStringLiteralExpression) parent).getStringValueTextRanges();
int relativeOffset = offset - parent.getTextRange().getStartOffset();
if (ranges.size() > 0 && relativeOffset < ranges.get(0).getStartOffset()) {
return ThreeState.YES;
}
}
}
}
return super.shouldSkipAutopopup(contextElement, psiFile, offset);
}
use of com.jetbrains.python.psi.PyStringLiteralExpression in project intellij-community by JetBrains.
the class PyMagicLiteralRenameHandler method getStringLiteral.
/**
* @return string literal under data context or null if not a literal.
* This method is fast, so it can safely be used at {@link #isAvailableOnDataContext(DataContext)}
*/
@Nullable
private static PyStringLiteralExpression getStringLiteral(@NotNull final DataContext dataContext) {
final Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
if (editor == null) {
return null;
}
final PsiFile file = CommonDataKeys.PSI_FILE.getData(dataContext);
if (file == null) {
return null;
}
final PsiElement element = getElement(file, editor);
if (element instanceof PyStringLiteralExpression) {
return (PyStringLiteralExpression) element;
}
return null;
}
use of com.jetbrains.python.psi.PyStringLiteralExpression in project intellij-community by JetBrains.
the class PyFStringsInjector method getLanguagesToInject.
@Override
public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull PsiElement context) {
final PyStringLiteralExpression pyString = as(context, PyStringLiteralExpression.class);
if (pyString == null) {
return;
}
for (ASTNode node : pyString.getStringNodes()) {
final int relNodeOffset = node.getTextRange().getStartOffset() - pyString.getTextRange().getStartOffset();
for (Fragment offsets : getInjectionRanges(node)) {
if (offsets.containsNamedUnicodeEscape())
continue;
registrar.startInjecting(PyDocstringLanguageDialect.getInstance());
registrar.addPlace(null, null, pyString, offsets.getContentRange().shiftRight(relNodeOffset));
registrar.doneInjecting();
}
}
}
use of com.jetbrains.python.psi.PyStringLiteralExpression in project intellij-community by JetBrains.
the class PyQuotedStringIntention method isAvailable.
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!(file instanceof PyFile)) {
return false;
}
PyStringLiteralExpression string = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyStringLiteralExpression.class);
if (string != null) {
final PyDocStringOwner docStringOwner = PsiTreeUtil.getParentOfType(string, PyDocStringOwner.class);
if (docStringOwner != null) {
if (docStringOwner.getDocStringExpression() == string)
return false;
}
String stringText = string.getText();
int prefixLength = PyStringLiteralExpressionImpl.getPrefixLength(stringText);
stringText = stringText.substring(prefixLength);
if (stringText.length() >= 6) {
if (stringText.startsWith("'''") && stringText.endsWith("'''") || stringText.startsWith("\"\"\"") && stringText.endsWith("\"\"\""))
return false;
}
if (stringText.length() > 2) {
if (stringText.startsWith("'") && stringText.endsWith("'")) {
setText(PyBundle.message("INTN.quoted.string.single.to.double"));
return true;
}
if (stringText.startsWith("\"") && stringText.endsWith("\"")) {
setText(PyBundle.message("INTN.quoted.string.double.to.single"));
return true;
}
}
}
return false;
}
Aggregations