use of com.perl5.lang.perl.psi.utils.PerlVariableType 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.utils.PerlVariableType 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;
}
use of com.perl5.lang.perl.psi.utils.PerlVariableType in project Perl5-IDEA by Camelcade.
the class PerlVariableMixin method getGlobalDeclarations.
// fixme this need to be moved to PerlResolveUtil or Resolver
@Override
public List<PerlVariableDeclarationElement> getGlobalDeclarations() {
List<PerlVariableDeclarationElement> result = new ArrayList<>();
PerlVariableType myType = getActualType();
// wrapper if any
PsiElement parent = getParent();
if (myType == PerlVariableType.SCALAR) {
for (PerlVariableDeclarationElement variable : PerlScalarUtil.getGlobalScalarDefinitions(getProject(), getCanonicalName())) {
if (!variable.equals(parent)) {
result.add(variable);
}
}
} else if (myType == PerlVariableType.ARRAY) {
for (PerlVariableDeclarationElement variable : PerlArrayUtil.getGlobalArrayDefinitions(getProject(), getCanonicalName())) {
if (!variable.equals(parent)) {
result.add(variable);
}
}
} else if (myType == PerlVariableType.HASH) {
for (PerlVariableDeclarationElement variable : PerlHashUtil.getGlobalHashDefinitions(getProject(), getCanonicalName())) {
if (!variable.equals(parent)) {
result.add(variable);
}
}
}
return result;
}
use of com.perl5.lang.perl.psi.utils.PerlVariableType in project Perl5-IDEA by Camelcade.
the class PerlBuiltInSubsService method readArgument.
@Nullable
private PerlSubArgument readArgument(@NotNull Element element, boolean isOptional, String subName) {
String variableName = element.getAttribute("name").getValue();
if (StringUtil.isEmpty(variableName)) {
LOG.warn("Missing argument name for " + subName);
return null;
}
PerlVariableType variableType;
String type = element.getAttribute("type").getValue();
if (type == null || type.length() != 1 || (variableType = PerlVariableType.bySigil(type.charAt(0))) == null) {
LOG.warn("Unknown type modifier for argument: " + variableName + " in " + subName);
return null;
}
return PerlSubArgument.create(variableType, variableName, isOptional);
}
use of com.perl5.lang.perl.psi.utils.PerlVariableType in project Perl5-IDEA by Camelcade.
the class PerlDocUtil method getPerlVarDoc.
@Nullable
public static PsiElement getPerlVarDoc(PerlVariable variable) {
final Project project = variable.getProject();
PerlVariableType actualType = variable.getActualType();
String variableName = variable.getName();
if (actualType != null && StringUtil.isNotEmpty(variableName)) {
String text = actualType.getSigil() + variableName;
if (myVariablesRedirections.containsKey(text)) {
return resolveDocLink(myVariablesRedirections.get(text), variable);
}
if (variable.isBuiltIn()) {
PodDocumentPattern pattern = PodDocumentPattern.itemPattern(text);
if (text.matches("\\$[123456789]")) {
pattern.setItemPattern("$<digits>");
}
return searchPodElementInFile(project, PodSearchHelper.PERL_VAR_FILE_NAME, pattern);
}
}
return null;
}
Aggregations