use of com.intellij.psi.FileViewProvider in project idea-handlebars by dmarcotte.
the class HbTypedHandler method charTyped.
@Override
public Result charTyped(char c, Project project, @NotNull Editor editor, @NotNull PsiFile file) {
int offset = editor.getCaretModel().getOffset();
FileViewProvider provider = file.getViewProvider();
if (!provider.getBaseLanguage().isKindOf(HbLanguage.INSTANCE)) {
return Result.CONTINUE;
}
if (offset < 2 || offset > editor.getDocument().getTextLength()) {
return Result.CONTINUE;
}
String previousChar = editor.getDocument().getText(new TextRange(offset - 2, offset - 1));
boolean closeBraceCompleted = false;
if (provider instanceof HbFileViewProvider) {
if (HbConfig.isAutocompleteMustachesEnabled() && c == '}' && !previousChar.equals("}")) {
// we may be able to complete the second brace
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
PsiElement elementAt = provider.findElementAt(offset - 1, provider.getBaseLanguage());
ASTNode node = elementAt != null ? elementAt.getNode() : null;
if (node != null && node.getElementType() == HbTokenTypes.INVALID) {
// we should be looking at the beginning of a close brace. Find its matching open brace and auto-complete based on its type
PsiElement mustache = PsiTreeUtil.findFirstParent(elementAt, new Condition<PsiElement>() {
@Override
public boolean value(PsiElement psiElement) {
return psiElement instanceof HbMustache;
}
});
if (mustache != null) {
String braceCompleter;
if (mustache.getFirstChild().getNode().getElementType() == HbTokenTypes.OPEN_UNESCAPED) {
// add "}}" to complete the CLOSE_UNESCAPED
braceCompleter = "}}";
} else {
// add "}" to complete the CLOSE
braceCompleter = "}";
}
editor.getDocument().insertString(offset, braceCompleter);
offset = offset + braceCompleter.length();
editor.getCaretModel().moveToOffset(offset);
closeBraceCompleted = true;
}
}
}
}
// if we just completed a close brace or the user just typed one, we may have some business to attend to
if (closeBraceCompleted || (c == '}' && previousChar.equals("}"))) {
autoInsertCloseTag(project, offset, editor, provider);
adjustMustacheFormatting(project, offset, editor, file, provider);
} else if (c == '/' && previousChar.equals("{")) {
finishClosingTag(offset, editor, provider);
}
return Result.CONTINUE;
}
use of com.intellij.psi.FileViewProvider in project intellij-elixir by KronicDeth.
the class Logger method excerpt.
@NotNull
private static String excerpt(@NotNull PsiFile containingFile, @NotNull PsiElement element) {
StringBuilder excerptBuilder = new StringBuilder();
excerptBuilder.append('\n');
excerptBuilder.append("### Excerpt\n");
excerptBuilder.append('\n');
excerptBuilder.append("```\n");
excerptBuilder.append(element.getText());
excerptBuilder.append('\n');
excerptBuilder.append("```\n");
FileViewProvider fileViewProvider = containingFile.getViewProvider();
Document document = fileViewProvider.getDocument();
if (document != null) {
TextRange textRange = element.getTextRange();
int startingLine = document.getLineNumber(textRange.getStartOffset());
int endingLine = document.getLineNumber(textRange.getEndOffset());
excerptBuilder.append(" Line(s) ");
excerptBuilder.append(startingLine);
excerptBuilder.append('-');
excerptBuilder.append(endingLine);
VirtualFile virtualFile = containingFile.getVirtualFile();
if (virtualFile != null) {
excerptBuilder.append(" in ");
excerptBuilder.append(virtualFile.getPath());
}
excerptBuilder.append("\n");
}
return excerptBuilder.toString();
}
use of com.intellij.psi.FileViewProvider in project intellij-community by JetBrains.
the class XPathEvalAction method execute.
private void execute(Editor editor) {
final Project project = editor.getProject();
final PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
if (psiFile == null) {
return;
}
InputExpressionDialog.Context input;
XmlElement contextNode = null;
final Config cfg = XPathAppComponent.getInstance().getConfig();
do {
RangeHighlighter contextHighlighter = null;
if (cfg.isUseContextAtCursor()) {
// find out current context node
contextNode = MyPsiUtil.findContextNode(psiFile, editor);
if (contextNode != null) {
contextHighlighter = HighlighterUtil.highlightNode(editor, contextNode, cfg.getContextAttributes(), cfg);
}
}
if (contextNode == null) {
// in XPath data model, / is the document itself, including comments, PIs and the root element
contextNode = ((XmlFile) psiFile).getDocument();
if (contextNode == null) {
FileViewProvider fileViewProvider = psiFile.getViewProvider();
if (fileViewProvider instanceof TemplateLanguageFileViewProvider) {
Language dataLanguage = ((TemplateLanguageFileViewProvider) fileViewProvider).getTemplateDataLanguage();
PsiFile templateDataFile = fileViewProvider.getPsi(dataLanguage);
if (templateDataFile instanceof XmlFile)
contextNode = ((XmlFile) templateDataFile).getDocument();
}
}
}
input = inputXPathExpression(project, contextNode);
if (contextHighlighter != null) {
contextHighlighter.dispose();
}
if (input == null) {
return;
}
HighlighterUtil.clearHighlighters(editor);
} while (contextNode != null && evaluateExpression(input, contextNode, editor, cfg));
}
use of com.intellij.psi.FileViewProvider in project intellij-community by JetBrains.
the class XmlUnwrapDescriptor method collectUnwrappers.
@Override
public List<Pair<PsiElement, Unwrapper>> collectUnwrappers(Project project, Editor editor, PsiFile file) {
int offset = editor.getCaretModel().getOffset();
PsiElement e1 = file.findElementAt(offset);
if (e1 != null) {
Language language = e1.getParent().getLanguage();
if (language != file.getLanguage()) {
UnwrapDescriptor unwrapDescriptor = LanguageUnwrappers.INSTANCE.forLanguage(language);
if (unwrapDescriptor != null && !(unwrapDescriptor instanceof XmlUnwrapDescriptor)) {
return unwrapDescriptor.collectUnwrappers(project, editor, file);
}
}
}
List<Pair<PsiElement, Unwrapper>> result = new ArrayList<>();
FileViewProvider viewProvider = file.getViewProvider();
for (Language language : viewProvider.getLanguages()) {
UnwrapDescriptor unwrapDescriptor = LanguageUnwrappers.INSTANCE.forLanguage(language);
if (unwrapDescriptor instanceof XmlUnwrapDescriptor) {
PsiElement e = viewProvider.findElementAt(offset, language);
PsiElement tag = PsiTreeUtil.getParentOfType(e, XmlTag.class);
while (tag != null) {
if (XmlChildRole.START_TAG_NAME_FINDER.findChild(tag.getNode()) != null) {
// Exclude implicit tags suck as 'jsp:root'
result.add(new Pair<>(tag, new XmlEnclosingTagUnwrapper()));
}
tag = PsiTreeUtil.getParentOfType(tag, XmlTag.class);
}
}
}
Collections.sort(result, (o1, o2) -> o2.first.getTextOffset() - o1.first.getTextOffset());
return result;
}
use of com.intellij.psi.FileViewProvider in project intellij-community by JetBrains.
the class XmlAutoPopupHandler method autoPopupXmlLookup.
public static void autoPopupXmlLookup(final Project project, final Editor editor) {
AutoPopupController.getInstance(project).autoPopupMemberLookup(editor, file -> {
int offset = editor.getCaretModel().getOffset();
PsiElement lastElement = InjectedLanguageUtil.findElementAtNoCommit(file, offset - 1);
if (lastElement instanceof PsiFile) {
lastElement = file.findElementAt(offset - 1);
}
if (lastElement == null || !lastElement.isValid())
return false;
if (doCompleteIfNeeded(offset, file, lastElement)) {
return true;
}
FileViewProvider fileViewProvider = file.getViewProvider();
Language templateDataLanguage;
final PsiElement parent = lastElement.getParent();
if (fileViewProvider instanceof TemplateLanguageFileViewProvider && (templateDataLanguage = ((TemplateLanguageFileViewProvider) fileViewProvider).getTemplateDataLanguage()) != parent.getLanguage()) {
lastElement = fileViewProvider.findElementAt(offset - 1, templateDataLanguage);
if (lastElement == null || !lastElement.isValid())
return false;
return doCompleteIfNeeded(offset, file, lastElement);
}
return false;
});
}
Aggregations