下面列出了com.intellij.psi.PsiElement#getText ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@NotNull
public static String getTextUntilClass(@NotNull PsiElement root, @Nullable Class clazz) {
String text = root.getText();
PsiElement sibling = root.getNextSibling();
while (sibling != null) {
if (sibling.getClass().isAssignableFrom(clazz)) {
sibling = null;
} else {
text += sibling.getText();
sibling = sibling.getNextSibling();
}
}
return text.trim();
}
private int getArgumentIndexUnderCaret(@NotNull PsiElement place, List<HaxeExpression> expressionList) {
HaxeExpression expression = getExpressionAtPlace(place, expressionList);
if (expression != null) {
int expressionIndex = expressionList.indexOf(expression);
if (expressionIndex >= 0) {
return expressionIndex;
}
}
else {
final String tokenText = place.getText();
if (tokenText.equals(HaxeTokenTypes.PRPAREN.toString())) {
return getExpressionIndexBeforeRightParen(expressionList);
}
else {
return getExpressionIndexAtPlace(place, expressionList);
}
}
return -1;
}
public static String extractHighlightedText(@Nonnull CommonProblemDescriptor descriptor, PsiElement psiElement) {
if (psiElement == null || !psiElement.isValid()) return "";
String ref = psiElement.getText();
if (descriptor instanceof ProblemDescriptorBase) {
TextRange textRange = ((ProblemDescriptorBase)descriptor).getTextRange();
final TextRange elementRange = psiElement.getTextRange();
if (textRange != null && elementRange != null) {
textRange = textRange.shiftRight(-elementRange.getStartOffset());
if (textRange.getStartOffset() >= 0 && textRange.getEndOffset() <= elementRange.getLength()) {
ref = textRange.substring(ref);
}
}
}
ref = StringUtil.replaceChar(ref, '\n', ' ').trim();
ref = StringUtil.first(ref, 100, true);
return ref;
}
private void attachConstantFoldingDescriptors(PsiElement psiElement, List<FoldingDescriptor> descriptors) {
// find path calls in file
PsiElement[] constantReferences = PsiTreeUtil.collectElements(psiElement, psiElement1 ->
TwigPattern.getPrintBlockOrTagFunctionPattern("constant").accepts(psiElement1)
);
if(constantReferences.length == 0) {
return;
}
for(PsiElement fileReference: constantReferences) {
String contents = fileReference.getText();
if(StringUtils.isNotBlank(contents) && contents.contains(":")) {
final String[] parts = contents.split("::");
if(parts.length == 2) {
descriptors.add(new FoldingDescriptor(fileReference.getNode(),
new TextRange(fileReference.getTextRange().getStartOffset(), fileReference.getTextRange().getEndOffset())) {
@Nullable
@Override
public String getPlaceholderText() {
return parts[1];
}
});
}
}
}
}
@RequiredReadAction
@Nullable
@Override
public String getVarName()
{
PsiElement nameIdentifier = getVarElement();
return nameIdentifier != null ? nameIdentifier.getText() : null;
}
@Override
public String getElementText(PsiElement element) {
if (element instanceof PsiLiteralExpression) {
return ((PsiLiteralExpression) element).getValue().toString();
}
return element.getText();
}
@Override
public TextAttributesKey getKeywordKind(@NotNull final PsiElement element) {
TextAttributesKey kind = null;
switch (element.getText()) {
case "public":
case "protected":
case "private":
kind = PRIVATE;
break;
}
return kind;
}
public static String printTree(MockJSElementInterface element)
{
PsiElement nextSibling = element.getNextSibling();
String result = element.getText();
while(nextSibling != null)
{
result += nextSibling.getText();
nextSibling = nextSibling.getNextSibling();
}
return result;
}
@Override
public void visitElement(PsiElement element) {
if(!(element instanceof PhpDocTag)) {
super.visitElement(element);
return;
}
String annotationName = StringUtils.stripStart(((PhpDocTag) element).getName(), "@");
if(!"ConfigEntityType".equals(annotationName)) {
super.visitElement(element);
return;
}
PsiElement phpDocComment = element.getParent();
if(!(phpDocComment instanceof PhpDocComment)) {
super.visitElement(element);
return;
}
PhpPsiElement phpClass = ((PhpDocComment) phpDocComment).getNextPsiSibling();
if(!(phpClass instanceof PhpClass)) {
super.visitElement(element);
return;
}
String tagValue = element.getText();
Matcher matcher = Pattern.compile("id\\s*=\\s*\"([\\w\\.-]+)\"").matcher(tagValue);
if (matcher.find()) {
map.put(matcher.group(1), StringUtils.stripStart(((PhpClass) phpClass).getFQN(), "\\"));
}
super.visitElement(element);
}
@Override
public String getName() {
PsiElement nameIdentifier = getNameIdentifier();
if (nameIdentifier == null) {
return "unknown";
}
return nameIdentifier.getText();
}
@Override
public PsiElement getSignalHandlerElement() {
//the first element after the "trap" command element
PsiElement firstParam = BashPsiUtils.findNextSibling(getFirstChild(), BashTokenTypes.WHITESPACE);
if (firstParam == null) {
return null;
}
String text = firstParam.getText();
if (text.startsWith("-p") || text.startsWith("-l")) {
return null;
}
//the string/word container is embedded in a structure of "simple command" -> "generic command element"
//extract it without relying too much on the defined structure
PsiElement child = firstParam;
while (child.getTextRange().equals(firstParam.getTextRange())) {
PsiElement firstChild = child.getFirstChild();
if (firstChild == null || firstChild instanceof LeafPsiElement) {
break;
}
child = child.getFirstChild();
}
return child;
}
@Override
public String getName()
{
PsiElement nameIdentifier = getNameIdentifier();
if(nameIdentifier != null)
{
return nameIdentifier.getText();
}
return super.getName();
}
public static String getSuperClassName(PsiElement child) {
PsiElement superclass = PsiPhpHelper.getSuperClassElement(child);
return superclass.getText();
}
public TemplateDefinitionReference(PsiElement element, TextRange textRange) {
super(element, textRange);
this.templateName = element.getText();
}
@Override
public boolean matches(@NotNull LanguageMatcherParameter parameter) {
List<JsonSignature> filter = ContainerUtil.filter(parameter.getSignatures(), jsonSignature -> "annotation".equals(jsonSignature.getType()) &&
StringUtils.isNotBlank(jsonSignature.getField()) &&
StringUtils.isNotBlank(jsonSignature.getClassName()));
if(filter.size() == 0) {
return false;
}
PsiElement phpDocString = parameter.getElement().getParent();
if(!(phpDocString instanceof StringLiteralExpression)) {
return false;
}
boolean accepts = AnnotationPattern.getTextIdentifier().accepts(parameter.getElement());
if(!accepts) {
return false;
}
PsiElement propertyName = PhpElementsUtil.getPrevSiblingOfPatternMatch(
phpDocString,
PlatformPatterns.psiElement(PhpDocTokenTypes.DOC_IDENTIFIER)
);
if(propertyName == null) {
return false;
}
String fieldName = propertyName.getText();
if(StringUtils.isBlank(fieldName)) {
return false;
}
for (JsonSignature signature : filter) {
if(!fieldName.equals(signature.getField())) {
continue;
}
PhpClass phpClass = AnnotationUtil.getAnnotationReference(PsiTreeUtil.getParentOfType(phpDocString, PhpDocTag.class));
if(phpClass == null) {
continue;
}
if(StringUtils.stripStart(phpClass.getFQN(), "\\").equals(StringUtils.stripStart(signature.getClassName(), "\\"))) {
return true;
}
}
return false;
}
@NotNull
@Override
public String getNodeText(@NotNull PsiElement element, boolean useFullName) {
String result = element instanceof ThriftDefinitionName ? ((ThriftDefinitionName)element).getName() : element.getText();
return StringUtil.notNullize(result);
}
@Override
public void visitElement(PsiElement element) {
if (!(element instanceof StringLiteralExpression) || element.getNode().getElementType() != PhpDocElementTypes.phpDocString) {
super.visitElement(element);
return;
}
String contents = ((StringLiteralExpression) element).getContents();
if (StringUtils.isBlank(contents)) {
super.visitElement(element);
return;
}
PhpDocTag phpDocTag = PsiTreeUtil.getParentOfType(element, PhpDocTag.class);
if (phpDocTag == null) {
super.visitElement(element);
return;
}
PsiElement propertyName = PhpElementsUtil.getPrevSiblingOfPatternMatch(element, PlatformPatterns.psiElement(PhpDocTokenTypes.DOC_IDENTIFIER));
if(propertyName == null) {
super.visitElement(element);
return;
}
String text = propertyName.getText();
if ("type".equalsIgnoreCase(text)) {
PhpDocTagAnnotation phpDocAnnotationContainer = AnnotationUtil.getPhpDocAnnotationContainer(phpDocTag);
if (phpDocAnnotationContainer != null) {
PhpClass tagPhpClass = phpDocAnnotationContainer.getPhpClass();
if (PhpLangUtil.equalsClassNames(tagPhpClass.getPresentableFQN(), "Doctrine\\ORM\\Mapping\\Column")) {
for (PhpClass columnPhpClass : DoctrineUtil.getColumnTypesTargets(holder.getProject(), contents)) {
if (!columnPhpClass.isDeprecated()) {
continue;
}
String deprecationMessage = PhpElementsUtil.getClassDeprecatedMessage(columnPhpClass);
holder.registerProblem(
element,
"[Annotations] " + (deprecationMessage != null ? deprecationMessage : String.format("Field '%s' is deprecated", text)),
ProblemHighlightType.LIKE_DEPRECATED
);
break;
}
}
}
}
super.visitElement(element);
}
@Override
public void actionPerformed(AnActionEvent e) {
Analytics.event("query", getQueryExecutionAction(e));
Project project = getEventProject(e);
Editor editor = e.getData(CommonDataKeys.EDITOR_EVEN_IF_INACTIVE);
PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE);
if (isNull(project)) {
Notifier.error(QUERY_EXECUTION_ERROR_TITLE, NO_PROJECT_PRESENT_MESSAGE);
return;
}
if (isNull(editor)) {
Notifier.error(QUERY_EXECUTION_ERROR_TITLE, NO_EDITOR_PRESENT_MESSAGE);
return;
}
String query = null;
String analyticsEvent;
Map<String, Object> parameters = Collections.emptyMap();
if (preSetQuery == null) {
Caret caret = editor.getCaretModel().getPrimaryCaret();
analyticsEvent = caret.hasSelection() ? CONTENT_FROM_SELECT_ACTION : CONTENT_FROM_CARET_ACTION;
if (caret.hasSelection()) {
query = caret.getSelectedText();
} else if (nonNull(psiFile)) {
String languageId = psiFile.getLanguage().getID();
if (isSupported(languageId)) {
PsiElement cypherStatement = getCypherStatementAtOffset(psiFile, caret.getOffset());
if (nonNull(cypherStatement)) {
query = cypherStatement.getText();
parameters = getParametersFromQuery(cypherStatement, project, editor);
}
}
}
} else {
analyticsEvent = CONTENT_FROM_LINE_MARKER_ACTION;
query = preSetQuery.getText();
parameters = getParametersFromQuery(preSetQuery, project, editor);
}
Analytics.event("query-content", analyticsEvent);
if (isNull(query)) {
Notifier.error(QUERY_EXECUTION_ERROR_TITLE, NO_QUERY_SELECTED_MESSAGE);
return;
}
actionPerformed(e, project, editor, query, parameters);
}
@Override
public String getName() {
PsiElement nameIdentifier = getNameIdentifier();
return nameIdentifier == null ? "" : nameIdentifier.getText();
}
@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
project = element.getProject();
String elname = element.getClass().getName();
properties = PropertiesComponent.getInstance(project);
projectPath = project.getBaseDir().getCanonicalPath();
if (elname.endsWith("StringLiteralExpressionImpl")) {
try {
PsiFile file = element.getContainingFile();
VirtualFile vfile = file.getVirtualFile();
if (vfile != null) {
String path = vfile.getPath();
VirtualFile baseDir = project.getBaseDir();
if (baseDir != null) {
String inProtectedPath = path.replace(projectPath, "");
String protectedPath = CommonHelper.searchCurrentProtected(inProtectedPath);
String widgetPath = element.getText().replace("'", "");
String widgetFilePath = "";
if (widgetPath.matches("^components.+")) {
widgetFilePath = protectedPath + "/" + widgetPath.replace(".", "/") + ".php";
} else if (widgetPath.matches("^ext\\..+")) {
widgetFilePath = (protectedPath + "/" + widgetPath.replace(".", "/")).replace("/ext/", "/extensions/") + ".php";
} else if (widgetPath.matches("^app\\..+")) {
widgetFilePath = widgetPath.replace(".", "/").replace("app", protectedPath) + ".php";
} else if (widgetPath.matches("^application.+")) {
widgetFilePath = widgetPath.replace(".", "/").replace("application", protectedPath) + ".php";
} else {
if (!widgetPath.contains(".")) {
String currentFolder = inProtectedPath.replaceAll("[a-z0-9A-Z_]+?.php", "");
VirtualFile existsNear = baseDir.findFileByRelativePath(currentFolder + widgetPath + ".php");
if (existsNear == null) {
VirtualFile existsInParentDir = baseDir.findFileByRelativePath(currentFolder + ".." + "/"
+ widgetPath + ".php");
if (existsInParentDir != null) {
widgetFilePath = currentFolder + ".." + "/" + widgetPath + ".php";
} else {
VirtualFile existsInProtectedComponents = baseDir.findFileByRelativePath(protectedPath
+ "/" + "components" + "/" + widgetPath + ".php");
if (existsInProtectedComponents != null) {
widgetFilePath = protectedPath + "/" + "components" + "/" + widgetPath + ".php";
}
}
} else {
widgetFilePath = currentFolder + widgetPath + ".php";
}
}
}
VirtualFile widgetfile = baseDir.findFileByRelativePath(widgetFilePath);
VirtualFile protectedPathDir = (!protectedPath.equals("")) ? baseDir.findFileByRelativePath(protectedPath) : null;
String str = element.getText();
TextRange textRange = CommonHelper.getTextRange(element, str);
String uri = str.substring(textRange.getStartOffset(), textRange.getEndOffset());
int start = textRange.getStartOffset();
int len = textRange.getLength();
if (widgetfile != null) {
PsiReference ref = new FileReference(widgetfile, uri, element,
new TextRange(start, start + len), project, protectedPathDir, protectedPathDir);
return new PsiReference[]{ref};
}
return PsiReference.EMPTY_ARRAY;
}
}
} catch (Exception e) {
System.err.println("error" + e.getMessage());
}
}
return PsiReference.EMPTY_ARRAY;
}