use of com.jetbrains.python.psi.PyStringLiteralExpression in project intellij-community by JetBrains.
the class PyDunderAllReference method resolve.
@Override
public PsiElement resolve() {
final PyStringLiteralExpression element = getElement();
final String name = element.getStringValue();
PyFile containingFile = (PyFile) element.getContainingFile();
return containingFile.getElementNamed(name);
}
use of com.jetbrains.python.psi.PyStringLiteralExpression in project intellij-community by JetBrains.
the class PyKeywordTypedHandler method getOverTypeResult.
@Nullable
public Result getOverTypeResult(Editor editor, PsiFile file) {
final Document document = editor.getDocument();
final int offset = editor.getCaretModel().getOffset();
PsiElement token = file.findElementAt(offset - 1);
// sanity check: beyond EOL
if (token == null || offset >= document.getTextLength())
return Result.CONTINUE;
PsiElement here_elt = file.findElementAt(offset);
if (here_elt == null)
return Result.CONTINUE;
if (here_elt instanceof PyStringLiteralExpression || here_elt.getParent() instanceof PyStringLiteralExpression)
return Result.CONTINUE;
// double colons aren't found in Python's syntax, so we can safely overtype a colon everywhere but strings.
String here_text = here_elt.getText();
if (":".equals(here_text)) {
// overtype, that is, jump over
editor.getCaretModel().moveToOffset(offset + 1);
return Result.STOP;
}
return null;
}
use of com.jetbrains.python.psi.PyStringLiteralExpression in project intellij-community by JetBrains.
the class PyMagicLiteralRenameProcessor method renameElement.
@Override
public void renameElement(final PsiElement element, final String newName, final UsageInfo[] usages, @Nullable final RefactoringElementListener listener) {
Preconditions.checkArgument(canProcessElement(element), "Element can't be renamed, call #canProcessElement first " + element);
element.replace(PyElementGenerator.getInstance(element.getProject()).createStringLiteral((PyStringLiteralExpression) element, newName));
for (final UsageInfo usage : usages) {
final PsiReference reference = usage.getReference();
if (reference != null) {
reference.handleElementRename(newName);
}
}
}
use of com.jetbrains.python.psi.PyStringLiteralExpression in project intellij-community by JetBrains.
the class RemovePrefixQuickFix method applyFix.
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PyStringLiteralExpression pyString = as(descriptor.getPsiElement(), PyStringLiteralExpression.class);
if (pyString != null) {
final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
for (ASTNode node : pyString.getStringNodes()) {
final String nodeText = node.getText();
final int prefixLength = PyStringLiteralExpressionImpl.getPrefixLength(nodeText);
if (nodeText.substring(0, prefixLength).equalsIgnoreCase(myPrefix)) {
final PyStringLiteralExpression replacement = elementGenerator.createStringLiteralAlreadyEscaped(nodeText.substring(prefixLength));
node.getPsi().replace(replacement.getFirstChild());
}
}
}
}
use of com.jetbrains.python.psi.PyStringLiteralExpression in project intellij-community by JetBrains.
the class PyQuotedStringIntention method doInvoke.
public void doInvoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
PyStringLiteralExpression string = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyStringLiteralExpression.class);
PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
if (string != null) {
final String stringText = string.getText();
int prefixLength = PyStringLiteralExpressionImpl.getPrefixLength(stringText);
final String text = stringText.substring(prefixLength);
if (text.startsWith("'") && text.endsWith("'")) {
String result = convertSingleToDoubleQuoted(stringText);
PyStringLiteralExpression st = elementGenerator.createStringLiteralAlreadyEscaped(result);
string.replace(st);
}
if (text.startsWith("\"") && text.endsWith("\"")) {
String result = convertDoubleToSingleQuoted(stringText);
PyStringLiteralExpression st = elementGenerator.createStringLiteralAlreadyEscaped(result);
string.replace(st);
}
}
}
Aggregations