public void initMarkers(Place shreds) { SmartPointerManager smartPointerManager = SmartPointerManager.getInstance(myProject); int curOffset = -1; for (PsiLanguageInjectionHost.Shred shred : shreds) { final RangeMarker rangeMarker = myNewDocument.createRangeMarker( shred.getRange().getStartOffset() + shred.getPrefix().length(), shred.getRange().getEndOffset() - shred.getSuffix().length()); final TextRange rangeInsideHost = shred.getRangeInsideHost(); PsiLanguageInjectionHost host = shred.getHost(); RangeMarker origMarker = myOrigDocument.createRangeMarker( rangeInsideHost.shiftRight(host.getTextRange().getStartOffset())); SmartPsiElementPointer<PsiLanguageInjectionHost> elementPointer = smartPointerManager.createSmartPsiElementPointer(host); Trinity<RangeMarker, RangeMarker, SmartPsiElementPointer> markers = Trinity.<RangeMarker, RangeMarker, SmartPsiElementPointer>create( origMarker, rangeMarker, elementPointer); myMarkers.add(markers); origMarker.setGreedyToRight(true); rangeMarker.setGreedyToRight(true); if (origMarker.getStartOffset() > curOffset) { origMarker.setGreedyToLeft(true); rangeMarker.setGreedyToLeft(true); } curOffset = origMarker.getEndOffset(); } initGuardedBlocks(shreds); }
private void highlightInjectedSyntax( @NotNull PsiFile injectedPsi, @NotNull HighlightInfoHolder holder) { List<Trinity<IElementType, SmartPsiElementPointer<PsiLanguageInjectionHost>, TextRange>> tokens = InjectedLanguageUtil.getHighlightTokens(injectedPsi); if (tokens == null) return; final Language injectedLanguage = injectedPsi.getLanguage(); Project project = injectedPsi.getProject(); SyntaxHighlighter syntaxHighlighter = SyntaxHighlighterFactory.getSyntaxHighlighter( injectedLanguage, project, injectedPsi.getVirtualFile()); final TextAttributes defaultAttrs = myGlobalScheme.getAttributes(HighlighterColors.TEXT); for (Trinity<IElementType, SmartPsiElementPointer<PsiLanguageInjectionHost>, TextRange> token : tokens) { ProgressManager.checkCanceled(); IElementType tokenType = token.getFirst(); PsiLanguageInjectionHost injectionHost = token.getSecond().getElement(); if (injectionHost == null) continue; TextRange textRange = token.getThird(); TextAttributesKey[] keys = syntaxHighlighter.getTokenHighlights(tokenType); if (textRange.getLength() == 0) continue; TextRange annRange = textRange.shiftRight(injectionHost.getTextRange().getStartOffset()); // force attribute colors to override host' ones TextAttributes attributes = null; for (TextAttributesKey key : keys) { TextAttributes attrs2 = myGlobalScheme.getAttributes(key); if (attrs2 != null) { attributes = attributes == null ? attrs2 : TextAttributes.merge(attributes, attrs2); } } TextAttributes forcedAttributes; if (attributes == null || attributes.isEmpty() || attributes.equals(defaultAttrs)) { forcedAttributes = TextAttributes.ERASE_MARKER; } else { HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.INJECTED_LANGUAGE_FRAGMENT) .range(annRange) .textAttributes(TextAttributes.ERASE_MARKER) .createUnconditionally(); holder.add(info); forcedAttributes = new TextAttributes( attributes.getForegroundColor(), attributes.getBackgroundColor(), attributes.getEffectColor(), attributes.getEffectType(), attributes.getFontType()); } HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.INJECTED_LANGUAGE_FRAGMENT) .range(annRange) .textAttributes(forcedAttributes) .createUnconditionally(); holder.add(info); } }
public static TextRange textRangeToInject(PsiLanguageInjectionHost host) { ASTNode[] children = ((ASTNode) host).getChildren(null); TextRange insideQuotes = new ProperTextRange(0, host.getTextLength()); if (children.length > 1 && children[0].getElementType() == XmlTokenType.XML_ATTRIBUTE_VALUE_START_DELIMITER) { insideQuotes = new ProperTextRange( children[1].getTextRange().getStartOffset() - host.getTextRange().getStartOffset(), insideQuotes.getEndOffset()); } if (children.length > 1 && children[children.length - 1].getElementType() == XmlTokenType.XML_ATTRIBUTE_VALUE_END_DELIMITER) { insideQuotes = new ProperTextRange( insideQuotes.getStartOffset(), children[children.length - 2].getTextRange().getEndOffset() - host.getTextRange().getStartOffset()); } if (host instanceof PsiLiteralExpression) { insideQuotes = new ProperTextRange(1, host.getTextLength() - 1); } return insideQuotes; }
public static void invokeImpl( Project project, Editor editor, final PsiFile file, Injectable injectable) { final PsiLanguageInjectionHost host = findInjectionHost(editor, file); if (host == null) return; if (defaultFunctionalityWorked(host, injectable.getId())) return; try { host.putUserData(FIX_KEY, null); Language language = injectable.toLanguage(); for (LanguageInjectionSupport support : InjectorUtils.getActiveInjectionSupports()) { if (support.isApplicableTo(host) && support.addInjectionInPlace(language, host)) { return; } } if (TemporaryPlacesRegistry.getInstance(project) .getLanguageInjectionSupport() .addInjectionInPlace(language, host)) { final Processor<PsiLanguageInjectionHost> data = host.getUserData(FIX_KEY); String text = StringUtil.escapeXml(language.getDisplayName()) + " was temporarily injected."; if (data != null) { if (!ApplicationManager.getApplication().isUnitTestMode()) { final SmartPsiElementPointer<PsiLanguageInjectionHost> pointer = SmartPointerManager.getInstance(project).createSmartPsiElementPointer(host); final TextRange range = host.getTextRange(); HintManager.getInstance() .showQuestionHint( editor, text + "<br>Do you want to insert annotation? " + KeymapUtil.getFirstKeyboardShortcutText( ActionManager.getInstance() .getAction(IdeActions.ACTION_SHOW_INTENTION_ACTIONS)), range.getStartOffset(), range.getEndOffset(), new QuestionAction() { @Override public boolean execute() { return data.process(pointer.getElement()); } }); } } else { HintManager.getInstance().showInformationHint(editor, text); } } } finally { if (injectable.getLanguage() != null) { // no need for reference injection FileContentUtil.reparseFiles(project, Collections.<VirtualFile>emptyList(), true); } else { ((PsiModificationTrackerImpl) PsiManager.getInstance(project).getModificationTracker()) .incCounter(); DaemonCodeAnalyzer.getInstance(project).restart(); } } }
@Nullable protected static PsiLanguageInjectionHost findInjectionHost(Editor editor, PsiFile file) { if (editor instanceof EditorWindow) return null; final int offset = editor.getCaretModel().getOffset(); final PsiLanguageInjectionHost host = PsiTreeUtil.getParentOfType( file.findElementAt(offset), PsiLanguageInjectionHost.class, false); if (host == null) return null; return host.isValidHost() ? host : null; }
private void commitToOriginalInner() { final String text = myNewDocument.getText(); final Map< PsiLanguageInjectionHost, Set<Trinity<RangeMarker, RangeMarker, SmartPsiElementPointer>>> map = ContainerUtil.classify( myMarkers.iterator(), new Convertor< Trinity<RangeMarker, RangeMarker, SmartPsiElementPointer>, PsiLanguageInjectionHost>() { @Override public PsiLanguageInjectionHost convert( final Trinity<RangeMarker, RangeMarker, SmartPsiElementPointer> o) { final PsiElement element = o.third.getElement(); return (PsiLanguageInjectionHost) element; } }); PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myProject); documentManager.commitDocument(myOrigDocument); // commit here and after each manipulator update int localInsideFileCursor = 0; for (PsiLanguageInjectionHost host : map.keySet()) { if (host == null) continue; String hostText = host.getText(); ProperTextRange insideHost = null; StringBuilder sb = new StringBuilder(); for (Trinity<RangeMarker, RangeMarker, SmartPsiElementPointer> entry : map.get(host)) { RangeMarker origMarker = entry.first; // check for validity? int hostOffset = host.getTextRange().getStartOffset(); ProperTextRange localInsideHost = new ProperTextRange( origMarker.getStartOffset() - hostOffset, origMarker.getEndOffset() - hostOffset); RangeMarker rangeMarker = entry.second; ProperTextRange localInsideFile = new ProperTextRange( Math.max(localInsideFileCursor, rangeMarker.getStartOffset()), rangeMarker.getEndOffset()); if (insideHost != null) { // append unchanged inter-markers fragment sb.append( hostText.substring(insideHost.getEndOffset(), localInsideHost.getStartOffset())); } sb.append( localInsideFile.getEndOffset() <= text.length() && !localInsideFile.isEmpty() ? localInsideFile.substring(text) : ""); localInsideFileCursor = localInsideFile.getEndOffset(); insideHost = insideHost == null ? localInsideHost : insideHost.union(localInsideHost); } assert insideHost != null; ElementManipulators.getManipulator(host).handleContentChange(host, insideHost, sb.toString()); documentManager.commitDocument(myOrigDocument); } }
@Nullable private static RegExpLanguageHost findRegExpHost(@Nullable final PsiElement element) { if (element == null) { return null; } PsiLanguageInjectionHost host = InjectedLanguageManager.getInstance(element.getProject()).getInjectionHost(element); if (host instanceof RegExpLanguageHost) { return (RegExpLanguageHost) host; } if (host != null) { return INSTANCE.forClass(host.getClass()); } return null; }
@NotNull @Override public ResolveResult[] multiResolve(boolean incompleteCode) { ResolveResult[] results = super.multiResolve(incompleteCode); if (results.length == 0) { PsiFile file = myElement.getContainingFile(); final InjectedLanguageManager languageManager = InjectedLanguageManager.getInstance(myElement.getProject()); final PsiLanguageInjectionHost host = languageManager.getInjectionHost(myElement); if (host != null) file = host.getContainingFile(); final String referencedName = myElement.getReferencedName(); if (referencedName == null) return ResolveResult.EMPTY_ARRAY; if (host != null) { final List<Pair<PsiElement, TextRange>> files = languageManager.getInjectedPsiFiles(host); if (files != null) { for (Pair<PsiElement, TextRange> pair : files) { ResolveProcessor processor = new ResolveProcessor(referencedName); PyResolveUtil.scopeCrawlUp( processor, (ScopeOwner) pair.getFirst(), referencedName, pair.getFirst()); final List<RatedResolveResult> resultList = getResultsFromProcessor( referencedName, processor, pair.getFirst(), pair.getFirst()); if (resultList.size() > 0) { List<RatedResolveResult> ret = RatedResolveResult.sorted(resultList); return ret.toArray(new RatedResolveResult[ret.size()]); } } } } ResolveProcessor processor = new ResolveProcessor(referencedName); if (file instanceof ScopeOwner) PyResolveUtil.scopeCrawlUp(processor, (ScopeOwner) file, referencedName, file); final List<RatedResolveResult> resultList = getResultsFromProcessor(referencedName, processor, file, file); if (resultList.size() > 0) { List<RatedResolveResult> ret = RatedResolveResult.sorted(resultList); return ret.toArray(new RatedResolveResult[ret.size()]); } } return results; }
public boolean setHostInjectionEnabled( final PsiLanguageInjectionHost host, final Collection<String> languages, final boolean enabled) { final ArrayList<BaseInjection> originalInjections = new ArrayList<BaseInjection>(); final ArrayList<BaseInjection> newInjections = new ArrayList<BaseInjection>(); for (String supportId : getAllInjectorIds()) { for (BaseInjection injection : getInjections(supportId)) { if (!languages.contains(injection.getInjectedLanguageId())) continue; boolean replace = false; final ArrayList<InjectionPlace> newPlaces = new ArrayList<InjectionPlace>(); for (InjectionPlace place : injection.getInjectionPlaces()) { if (place.isEnabled() != enabled && place.getElementPattern() != null && (place.getElementPattern().accepts(host) || place.getElementPattern().accepts(host.getParent()))) { newPlaces.add(new InjectionPlace(place.getText(), place.getElementPattern(), enabled)); replace = true; } else newPlaces.add(place); } if (replace) { originalInjections.add(injection); final BaseInjection newInjection = injection.copy(); newInjection.getInjectionPlaces().clear(); newInjection.getInjectionPlaces().addAll(newPlaces); newInjections.add(newInjection); } } } if (!originalInjections.isEmpty()) { replaceInjectionsWithUndo( host.getProject(), newInjections, originalInjections, Collections.<PsiElement>emptyList()); return true; } return false; }
@NotNull public Object[] getVariants() { final ArrayList<Object> ret = Lists.newArrayList(super.getVariants()); PsiFile file = myElement.getContainingFile(); final InjectedLanguageManager languageManager = InjectedLanguageManager.getInstance(myElement.getProject()); final PsiLanguageInjectionHost host = languageManager.getInjectionHost(myElement); if (host != null) file = host.getContainingFile(); final PsiElement originalElement = CompletionUtil.getOriginalElement(myElement); final PyQualifiedExpression element = originalElement instanceof PyQualifiedExpression ? (PyQualifiedExpression) originalElement : myElement; // include our own names final CompletionVariantsProcessor processor = new CompletionVariantsProcessor(element); if (file instanceof ScopeOwner) PyResolveUtil.scopeCrawlUp(processor, (ScopeOwner) file, null, null); ret.addAll(processor.getResultList()); return ret.toArray(); }
public static boolean hasInjections(@NotNull PsiLanguageInjectionHost host) { if (!host.isPhysical()) return false; final Ref<Boolean> result = Ref.create(false); enumerate( host, new PsiLanguageInjectionHost.InjectedPsiVisitor() { @Override public void visit( @NotNull final PsiFile injectedPsi, @NotNull final List<PsiLanguageInjectionHost.Shred> places) { result.set(true); } }); return result.get().booleanValue(); }
private boolean addInjectedPsiHighlights( @NotNull PsiFile injectedPsi, TextAttributes injectedAttributes, @NotNull Collection<HighlightInfo> outInfos, @NotNull ProgressIndicator progress, @NotNull InjectedLanguageManager injectedLanguageManager) { DocumentWindow documentWindow = (DocumentWindow) PsiDocumentManager.getInstance(myProject).getCachedDocument(injectedPsi); if (documentWindow == null) return true; Place places = InjectedLanguageUtil.getShreds(injectedPsi); for (PsiLanguageInjectionHost.Shred place : places) { PsiLanguageInjectionHost host = place.getHost(); if (host == null) continue; TextRange textRange = place.getRangeInsideHost().shiftRight(host.getTextRange().getStartOffset()); if (textRange.isEmpty()) continue; String desc = injectedPsi.getLanguage().getDisplayName() + ": " + injectedPsi.getText(); HighlightInfo.Builder builder = HighlightInfo.newHighlightInfo(HighlightInfoType.INJECTED_LANGUAGE_BACKGROUND) .range(textRange); if (injectedAttributes != null && InjectedLanguageUtil.isHighlightInjectionBackground(host)) { builder.textAttributes(injectedAttributes); } builder.unescapedToolTip(desc); HighlightInfo info = builder.createUnconditionally(); info.setFromInjection(true); outInfos.add(info); } HighlightInfoHolder holder = createInfoHolder(injectedPsi); runHighlightVisitorsForInjected(injectedPsi, holder, progress); for (int i = 0; i < holder.size(); i++) { HighlightInfo info = holder.get(i); final int startOffset = documentWindow.injectedToHost(info.startOffset); final TextRange fixedTextRange = getFixedTextRange(documentWindow, startOffset); addPatchedInfos( info, injectedPsi, documentWindow, injectedLanguageManager, fixedTextRange, outInfos); } int injectedStart = holder.size(); highlightInjectedSyntax(injectedPsi, holder); for (int i = injectedStart; i < holder.size(); i++) { HighlightInfo info = holder.get(i); final int startOffset = info.startOffset; final TextRange fixedTextRange = getFixedTextRange(documentWindow, startOffset); if (fixedTextRange == null) { info.setFromInjection(true); outInfos.add(info); } else { HighlightInfo patched = new HighlightInfo( info.forcedTextAttributes, info.forcedTextAttributesKey, info.type, fixedTextRange.getStartOffset(), fixedTextRange.getEndOffset(), info.getDescription(), info.getToolTip(), info.type.getSeverity(null), info.isAfterEndOfLine(), null, false, 0, info.getProblemGroup(), info.getGutterIconRenderer()); patched.setFromInjection(true); outInfos.add(patched); } } if (!isDumbMode()) { List<HighlightInfo> todos = new ArrayList<HighlightInfo>(); highlightTodos( injectedPsi, injectedPsi.getText(), 0, injectedPsi.getTextLength(), progress, myPriorityRange, todos, todos); for (HighlightInfo info : todos) { addPatchedInfos(info, injectedPsi, documentWindow, injectedLanguageManager, null, outInfos); } } advanceProgress(1); return true; }
private static boolean defaultFunctionalityWorked( final PsiLanguageInjectionHost host, String id) { return Configuration.getProjectInstance(host.getProject()) .setHostInjectionEnabled(host, Collections.singleton(id), true); }