use of com.perl5.lang.perl.psi.PerlVariable in project Perl5-IDEA by Camelcade.
the class PerlDocumentationProvider method getCustomDocumentationElement.
@Nullable
@Override
public PsiElement getCustomDocumentationElement(@NotNull Editor editor, @NotNull PsiFile file, @Nullable PsiElement contextElement) {
if (contextElement == null || contextElement.getLanguage() != PerlLanguage.INSTANCE) {
return null;
}
IElementType elementType = contextElement.getNode().getElementType();
if (contextElement instanceof PerlVariable) {
// fixme try to search doc in package or declaration
return PerlDocUtil.getPerlVarDoc((PerlVariable) contextElement);
} else if (elementType == REGEX_MODIFIER) {
return PerlDocUtil.getRegexModifierDoc(contextElement);
} else if (elementType == REGEX_TOKEN) {
return PerlDocUtil.resolveDocLink("perlretut", contextElement);
} else if (elementType == VERSION_ELEMENT) {
return PerlDocUtil.resolveDocLink("perldata/\"Version Strings\"", contextElement);
} else if (isFunc(contextElement)) {
return PerlDocUtil.getPerlFuncDoc(contextElement);
} else if (isOp(contextElement)) {
return PerlDocUtil.getPerlOpDoc(contextElement);
} else if (contextElement instanceof PerlSubNameElement) {
String packageName = ((PerlSubNameElement) contextElement).getPackageName();
String subName = ((PerlSubNameElement) contextElement).getName();
if (StringUtil.isNotEmpty(subName)) {
PsiElement result = null;
// search by link
if (StringUtil.isNotEmpty(packageName) && !StringUtil.equals(PerlPackageUtil.MAIN_PACKAGE, packageName)) {
result = PerlDocUtil.resolveDocLink(packageName + "/" + ((PerlSubNameElement) contextElement).getName(), contextElement);
}
// not found or main::
if (result == null) {
PsiElement target = null;
if (contextElement.getParent() instanceof PerlSubElement) {
target = contextElement.getParent();
} else {
PsiReference reference = contextElement.getReference();
if (reference != null) {
target = reference.resolve();
}
}
if (target != null) {
PsiFile targetFile = target.getContainingFile();
if (targetFile != null) {
PsiFile podFile = targetFile.getViewProvider().getPsi(PodLanguage.INSTANCE);
if (podFile != null) {
result = PerlDocUtil.searchPodElement(targetFile, PodDocumentPattern.headingAndItemPattern(subName));
}
}
}
}
if (result != null) {
return result;
}
}
} else if (contextElement instanceof PerlNamespaceElement) {
String packageName = ((PerlNamespaceElement) contextElement).getCanonicalName();
if (StringUtil.equals(PerlPackageUtil.SUPER_PACKAGE, packageName)) {
return PerlDocUtil.resolveDocLink("perlobj/Inheritance", contextElement);
} else if (StringUtil.isNotEmpty(packageName)) {
return PerlDocUtil.resolveDocLink(packageName, contextElement);
}
}
return super.getCustomDocumentationElement(editor, file, contextElement);
}
use of com.perl5.lang.perl.psi.PerlVariable in project Perl5-IDEA by Camelcade.
the class PerlVariableShadowingInspection method checkDeclaration.
@Override
public void checkDeclaration(ProblemsHolder holder, PerlVariableDeclarationElement variableDeclarationWrapper) {
PerlVariable variable = variableDeclarationWrapper.getVariable();
PsiElement declarationContainer = variableDeclarationWrapper.getParent();
if (variable != null && !(declarationContainer instanceof PsiPerlVariableDeclarationLocal)) {
PerlVariableDeclarationElement lexicalDeclaration = PerlResolveUtil.getLexicalDeclaration(variable);
if (lexicalDeclaration instanceof PerlBuiltInVariable) {
registerProblem(holder, variable, PerlBundle.message("perl.inspection.shadows.builtin", lexicalDeclaration.getVariable().getLineNumber()));
} else if (lexicalDeclaration instanceof PerlImplicitVariableDeclaration) {
registerProblem(holder, variable, PerlBundle.message("perl.inspection.shadows.implicit", lexicalDeclaration.getVariable().getLineNumber()));
} else if (lexicalDeclaration != null) {
registerProblem(holder, variable, PerlBundle.message("perl.inspection.shadows.other", lexicalDeclaration.getVariable().getLineNumber()));
}
}
}
use of com.perl5.lang.perl.psi.PerlVariable in project Perl5-IDEA by Camelcade.
the class PerlVariableNameCompletionProvider method fillWithFullQualifiedVariables.
private void fillWithFullQualifiedVariables(@NotNull PsiElement variableNameElement, @NotNull CompletionResultSet resultSet) {
PsiElement perlVariable = variableNameElement.getParent();
Project project = variableNameElement.getProject();
GlobalSearchScope resolveScope = variableNameElement.getResolveScope();
String variableName = variableNameElement.getText();
boolean forceShortMain = StringUtil.startsWith(variableName, PerlPackageUtil.PACKAGE_SEPARATOR);
final CompletionResultSet finalResultSet = resultSet;
Processor<PerlVariableDeclarationElement> scalarDefaultProcessor = wrapper -> {
String fullQualifiedName = wrapper.getFullQualifiedName();
if (fullQualifiedName != null) {
finalResultSet.addElement(PerlVariableCompletionUtil.getScalarLookupElement(adjustName(fullQualifiedName, forceShortMain)));
}
return true;
};
Processor<PerlVariableDeclarationElement> arrayDefaultProcessor = wrapper -> {
String fullQualifiedName = wrapper.getFullQualifiedName();
if (fullQualifiedName != null) {
finalResultSet.addElement(PerlVariableCompletionUtil.getArrayLookupElement(adjustName(fullQualifiedName, forceShortMain)));
}
return true;
};
Processor<PerlVariableDeclarationElement> hashDefaultProcessor = wrapper -> {
String fullQualifiedName = wrapper.getFullQualifiedName();
if (fullQualifiedName != null) {
finalResultSet.addElement(PerlVariableCompletionUtil.getHashLookupElement(adjustName(fullQualifiedName, forceShortMain)));
}
return true;
};
if (perlVariable instanceof PsiPerlScalarVariable) {
PerlScalarUtil.processDefinedGlobalScalars(project, resolveScope, scalarDefaultProcessor);
PerlArrayUtil.processDefinedGlobalArrays(project, resolveScope, wrapper -> {
String fullQualifiedName = wrapper.getFullQualifiedName();
if (fullQualifiedName != null) {
finalResultSet.addElement(PerlVariableCompletionUtil.getArrayElementLookupElement(adjustName(fullQualifiedName, forceShortMain)));
}
return true;
});
PerlHashUtil.processDefinedGlobalHashes(project, resolveScope, wrapper -> {
String fullQualifiedName = wrapper.getFullQualifiedName();
if (fullQualifiedName != null) {
finalResultSet.addElement(PerlVariableCompletionUtil.getHashElementLookupElement(adjustName(fullQualifiedName, forceShortMain)));
}
return true;
});
} else if (perlVariable instanceof PerlGlobVariable) {
PerlScalarUtil.processDefinedGlobalScalars(project, resolveScope, scalarDefaultProcessor);
PerlArrayUtil.processDefinedGlobalArrays(project, resolveScope, arrayDefaultProcessor);
PerlHashUtil.processDefinedGlobalHashes(project, resolveScope, hashDefaultProcessor);
// globs
PerlGlobUtil.processDefinedGlobsNames(project, resolveScope, typeglob -> {
String adjustedName = adjustName(typeglob.getCanonicalName(), forceShortMain);
if (adjustedName != null) {
finalResultSet.addElement(PerlVariableCompletionUtil.getGlobLookupElement(adjustedName));
}
return true;
});
} else if (perlVariable instanceof PsiPerlArrayVariable) {
PerlArrayUtil.processDefinedGlobalArrays(project, resolveScope, arrayDefaultProcessor);
PerlHashUtil.processDefinedGlobalHashes(project, resolveScope, wrapper -> {
String fullQualifiedName = wrapper.getFullQualifiedName();
if (fullQualifiedName != null) {
finalResultSet.addElement(PerlVariableCompletionUtil.getHashSliceLookupElement(adjustName(fullQualifiedName, forceShortMain)));
}
return true;
});
} else if (perlVariable instanceof PsiPerlArrayIndexVariable) {
// global arrays
PerlArrayUtil.processDefinedGlobalArrays(project, resolveScope, arrayDefaultProcessor);
} else if (perlVariable instanceof PsiPerlHashVariable) {
// global hashes
PerlHashUtil.processDefinedGlobalHashes(project, resolveScope, hashDefaultProcessor);
}
}
use of com.perl5.lang.perl.psi.PerlVariable in project Perl5-IDEA by Camelcade.
the class PerlVariableCompletionUtil method fillWithUnresolvedVars.
public static void fillWithUnresolvedVars(@NotNull PerlVariableNameElement variableNameElement, @NotNull CompletionResultSet resultSet) {
final PerlLexicalScope lexicalScope = PsiTreeUtil.getParentOfType(variableNameElement, PerlLexicalScope.class);
PsiElement perlVariable = variableNameElement.getParent();
final Set<String> collectedNames = new THashSet<>();
if (lexicalScope != null && perlVariable instanceof PerlVariable) {
final int minOffset = variableNameElement.getTextOffset();
final PerlVariableType actualType = ((PerlVariable) perlVariable).getActualType();
lexicalScope.accept(new PerlRecursiveVisitor() {
@Override
public void visitPerlVariable(@NotNull PerlVariable perlVariable) {
if (perlVariable.isValid() && !(perlVariable.getParent() instanceof PerlVariableDeclarationElement) && perlVariable.getTextOffset() > minOffset && actualType == perlVariable.getActualType()) {
String variableName = perlVariable.getName();
if (StringUtil.isNotEmpty(variableName) && !collectedNames.contains(variableName) && perlVariable.getLexicalDeclaration() == null) {
collectedNames.add(variableName);
resultSet.addElement(LookupElementBuilder.create(variableName));
}
}
super.visitPerlVariable(perlVariable);
}
});
}
}
use of com.perl5.lang.perl.psi.PerlVariable in project Perl5-IDEA by Camelcade.
the class PerlXNamedValue method computeMySourcePosition.
protected boolean computeMySourcePosition(@Nullable XNavigatable navigatable, @Nullable final XInlineDebuggerDataCallback callback) {
String name = myPerlValueDescriptor.getName();
if (StringUtil.isEmpty(name) || name.length() < 2) {
return false;
}
final PerlVariableType variableType = PerlVariableType.bySigil(name.charAt(0));
if (variableType == null || variableType == PerlVariableType.CODE) {
return false;
}
final String variableName = name.substring(1);
final XSourcePosition sourcePosition = myStackFrame.getSourcePosition();
if (sourcePosition == null) {
return false;
}
final Project project = myStackFrame.getPerlExecutionStack().getSuspendContext().getDebugSession().getProject();
final VirtualFile virtualFile = sourcePosition.getFile();
PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
if (!(psiFile instanceof PerlFileImpl)) {
return false;
}
PsiElement element = psiFile.findElementAt(sourcePosition.getOffset());
if (element == null) {
return false;
}
if (navigatable != null) {
PerlVariableDeclarationSearcher variableProcessor = new PerlVariableDeclarationSearcher(variableName, variableType, element);
PerlResolveUtil.treeWalkUp(element, variableProcessor);
PerlVariableDeclarationElement result = variableProcessor.getResult();
if (result == null) {
return false;
}
navigatable.setSourcePosition(XSourcePositionImpl.createByElement(result));
} else if (callback != null) {
final Document document = psiFile.getViewProvider().getDocument();
if (document == null) {
return false;
}
final boolean[] found = new boolean[] { false };
PerlVariableDeclarationSearcher variableProcessor = new PerlVariableDeclarationSearcher(variableName, variableType, element) {
@Override
public boolean execute(@NotNull PsiElement possibleElement, @NotNull ResolveState state) {
boolean result = super.execute(possibleElement, state);
if (!result) {
registerElement(getResult());
} else if (possibleElement instanceof PerlVariable && ((PerlVariable) possibleElement).getActualType() == variableType && StringUtil.equals(variableName, ((PerlVariable) possibleElement).getName())) {
registerElement(possibleElement);
}
return result;
}
private void registerElement(@Nullable PsiElement targetElement) {
if (targetElement == null) {
return;
}
found[0] = true;
try {
if (mySourcePositionMethod != null) {
mySourcePositionMethod.invoke(callback, XSourcePositionImpl.createByElement(targetElement));
} else if (myLegacyMethod != null) {
myLegacyMethod.invoke(callback, virtualFile, document, document.getLineNumber(targetElement.getTextOffset()));
} else {
found[0] = false;
}
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
};
PerlResolveUtil.treeWalkUp(element, variableProcessor);
return found[0];
}
return true;
}
Aggregations