use of com.intellij.psi.FileViewProvider in project intellij-plugins by JetBrains.
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 (file.getLanguage() instanceof HbLanguage) {
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, psiElement -> 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-community by JetBrains.
the class LineMarkersPass method doCollectInformation.
@Override
public void doCollectInformation(@NotNull ProgressIndicator progress) {
final List<LineMarkerInfo> lineMarkers = new ArrayList<>();
FileViewProvider viewProvider = myFile.getViewProvider();
for (Language language : viewProvider.getLanguages()) {
final PsiFile root = viewProvider.getPsi(language);
HighlightingLevelManager highlightingLevelManager = HighlightingLevelManager.getInstance(myProject);
if (!highlightingLevelManager.shouldHighlight(root))
continue;
Divider.divideInsideAndOutsideInOneRoot(root, myRestrictRange, myPriorityBounds, elements -> {
Collection<LineMarkerProvider> providers = getMarkerProviders(language, myProject);
List<LineMarkerProvider> providersList = new ArrayList<>(providers);
queryProviders(elements.inside, root, providersList, (element, info) -> {
lineMarkers.add(info);
ApplicationManager.getApplication().invokeLater(() -> LineMarkersUtil.addLineMarkerToEditorIncrementally(myProject, getDocument(), info), myProject.getDisposed());
});
queryProviders(elements.outside, root, providersList, (element, info) -> lineMarkers.add(info));
return true;
});
}
myMarkers = mergeLineMarkers(lineMarkers, getDocument());
if (LOG.isDebugEnabled()) {
LOG.debug("LineMarkersPass.doCollectInformation. lineMarkers: " + lineMarkers + "; merged: " + myMarkers);
}
}
use of com.intellij.psi.FileViewProvider in project intellij-community by JetBrains.
the class ExternalToolPass method collectInformationWithProgress.
@Override
protected void collectInformationWithProgress(@NotNull ProgressIndicator progress) {
final FileViewProvider viewProvider = myFile.getViewProvider();
final Set<Language> relevantLanguages = viewProvider.getLanguages();
int externalAnnotatorsInRoots = 0;
for (Language language : relevantLanguages) {
PsiFile psiRoot = viewProvider.getPsi(language);
if (!HighlightingLevelManager.getInstance(myProject).shouldInspect(psiRoot))
continue;
final List<ExternalAnnotator> externalAnnotators = ExternalLanguageAnnotators.allForFile(language, psiRoot);
externalAnnotatorsInRoots += externalAnnotators.size();
}
setProgressLimit(externalAnnotatorsInRoots);
InspectionProfileImpl profile = InspectionProjectProfileManager.getInstance(myProject).getCurrentProfile();
for (Language language : relevantLanguages) {
PsiFile psiRoot = viewProvider.getPsi(language);
if (!HighlightingLevelManager.getInstance(myProject).shouldInspect(psiRoot))
continue;
final List<ExternalAnnotator> externalAnnotators = ExternalLanguageAnnotators.allForFile(language, psiRoot);
if (!externalAnnotators.isEmpty()) {
DaemonCodeAnalyzerEx daemonCodeAnalyzer = DaemonCodeAnalyzerEx.getInstanceEx(myProject);
boolean errorFound = daemonCodeAnalyzer.getFileStatusMap().wasErrorFound(myDocument);
for (ExternalAnnotator externalAnnotator : externalAnnotators) {
String shortName = externalAnnotator.getPairedBatchInspectionShortName();
if (shortName != null) {
HighlightDisplayKey key = HighlightDisplayKey.find(shortName);
LOG.assertTrue(key != null, "Paired tool '" + shortName + "' not found for external annotator: " + externalAnnotator);
if (!profile.isToolEnabled(key, myFile))
continue;
}
final Object collectedInfo;
Editor editor = getEditor();
if (editor != null) {
collectedInfo = externalAnnotator.collectInformation(psiRoot, editor, errorFound);
} else {
collectedInfo = externalAnnotator.collectInformation(psiRoot);
}
advanceProgress(1);
if (collectedInfo != null) {
myAnnotator2DataMap.put(externalAnnotator, new MyData(psiRoot, collectedInfo));
}
}
}
}
}
use of com.intellij.psi.FileViewProvider in project intellij-community by JetBrains.
the class OffsetsElementSignatureProvider method restoreBySignatureTokens.
@Override
protected PsiElement restoreBySignatureTokens(@NotNull PsiFile file, @NotNull PsiElement parent, @NotNull String type, @NotNull StringTokenizer tokenizer, @Nullable StringBuilder processingInfoStorage) {
if (!TYPE_MARKER.equals(type)) {
if (processingInfoStorage != null) {
processingInfoStorage.append(String.format("Stopping '%s' provider because given signature doesn't have expected type - can work with '%s' but got '%s'%n", getClass().getName(), TYPE_MARKER, type));
}
return null;
}
int start;
int end;
try {
start = Integer.parseInt(tokenizer.nextToken());
end = Integer.parseInt(tokenizer.nextToken());
} catch (NumberFormatException e) {
return null;
}
if (processingInfoStorage != null) {
processingInfoStorage.append(String.format("Parsed target offsets - [%d; %d)%n", start, end));
}
int index = 0;
if (tokenizer.hasMoreTokens()) {
try {
index = Integer.parseInt(tokenizer.nextToken());
} catch (NumberFormatException e) {
// Do nothing
}
}
Language language = file.getLanguage();
if (tokenizer.hasMoreTokens()) {
String languageId = tokenizer.nextToken();
Language languageFromSignature = Language.findLanguageByID(languageId);
if (languageFromSignature == null) {
if (processingInfoStorage != null) {
processingInfoStorage.append(String.format("Couldn't find language for id %s", languageId));
}
} else {
language = languageFromSignature;
}
}
FileViewProvider viewProvider = file.getViewProvider();
PsiElement element = viewProvider.findElementAt(start, language);
if (element == null) {
return null;
}
PsiElement result = findElement(start, end, index, element, processingInfoStorage);
if (result != null) {
return result;
} else if (processingInfoStorage != null) {
processingInfoStorage.append(String.format("Failed to find an element by the given offsets for language %s. Started by the element '%s' (%s)", language, element, element.getText()));
}
PsiFile psiFile = viewProvider.getPsi(language);
if (psiFile == null) {
if (processingInfoStorage != null) {
processingInfoStorage.append(String.format("Couldn't find PSI for language %s", language.toString()));
}
return null;
}
final PsiElement injectedStartElement = InjectedLanguageUtil.findElementAtNoCommit(psiFile, start);
if (processingInfoStorage != null) {
processingInfoStorage.append(String.format("Trying to find injected element starting from the '%s'%s%n", injectedStartElement, injectedStartElement == null ? "" : String.format("(%s)", injectedStartElement.getText())));
}
if (injectedStartElement != null && injectedStartElement != element) {
return findElement(start, end, index, injectedStartElement, processingInfoStorage);
}
return null;
}
use of com.intellij.psi.FileViewProvider in project intellij-community by JetBrains.
the class JavaParsingTestCase method createPsiFile.
private PsiFile createPsiFile(final String name, final String text, final TestParser parser) {
if (TEST_FILE_ELEMENT_TYPE == null) {
TEST_FILE_ELEMENT_TYPE = new MyIFileElementType();
}
TEST_PARSER = parser;
final LightVirtualFile virtualFile = new LightVirtualFile(name + '.' + myFileExt, StdFileTypes.JAVA, text, -1);
final FileViewProvider viewProvider = new SingleRootFileViewProvider(PsiManager.getInstance(getProject()), virtualFile, true);
return new PsiJavaFileImpl(viewProvider) {
@NotNull
@Override
protected FileElement createFileElement(@NotNull final CharSequence text) {
return new FileElement(TEST_FILE_ELEMENT_TYPE, text);
}
};
}
Aggregations