use of com.perl5.lang.perl.psi.PsiPerlHashIndex in project Perl5-IDEA by Camelcade.
the class PerlFormattingScalarDerefExpand method apply.
@Override
public int apply() {
int delta = 0;
if (myScalarDereference.isValid()) {
PsiElement parent = myScalarDereference.getParent();
PsiPerlScalarVariable scalarVariable = PsiTreeUtil.findChildOfType(myScalarDereference, PsiPerlScalarVariable.class);
if (parent.isValid() && (parent instanceof PsiPerlHashElement || parent instanceof PsiPerlArrayElement) && scalarVariable != null && scalarVariable.isValid()) {
PsiElement indexElement = parent.getLastChild();
if (indexElement.isValid() && (indexElement instanceof PsiPerlHashIndex || indexElement instanceof PsiPerlArrayIndex)) {
String newCode = scalarVariable.getNode().getText() + "->" + indexElement.getText();
PerlFileImpl newFile = PerlElementFactory.createFile(myScalarDereference.getProject(), newCode);
PsiPerlDerefExpr derefExpr = PsiTreeUtil.findChildOfType(newFile, PsiPerlDerefExpr.class);
if (derefExpr != null) {
if (parent.getParent() instanceof PsiPerlDerefExpr) {
delta = new PerlFormattingReplace(parent, parent, derefExpr.getFirstChild(), derefExpr.getLastChild()).apply();
} else {
delta = new PerlFormattingReplace(parent, derefExpr).apply();
}
}
}
}
}
return delta;
}
use of com.perl5.lang.perl.psi.PsiPerlHashIndex in project Perl5-IDEA by Camelcade.
the class PerlStringContentCompletionProvider method addCompletions.
@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull final CompletionResultSet result) {
PsiElement element = parameters.getPosition();
PsiElement parent = element.getParent();
if (parent instanceof PsiLanguageInjectionHost && PerlInjectionUtil.hasInjections(parent)) {
return;
}
if (// exporter assignments
EXPORT_ASSIGNED_STRING_CONTENT.accepts(element)) {
PerlStringCompletionUtil.fillWithExportableEntities(element, result);
} else if (// hash indexes
SIMPLE_HASH_INDEX.accepts(element)) {
PsiPerlHashIndex indexElement = PsiTreeUtil.getParentOfType(element, PsiPerlHashIndex.class);
if (indexElement != null && indexElement.getParent() instanceof PsiPerlGlobSlot) {
PerlStringCompletionUtil.fillWithRefTypes(result);
} else {
PerlStringCompletionUtil.fillWithHashIndexes(element, result);
}
} else if (// use or no parameters
USE_PARAMETERS_PATTERN.accepts(element)) {
PerlStringCompletionUtil.fillWithUseParameters(element, result);
} else if (// #@Inject some
parent != null && parent.getParent() instanceof PsiPerlAnnotationInject) {
PerlStringCompletionUtil.fillWithInjectableMarkers(element, result);
result.stopHere();
} else if (// HERE-DOC openers
STRING_CONTENT_IN_HEREDOC_OPENER_PATTERN.accepts(element)) {
PerlStringCompletionUtil.fillWithInjectableMarkers(element, result);
PerlStringCompletionUtil.fillWithHeredocOpeners(element, result);
} else if (// begin of string or qw element
STRING_CONTENT_IN_LIST_OR_STRING_START.accepts(element)) {
PerlStringCompletionUtil.fillWithRefTypes(result);
PerlPackageCompletionUtil.fillWithAllPackageNames(element, result);
}
}
use of com.perl5.lang.perl.psi.PsiPerlHashIndex in project Perl5-IDEA by Camelcade.
the class PerlTypedHandler method charTyped.
@NotNull
@Override
public Result charTyped(char typedChar, @NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
final int offset = editor.getCaretModel().getOffset() - 1;
if (offset < 0) {
return Result.CONTINUE;
}
EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
HighlighterIterator iterator = highlighter.createIterator(offset);
IElementType elementTokenType = iterator.getTokenType();
Document document = editor.getDocument();
if (QUOTE_OPEN_ANY.contains(elementTokenType) && CodeInsightSettings.getInstance().AUTOINSERT_PAIR_QUOTE) {
IElementType quotePrefixType = offset > 0 ? PerlEditorUtil.getPreviousTokenType(highlighter.createIterator(offset - 1)) : null;
CharSequence text = document.getCharsSequence();
if (offset > text.length() - 1 || text.charAt(offset) != typedChar) {
return Result.CONTINUE;
}
if (elementTokenType == QUOTE_DOUBLE_OPEN || elementTokenType == QUOTE_SINGLE_OPEN) {
AutoPopupController.getInstance(project).scheduleAutoPopup(editor);
}
char openChar = text.charAt(offset);
char closeChar = PerlBaseLexer.getQuoteCloseChar(openChar);
iterator.advance();
IElementType possibleCloseQuoteType = iterator.atEnd() ? null : iterator.getTokenType();
if (QUOTE_CLOSE_FIRST_ANY.contains(possibleCloseQuoteType) && closeChar == text.charAt(iterator.getStart())) {
if (DOUBLE_QUOTE_OPENERS.contains(quotePrefixType) && StringUtil.containsChar(HANDLED_BY_BRACE_MATCHER, openChar)) {
iterator.advance();
if (iterator.atEnd() || !QUOTE_OPEN_ANY.contains(iterator.getTokenType())) {
EditorModificationUtil.insertStringAtCaret(editor, Character.toString(closeChar) + openChar, false, false);
}
}
return Result.CONTINUE;
}
StringBuilder textToAppend = new StringBuilder();
textToAppend.append(closeChar);
if (DOUBLE_QUOTE_OPENERS.contains(quotePrefixType)) {
textToAppend.append(openChar);
if (openChar != closeChar) {
textToAppend.append(closeChar);
}
}
EditorModificationUtil.insertStringAtCaret(editor, textToAppend.toString(), false, false);
} else if (elementTokenType == LEFT_BRACE) {
AutoPopupController.getInstance(project).scheduleAutoPopup(editor, CompletionType.BASIC, psiFile -> {
PsiElement newElement = psiFile.findElementAt(offset);
return PsiUtilCore.getElementType(newElement) == elementTokenType && newElement.getParent() instanceof PsiPerlHashIndex;
});
}
return Result.CONTINUE;
}
Aggregations