use of com.intellij.injected.editor.DocumentWindow in project intellij-community by JetBrains.
the class PsiDocumentManagerBase method getPsiFile.
@Override
@Nullable
public PsiFile getPsiFile(@NotNull Document document) {
if (document instanceof DocumentWindow && !((DocumentWindow) document).isValid()) {
return null;
}
final PsiFile userData = document.getUserData(HARD_REF_TO_PSI);
if (userData != null) {
return ensureValidFile(userData, "From hard ref");
}
PsiFile psiFile = getCachedPsiFile(document);
if (psiFile != null) {
return ensureValidFile(psiFile, "Cached PSI");
}
final VirtualFile virtualFile = FileDocumentManager.getInstance().getFile(document);
if (virtualFile == null || !virtualFile.isValid())
return null;
psiFile = getPsiFile(virtualFile);
if (psiFile == null)
return null;
fireFileCreated(document, psiFile);
return psiFile;
}
use of com.intellij.injected.editor.DocumentWindow in project intellij-community by JetBrains.
the class InjectedSelfElementInfo method getInjectedFileIn.
private PsiFile getInjectedFileIn(@NotNull final PsiElement hostContext, @NotNull final PsiFile hostFile, @NotNull final TextRange rangeInHostFile) {
final PsiDocumentManagerBase docManager = (PsiDocumentManagerBase) PsiDocumentManager.getInstance(getProject());
final PsiFile[] result = { null };
final PsiLanguageInjectionHost.InjectedPsiVisitor visitor = new PsiLanguageInjectionHost.InjectedPsiVisitor() {
@Override
public void visit(@NotNull PsiFile injectedPsi, @NotNull List<PsiLanguageInjectionHost.Shred> places) {
Document document = docManager.getDocument(injectedPsi);
if (document instanceof DocumentWindow) {
DocumentWindow window = (DocumentWindow) docManager.getLastCommittedDocument(document);
TextRange hostRange = window.injectedToHost(new TextRange(0, injectedPsi.getTextLength()));
if (hostRange.contains(rangeInHostFile)) {
result[0] = injectedPsi;
}
}
}
};
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
Document document = documentManager.getDocument(hostFile);
if (document != null && documentManager.isUncommited(document)) {
for (DocumentWindow documentWindow : InjectedLanguageManager.getInstance(getProject()).getCachedInjectedDocuments(hostFile)) {
PsiFile injected = documentManager.getPsiFile(documentWindow);
if (injected != null) {
visitor.visit(injected, Collections.<PsiLanguageInjectionHost.Shred>emptyList());
}
}
} else {
List<Pair<PsiElement, TextRange>> injected = InjectedLanguageManager.getInstance(getProject()).getInjectedPsiFiles(hostContext);
if (injected != null) {
for (Pair<PsiElement, TextRange> pair : injected) {
PsiFile injectedFile = pair.first.getContainingFile();
visitor.visit(injectedFile, ContainerUtil.<PsiLanguageInjectionHost.Shred>emptyList());
}
}
}
return result[0];
}
use of com.intellij.injected.editor.DocumentWindow in project intellij-community by JetBrains.
the class CodeStyleManagerRunnable method perform.
public T perform(PsiFile file, int offset, @Nullable TextRange range, T defaultValue) {
if (file instanceof PsiCompiledFile) {
file = ((PsiCompiledFile) file).getDecompiledPsiFile();
}
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myCodeStyleManager.getProject());
Document document = documentManager.getDocument(file);
if (document instanceof DocumentWindow) {
final DocumentWindow documentWindow = (DocumentWindow) document;
final PsiFile topLevelFile = InjectedLanguageManager.getInstance(file.getProject()).getTopLevelFile(file);
if (!file.equals(topLevelFile)) {
if (range != null) {
range = documentWindow.injectedToHost(range);
}
if (offset != -1) {
offset = documentWindow.injectedToHost(offset);
}
return adjustResultForInjected(perform(topLevelFile, offset, range, defaultValue), documentWindow);
}
}
final PsiFile templateFile = PsiUtilCore.getTemplateLanguageFile(file);
if (templateFile != null) {
file = templateFile;
document = documentManager.getDocument(templateFile);
}
PsiElement element = null;
if (offset != -1) {
element = CodeStyleManagerImpl.findElementInTreeWithFormatterEnabled(file, offset);
if (element == null && offset != file.getTextLength()) {
return defaultValue;
}
if (isInsidePlainComment(offset, element)) {
return computeValueInsidePlainComment(file, offset, defaultValue);
}
}
final FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forContext(file);
FormattingModelBuilder elementBuilder = element != null ? LanguageFormatting.INSTANCE.forContext(element) : builder;
if (builder != null && elementBuilder != null) {
mySettings = CodeStyleSettingsManager.getSettings(myCodeStyleManager.getProject());
mySignificantRange = offset != -1 ? getSignificantRange(file, offset) : null;
myIndentOptions = mySettings.getIndentOptionsByFile(file, mySignificantRange);
FormattingMode currentMode = myCodeStyleManager.getCurrentFormattingMode();
myCodeStyleManager.setCurrentFormattingMode(myMode);
try {
myModel = buildModel(builder, file, document);
T result = doPerform(offset, range);
if (result != null) {
return result;
}
} finally {
myCodeStyleManager.setCurrentFormattingMode(currentMode);
}
}
return defaultValue;
}
use of com.intellij.injected.editor.DocumentWindow in project intellij-community by JetBrains.
the class AbstractBlock method buildInjectedBlocks.
@NotNull
private List<Block> buildInjectedBlocks() {
if (myBuildIndentsOnly) {
return EMPTY;
}
if (!(this instanceof SettingsAwareBlock)) {
return EMPTY;
}
PsiElement psi = myNode.getPsi();
if (psi == null) {
return EMPTY;
}
PsiFile file = psi.getContainingFile();
if (file == null) {
return EMPTY;
}
if (InjectedLanguageUtil.getCachedInjectedDocuments(file).isEmpty()) {
return EMPTY;
}
TextRange blockRange = myNode.getTextRange();
List<DocumentWindow> documentWindows = InjectedLanguageUtil.getCachedInjectedDocuments(file);
for (DocumentWindow documentWindow : documentWindows) {
int startOffset = documentWindow.injectedToHost(0);
int endOffset = startOffset + documentWindow.getTextLength();
if (blockRange.containsRange(startOffset, endOffset)) {
PsiFile injected = PsiDocumentManager.getInstance(psi.getProject()).getCachedPsiFile(documentWindow);
if (injected != null) {
List<Block> result = ContainerUtilRt.newArrayList();
DefaultInjectedLanguageBlockBuilder builder = new DefaultInjectedLanguageBlockBuilder(((SettingsAwareBlock) this).getSettings());
builder.addInjectedBlocks(result, myNode, getWrap(), getAlignment(), getIndent());
return result;
}
}
}
return EMPTY;
}
use of com.intellij.injected.editor.DocumentWindow in project intellij-community by JetBrains.
the class ChunkExtractor method extractChunks.
@NotNull
private TextChunk[] extractChunks(@NotNull UsageInfo2UsageAdapter usageInfo2UsageAdapter, @NotNull PsiFile file) {
int absoluteStartOffset = usageInfo2UsageAdapter.getNavigationOffset();
if (absoluteStartOffset == -1)
return TextChunk.EMPTY_ARRAY;
Document visibleDocument = myDocument instanceof DocumentWindow ? ((DocumentWindow) myDocument).getDelegate() : myDocument;
int visibleStartOffset = myDocument instanceof DocumentWindow ? ((DocumentWindow) myDocument).injectedToHost(absoluteStartOffset) : absoluteStartOffset;
int lineNumber = myDocument.getLineNumber(absoluteStartOffset);
int visibleLineNumber = visibleDocument.getLineNumber(visibleStartOffset);
List<TextChunk> result = new ArrayList<>();
appendPrefix(result, visibleLineNumber);
int fragmentToShowStart = myDocument.getLineStartOffset(lineNumber);
int fragmentToShowEnd = fragmentToShowStart < myDocument.getTextLength() ? myDocument.getLineEndOffset(lineNumber) : 0;
if (fragmentToShowStart > fragmentToShowEnd)
return TextChunk.EMPTY_ARRAY;
final CharSequence chars = myDocument.getCharsSequence();
if (fragmentToShowEnd - fragmentToShowStart > MAX_LINE_LENGTH_TO_SHOW) {
final int lineStartOffset = fragmentToShowStart;
fragmentToShowStart = Math.max(lineStartOffset, absoluteStartOffset - OFFSET_BEFORE_TO_SHOW_WHEN_LONG_LINE);
final int lineEndOffset = fragmentToShowEnd;
Segment segment = usageInfo2UsageAdapter.getUsageInfo().getSegment();
int usage_length = segment != null ? segment.getEndOffset() - segment.getStartOffset() : 0;
fragmentToShowEnd = Math.min(lineEndOffset, absoluteStartOffset + usage_length + OFFSET_AFTER_TO_SHOW_WHEN_LONG_LINE);
// this should not cause restarts of the lexer as the tokens are usually words
if (usage_length > 0 && StringUtil.isJavaIdentifierStart(chars.charAt(absoluteStartOffset)) && StringUtil.isJavaIdentifierStart(chars.charAt(absoluteStartOffset + usage_length - 1))) {
while (fragmentToShowEnd < lineEndOffset && StringUtil.isJavaIdentifierStart(chars.charAt(fragmentToShowEnd - 1))) ++fragmentToShowEnd;
while (fragmentToShowStart > lineStartOffset && StringUtil.isJavaIdentifierStart(chars.charAt(fragmentToShowStart))) --fragmentToShowStart;
if (fragmentToShowStart != lineStartOffset)
++fragmentToShowStart;
if (fragmentToShowEnd != lineEndOffset)
--fragmentToShowEnd;
}
}
if (myDocument instanceof DocumentWindow) {
List<TextRange> editable = InjectedLanguageManager.getInstance(file.getProject()).intersectWithAllEditableFragments(file, new TextRange(fragmentToShowStart, fragmentToShowEnd));
for (TextRange range : editable) {
createTextChunks(usageInfo2UsageAdapter, chars, range.getStartOffset(), range.getEndOffset(), true, result);
}
return result.toArray(new TextChunk[result.size()]);
}
return createTextChunks(usageInfo2UsageAdapter, chars, fragmentToShowStart, fragmentToShowEnd, true, result);
}
Aggregations