use of com.jetbrains.python.psi.PyStringLiteralExpression in project intellij-community by JetBrains.
the class PyStringLiteralTest method createLiteralFromText.
private PyStringLiteralExpression createLiteralFromText(final String text) {
final PsiFile file = PsiFileFactory.getInstance(myFixture.getProject()).createFileFromText("test.py", "a = " + text);
final PyStringLiteralExpression expr = PsiTreeUtil.getParentOfType(file.findElementAt(5), PyStringLiteralExpression.class);
assert expr != null;
return expr;
}
use of com.jetbrains.python.psi.PyStringLiteralExpression in project intellij-community by JetBrains.
the class DocStringReferenceProvider method getReferencesByElement.
@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull final PsiElement element, @NotNull ProcessingContext context) {
if (element == DocStringUtil.getParentDefinitionDocString(element)) {
final PyStringLiteralExpression expr = (PyStringLiteralExpression) element;
final List<TextRange> ranges = expr.getStringValueTextRanges();
final String exprText = expr.getText();
final TextRange textRange = PyStringLiteralExpressionImpl.getNodeTextRange(exprText);
final String text = textRange.substring(exprText);
if (!ranges.isEmpty()) {
final List<PsiReference> result = new ArrayList<>();
final int offset = ranges.get(0).getStartOffset();
// XXX: It does not work with multielement docstrings
StructuredDocString docString = DocStringUtil.parse(text, element);
if (docString instanceof TagBasedDocString) {
final TagBasedDocString taggedDocString = (TagBasedDocString) docString;
result.addAll(referencesFromNames(expr, offset, docString, taggedDocString.getTagArguments(TagBasedDocString.PARAM_TAGS), ReferenceType.PARAMETER));
result.addAll(referencesFromNames(expr, offset, docString, taggedDocString.getTagArguments(TagBasedDocString.PARAM_TYPE_TAGS), ReferenceType.PARAMETER_TYPE));
result.addAll(referencesFromNames(expr, offset, docString, docString.getKeywordArgumentSubstrings(), ReferenceType.KEYWORD));
result.addAll(referencesFromNames(expr, offset, docString, taggedDocString.getTagArguments("var"), ReferenceType.VARIABLE));
result.addAll(referencesFromNames(expr, offset, docString, taggedDocString.getTagArguments("cvar"), ReferenceType.CLASS_VARIABLE));
result.addAll(referencesFromNames(expr, offset, docString, taggedDocString.getTagArguments("ivar"), ReferenceType.INSTANCE_VARIABLE));
result.addAll(returnTypes(element, docString, offset));
} else if (docString instanceof SectionBasedDocString) {
final SectionBasedDocString sectioned = (SectionBasedDocString) docString;
result.addAll(referencesFromFields(expr, offset, sectioned.getParameterFields(), ReferenceType.PARAMETER));
result.addAll(referencesFromFields(expr, offset, sectioned.getKeywordArgumentFields(), ReferenceType.KEYWORD));
result.addAll(referencesFromFields(expr, offset, sectioned.getAttributeFields(), PyUtil.isTopLevel(element) ? ReferenceType.GLOBAL_VARIABLE : ReferenceType.INSTANCE_VARIABLE));
result.addAll(referencesFromFields(expr, offset, sectioned.getReturnFields(), null));
}
return result.toArray(new PsiReference[result.size()]);
}
}
return PsiReference.EMPTY_ARRAY;
}
use of com.jetbrains.python.psi.PyStringLiteralExpression in project intellij-community by JetBrains.
the class PyLiteralSelectionHandler method select.
@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
final PyStringLiteralExpression literal = PsiTreeUtil.getParentOfType(e, PyStringLiteralExpression.class);
if (literal != null) {
List<TextRange> ranges = literal.getStringValueTextRanges();
List<ASTNode> nodes = literal.getStringNodes();
for (int i = 0; i < ranges.size(); i++) {
TextRange stringRange = ranges.get(i);
TextRange offsetRange = stringRange.shiftRight(literal.getTextRange().getStartOffset());
if (offsetRange.contains(cursorOffset) && offsetRange.getLength() > 1) {
List<TextRange> result = new ArrayList<>();
SelectWordUtil.addWordHonoringEscapeSequences(editorText, nodes.get(i).getTextRange(), cursorOffset, new PyStringLiteralLexer(nodes.get(i).getElementType()), result);
result.add(offsetRange);
return result;
}
}
}
return Collections.emptyList();
}
Aggregations