com.intellij.psi.PsiLanguageInjectionHost Java Examples

The following examples show how to use com.intellij.psi.PsiLanguageInjectionHost. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QuickEditAction.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
protected Pair<PsiElement, TextRange> getRangePair(final PsiFile file, final Editor editor) {
  final int offset = editor.getCaretModel().getOffset();
  final PsiLanguageInjectionHost host =
          PsiTreeUtil.getParentOfType(file.findElementAt(offset), PsiLanguageInjectionHost.class, false);
  if (host == null || ElementManipulators.getManipulator(host) == null) return null;
  final List<Pair<PsiElement, TextRange>> injections = InjectedLanguageManager.getInstance(host.getProject()).getInjectedPsiFiles(host);
  if (injections == null || injections.isEmpty()) return null;
  final int offsetInElement = offset - host.getTextRange().getStartOffset();
  final Pair<PsiElement, TextRange> rangePair = ContainerUtil.find(injections, new Condition<Pair<PsiElement, TextRange>>() {
    @Override
    public boolean value(final Pair<PsiElement, TextRange> pair) {
      return pair.second.containsRange(offsetInElement, offsetInElement);
    }
  });
  if (rangePair != null) {
    final Language language = rangePair.first.getContainingFile().getLanguage();
    final Object action = language.getUserData(EDIT_ACTION_AVAILABLE);
    if (action != null && action.equals(false)) return null;

    myLastLanguageName = language.getDisplayName();
  }
  return rangePair;
}
 
Example #2
Source File: DocumentWindowImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
private String calcText() {
  StringBuilder text = new StringBuilder();
  CharSequence hostText = myDelegate.getCharsSequence();
  synchronized (myLock) {
    for (PsiLanguageInjectionHost.Shred shred : myShreds) {
      Segment hostRange = shred.getHostRangeMarker();
      if (hostRange != null) {
        text.append(shred.getPrefix());
        text.append(hostText, hostRange.getStartOffset(), hostRange.getEndOffset());
        text.append(shred.getSuffix());
      }
    }
  }
  return text.toString();
}
 
Example #3
Source File: BashIdentityStringLiteralEscaperTest.java    From BashSupport with Apache License 2.0 6 votes vote down vote up
@Test
public void testEncoding() throws Exception {
    PsiFile psiFile = myFixture.configureByText(BashFileType.BASH_FILE_TYPE, " 'abc\n'");

    BashWordImpl word = PsiTreeUtil.findChildOfType(psiFile, BashWordImpl.class);
    Assert.assertNotNull(word);

    LiteralTextEscaper<? extends PsiLanguageInjectionHost> escaper = word.createLiteralTextEscaper();
    Assert.assertTrue(escaper instanceof BashIdentityStringLiteralEscaper<?>);

    TextRange range = TextRange.allOf(word.getUnwrappedCharSequence());
    Assert.assertEquals(0, escaper.getOffsetInHost(0, range));
    Assert.assertEquals(1, escaper.getOffsetInHost(1, range));
    Assert.assertEquals(2, escaper.getOffsetInHost(2, range));
    Assert.assertEquals(3, escaper.getOffsetInHost(3, range));
    Assert.assertEquals(4, escaper.getOffsetInHost(4, range));
}
 
Example #4
Source File: InjectionReferenceTest.java    From BashSupport with Apache License 2.0 6 votes vote down vote up
@NotNull
private PsiElement findInjectedBashReference(String fileName, String lookupText) {
    PsiElement javaLiteral = configurePsiAtCaret(fileName);
    Assert.assertTrue(javaLiteral instanceof PsiLanguageInjectionHost);

    //inject bash into the literal
    InjectLanguageAction.invokeImpl(getProject(), myFixture.getEditor(), javaLiteral.getContainingFile(), Injectable.fromLanguage(BashFileType.BASH_LANGUAGE));

    String fileContent = javaLiteral.getContainingFile().getText();
    PsiElement bashPsiLeaf = InjectedLanguageManager.getInstance(getProject()).findInjectedElementAt(myFixture.getFile(), fileContent.indexOf(lookupText) + 1);
    Assert.assertNotNull(bashPsiLeaf);

    PsiElement reference = PsiTreeUtil.findFirstParent(bashPsiLeaf, psiElement -> psiElement.getReference() != null);
    Assert.assertNotNull(reference);

    return reference;
}
 
Example #5
Source File: DocumentWindowImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public int getLineStartOffset(int line) {
  LOG.assertTrue(line >= 0, line);
  if (line == 0) return 0;
  String hostText = myDelegate.getText();

  int[] pos = new int[2]; // pos[0] = curLine; pos[1] == offset;
  synchronized (myLock) {
    for (PsiLanguageInjectionHost.Shred shred : myShreds) {
      Segment hostRange = shred.getHostRangeMarker();
      if (hostRange == null) continue;

      int found = countNewLinesIn(shred.getPrefix(), pos, line);
      if (found != -1) return found;

      CharSequence text = hostText.subSequence(hostRange.getStartOffset(), hostRange.getEndOffset());
      found = countNewLinesIn(text, pos, line);
      if (found != -1) return found;

      found = countNewLinesIn(shred.getSuffix(), pos, line);
      if (found != -1) return found;
    }
  }

  return pos[1];
}
 
Example #6
Source File: BashEnhancedLiteralTextEscaperTest.java    From BashSupport with Apache License 2.0 6 votes vote down vote up
@Test
public void testEscaped1() throws Exception {
    BashWordImpl psiElement = (BashWordImpl) BashPsiElementFactory.createWord(myProject, "$'a\\'a'");
    Assert.assertNotNull(psiElement);

    LiteralTextEscaper<? extends PsiLanguageInjectionHost> textEscaper = psiElement.createLiteralTextEscaper();

    StringBuilder content = new StringBuilder();
    TextRange range = psiElement.getTextContentRange();
    textEscaper.decode(range, content);
    Assert.assertEquals("a'a", content.toString());

    //check the offsets
    Assert.assertEquals(2, textEscaper.getOffsetInHost(0, range)); // a at 2
    Assert.assertEquals(3, textEscaper.getOffsetInHost(1, range)); // ' at 3-4
    Assert.assertEquals(5, textEscaper.getOffsetInHost(2, range)); // a at 5
}
 
Example #7
Source File: BashEnhancedLiteralTextEscaperTest.java    From BashSupport with Apache License 2.0 6 votes vote down vote up
@Test
public void testEscaped2() throws Exception {
    // unescpaed content: a\\"'a
    // java escapes \\ to \\\\
    // bash escapes \\\\ to \\\\\\\\
    BashWordImpl psiElement = (BashWordImpl) BashPsiElementFactory.createWord(myProject, "$'a\\\\\\\\\"\\'a\\'");
    Assert.assertNotNull(psiElement);

    LiteralTextEscaper<? extends PsiLanguageInjectionHost> textEscaper = psiElement.createLiteralTextEscaper();

    StringBuilder content = new StringBuilder();
    TextRange range = psiElement.getTextContentRange();
    textEscaper.decode(range, content);

    //decoded text is a\\"'a
    Assert.assertEquals("a\\\\\"'a", content.toString());

    //check the offsets
    Assert.assertEquals(2, textEscaper.getOffsetInHost(0, range)); // a at 2
    Assert.assertEquals(3, textEscaper.getOffsetInHost(1, range)); // \ at 3-4
    Assert.assertEquals(5, textEscaper.getOffsetInHost(2, range)); // \ at 5-6
    Assert.assertEquals(7, textEscaper.getOffsetInHost(3, range)); // " at 7
    Assert.assertEquals(8, textEscaper.getOffsetInHost(4, range)); // ' at 8-9
    Assert.assertEquals(10, textEscaper.getOffsetInHost(5, range)); // a at 10
}
 
Example #8
Source File: DocumentWindowImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public boolean areRangesEqual(@Nonnull DocumentWindow other) {
  DocumentWindowImpl window = (DocumentWindowImpl)other;
  Place shreds = getShreds();
  Place otherShreds = window.getShreds();
  if (shreds.size() != otherShreds.size()) return false;
  for (int i = 0; i < shreds.size(); i++) {
    PsiLanguageInjectionHost.Shred shred = shreds.get(i);
    PsiLanguageInjectionHost.Shred otherShred = otherShreds.get(i);
    if (!shred.getPrefix().equals(otherShred.getPrefix())) return false;
    if (!shred.getSuffix().equals(otherShred.getSuffix())) return false;

    Segment hostRange = shred.getHostRangeMarker();
    Segment otherRange = otherShred.getHostRangeMarker();
    if (hostRange == null || otherRange == null || !TextRange.areSegmentsEqual(hostRange, otherRange)) return false;
  }
  return true;
}
 
Example #9
Source File: SQLInjector.java    From dbunit-extractor with MIT License 6 votes vote down vote up
@Override
public void getLanguagesToInject(@NotNull final PsiLanguageInjectionHost host,
                                 @NotNull final InjectedLanguagePlaces places) {
    final boolean isSelectQuery = host.getText().trim().toUpperCase().startsWith("SELECT");
    final boolean isDataSetFile = host.getContainingFile().getText().startsWith("<dataset>");
    if (isDataSetFile && isSelectQuery) {
        final Language language = Language.findLanguageByID("SQL");
        if (language != null) {
            try {
                places.addPlace(language, TextRange.from(0, host.getTextLength()), null, null);
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }
}
 
Example #10
Source File: BladeInjectTypeProvider.java    From idea-php-laravel-plugin with MIT License 6 votes vote down vote up
/**
 *  Resolve PHP language injection in Blade
 *
 *  @see BladeVariableTypeProvider#getHostPhpFileForInjectedIfExists
 */
@Nullable
private static BladeFileImpl getHostBladeFileForInjectionIfExists(PsiElement element) {
    PsiFile file = element.getContainingFile();
    InjectedLanguageManager injectedLanguageManager = InjectedLanguageManager.getInstance(element.getProject());
    if (injectedLanguageManager.isInjectedFragment(file)) {
        PsiLanguageInjectionHost host = injectedLanguageManager.getInjectionHost(element);
        if (host instanceof BladePsiLanguageInjectionHost && host.isValidHost()) {
            PsiFile bladeFile = host.getContainingFile();
            if (bladeFile instanceof BladeFileImpl) {
                return (BladeFileImpl) bladeFile;
            }
        }
    }

    return null;
}
 
Example #11
Source File: ShaderLabCGCompletionContributor.java    From consulo-unity3d with Apache License 2.0 6 votes vote down vote up
public ShaderLabCGCompletionContributor()
{
	extend(CompletionType.BASIC, StandardPatterns.psiElement().withLanguage(CGLanguage.INSTANCE), new CompletionProvider()
	{
		@RequiredReadAction
		@Override
		public void addCompletions(@Nonnull CompletionParameters parameters, ProcessingContext context, @Nonnull final CompletionResultSet result)
		{
			Place shreds = InjectedLanguageUtil.getShreds(parameters.getOriginalFile());

			for(PsiLanguageInjectionHost.Shred shred : shreds)
			{
				PsiLanguageInjectionHost host = shred.getHost();
				if(host instanceof ShaderCGScript)
				{
					ShaderLabFile containingFile = (ShaderLabFile) host.getContainingFile();
					ShaderReference.consumeProperties(containingFile, result::addElement);
				}
			}
		}
	});
}
 
Example #12
Source File: FluidInjector.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
@Override
public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull PsiElement context) {
    if (context.getLanguage() == XMLLanguage.INSTANCE) return;
    final Project project = context.getProject();
    if (!FluidIndexUtil.hasFluid(project)) return;

    final PsiElement parent = context.getParent();
    if (context instanceof XmlAttributeValueImpl && parent instanceof XmlAttribute) {
        final int length = context.getTextLength();
        final String name = ((XmlAttribute) parent).getName();

        if (isInjectableAttribute(project, length, name)) {
            registrar
                .startInjecting(FluidLanguage.INSTANCE)
                .addPlace(null, null, (PsiLanguageInjectionHost) context, ElementManipulators.getValueTextRange(context))
                .doneInjecting();
        }
    }
}
 
Example #13
Source File: CSharpConstantExpressionImpl.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
@RequiredReadAction
public LiteralTextEscaper<? extends PsiLanguageInjectionHost> createLiteralTextEscaper()
{
	IElementType elementType = getLiteralType();
	if(elementType == CSharpTokens.STRING_LITERAL)
	{
		return new CSharpStringLiteralEscaper<>(this);
	}
	else if(elementType == CSharpTokens.VERBATIM_STRING_LITERAL)
	{
		return LiteralTextEscaper.createSimple(this);
	}
	throw new IllegalArgumentException("Unknown " + elementType);
}
 
Example #14
Source File: BladeDirectiveReferences.java    From idea-php-laravel-plugin with MIT License 6 votes vote down vote up
@NotNull
@Override
public Collection<PsiElement> getPsiTargets(StringLiteralExpression element) {

    PsiLanguageInjectionHost host = InjectedLanguageManager.getInstance(getProject()).getInjectionHost(getElement());
    if (!(host instanceof BladePsiLanguageInjectionHost)) {
        return Collections.emptyList();
    }

    final String sectionNameSource = element.getContents();
    if(StringUtils.isBlank(sectionNameSource)) {
        return Collections.emptyList();
    }

    final Set<PsiElement> uniqueSet = new HashSet<>();
    BladeTemplateUtil.visitUpPath(host.getContainingFile(), 10, parameter -> {
        if(sectionNameSource.equalsIgnoreCase(parameter.getContent())) {
            uniqueSet.add(parameter.getPsiElement());
        }
    }, this.visitElements);

    return uniqueSet;
}
 
Example #15
Source File: DocumentWindowImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isValid() {
  Place shreds;
  synchronized (myLock) {
    shreds = myShreds; // assumption: myShreds list is immutable
  }
  // can grab PsiLock in SmartPsiPointer.restore()
  // will check the 0th element manually (to avoid getting .getHost() twice)
  for (int i = 1; i < shreds.size(); i++) {
    PsiLanguageInjectionHost.Shred shred = shreds.get(i);
    if (!shred.isValid()) return false;
  }

  PsiLanguageInjectionHost.Shred firstShred = shreds.get(0);
  PsiLanguageInjectionHost host = firstShred.getHost();
  if (host == null || firstShred.getHostRangeMarker() == null) return false;
  VirtualFile virtualFile = FileDocumentManager.getInstance().getFile(this);
  return virtualFile != null && ((PsiManagerEx)host.getManager()).getFileManager().findCachedViewProvider(virtualFile) != null;
}
 
Example #16
Source File: PlaceInfo.java    From consulo with Apache License 2.0 5 votes vote down vote up
PlaceInfo(@Nonnull String prefix, @Nonnull String suffix, @Nonnull PsiLanguageInjectionHost host, @Nonnull TextRange registeredRangeInsideHost) {
  this.prefix = prefix;
  this.suffix = suffix;
  this.host = host;
  this.registeredRangeInsideHost = registeredRangeInsideHost;
  myEscaper = host.createLiteralTextEscaper();
  myHostText = host.getText();
}
 
Example #17
Source File: MuleBreakpointType.java    From mule-intellij-plugins with Apache License 2.0 5 votes vote down vote up
@Override
public boolean canPutAt(@NotNull VirtualFile file, int line, @NotNull Project project)
{
    final Document document = FileDocumentManager.getInstance().getDocument(file);
    if (document != null)
    {
        final PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document);
        if (psiFile != null)
        {
            final boolean isMuleAndXml = MuleConfigUtils.isMuleFile(psiFile);
            if (isMuleAndXml)
            {
                final XmlTag xmlTagAt = MuleConfigUtils.getXmlTagAt(project, XDebuggerUtil.getInstance().createPosition(file, line));
                if (xmlTagAt != null)
                {
                    return MuleConfigUtils.getMuleElementTypeFromXmlElement(xmlTagAt) == MuleElementType.MESSAGE_PROCESSOR;
                }
                else
                {
                    final PsiElement firstWeaveElement = WeavePsiUtils.getFirstWeaveElement(project, document, line);
                    if (firstWeaveElement != null)
                    {
                        PsiLanguageInjectionHost parent = PsiTreeUtil.getParentOfType(firstWeaveElement, PsiLanguageInjectionHost.class);
                        if (parent != null)
                        {
                            final PsiElement elementInInjected = InjectedLanguageUtil.findElementInInjected(parent, line);
                            final Language language = elementInInjected.getLanguage();
                            return language == WeaveLanguage.getInstance();
                        }
                    }
                }
            }
        }
    }
    return false;
}
 
Example #18
Source File: MuleLanguageInjector.java    From mule-intellij-plugins with Apache License 2.0 5 votes vote down vote up
private void injectLanguage(@NotNull PsiLanguageInjectionHost host, @NotNull InjectedLanguagePlaces injectedLanguagePlaces, String scriptingName) {
    final Language requiredLanguage = Language.findLanguageByID(scriptingName);
    if (requiredLanguage != null) {
        final TextRange range = TextRange.from(0, host.getTextRange().getLength());
        injectedLanguagePlaces.addPlace(requiredLanguage, range, null, null);
    }
}
 
Example #19
Source File: MuleLanguageInjector.java    From mule-intellij-plugins with Apache License 2.0 5 votes vote down vote up
@Override
public void getLanguagesToInject(@NotNull PsiLanguageInjectionHost host,
                                 @NotNull InjectedLanguagePlaces injectedLanguagePlaces) {
    if (MuleConfigUtils.isMuleFile(host.getContainingFile())) {
        if (host instanceof XmlAttributeValue) {
            // Try to inject a language, somewhat abusing the lazy evaluation of predicates :(
            for (Pair<String, String> language : languages) {
                if (tryInjectLanguage(language.getFirst(), language.getSecond(), host, injectedLanguagePlaces)) {
                    break;
                }
            }
        } else if (host instanceof XmlText) {
            final XmlTag tag = ((XmlText) host).getParentTag();
            if (tag != null) {
                final QName tagName = MuleConfigUtils.getQName(tag);
                if (tagName.equals(globalFunctions) || tagName.equals(expressionComponent) || tagName.equals(expressionTransformer)) {
                    final String scriptingName = MelLanguage.MEL_LANGUAGE_ID;
                    injectLanguage(host, injectedLanguagePlaces, scriptingName);
                } else if (tagName.equals(scriptingScript)) {
                    final String engine = tag.getAttributeValue("engine");
                    if (engine != null) {
                        injectLanguage(host, injectedLanguagePlaces, StringUtil.capitalize(engine));
                    }
                } else if (tagName.equals(dwSetPayload) || tagName.equals(dwSetProperty) || tagName.equals(dwSetVariable) || tagName.equals(dwSetSessionVar)) {
                    injectLanguage(host, injectedLanguagePlaces, WEAVE_LANGUAGE_ID);
                }
            }
        }
    }
}
 
Example #20
Source File: MuleLanguageInjector.java    From mule-intellij-plugins with Apache License 2.0 5 votes vote down vote up
private boolean tryInjectLanguage(@NotNull String langPrefix,
                                  @NotNull String languageId,
                                  @NotNull PsiLanguageInjectionHost host,
                                  @NotNull InjectedLanguagePlaces injectedLanguagePlaces) {
    if (isExpectedLocalName(langPrefix, host)) {
        injectLanguage(langPrefix, languageId, host, injectedLanguagePlaces);
        return true;
    }
    return false;
}
 
Example #21
Source File: PsiRawBody.java    From reasonml-idea-plugin with MIT License 5 votes vote down vote up
@NotNull
@Override
public LiteralTextEscaper<? extends PsiLanguageInjectionHost> createLiteralTextEscaper() {
    return new JSStringLiteralEscaper<PsiLanguageInjectionHost>(this) {
        @Override
        protected boolean isRegExpLiteral() {
            return false;
        }
    };
}
 
Example #22
Source File: RegexLanguageInjector.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
@Override
public void getLanguagesToInject(@NotNull PsiLanguageInjectionHost host, @NotNull InjectedLanguagePlaces injectionPlacesRegistrar) {
  if (host instanceof HaxeRegularExpression) {
    final String text = host.getText();
    final TextRange textRange = new TextRange(text.indexOf('/') + 1, text.lastIndexOf('/'));
    injectionPlacesRegistrar.addPlace(RegExpLanguage.INSTANCE, textRange, null, null);
  }
}
 
Example #23
Source File: HaxeRegularExpressionImpl.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
@Override
public PsiLanguageInjectionHost updateText(@NotNull String text) {
  ASTNode node = getNode();
  ASTNode parent = node.getTreeParent();
  final HaxeFieldDeclaration fieldDeclaration = HaxeElementGenerator.createVarDeclaration(getProject(), "a=" + text);
  final HaxeVarInit varInit = fieldDeclaration.getVarInit();
  final ASTNode outerNode = varInit == null ? null : varInit.getNode();
  assert outerNode != null;
  parent.replaceChild(node, outerNode);

  return (PsiLanguageInjectionHost)outerNode.getPsi();
}
 
Example #24
Source File: DocumentWindowImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * @param hPos
 * @return null means we were unable to calculate
 */
@Nullable
LogicalPosition hostToInjectedInVirtualSpace(@Nonnull LogicalPosition hPos) {
  // beware the virtual space
  int hLineStartOffset = hPos.line >= myDelegate.getLineCount() ? myDelegate.getTextLength() : myDelegate.getLineStartOffset(hPos.line);
  int iLineStartOffset = hostToInjected(hLineStartOffset);
  int iLine = getLineNumber(iLineStartOffset);

  synchronized (myLock) {
    for (int i = myShreds.size() - 1; i >= 0; i--) {
      PsiLanguageInjectionHost.Shred shred = myShreds.get(i);
      if (!shred.isValid()) continue;
      Segment hostRangeMarker = shred.getHostRangeMarker();
      if (hostRangeMarker == null) continue;
      int hShredEndOffset = hostRangeMarker.getEndOffset();
      int hShredStartOffset = hostRangeMarker.getStartOffset();

      int hShredStartLine = myDelegate.getLineNumber(hShredStartOffset);
      int hShredEndLine = myDelegate.getLineNumber(hShredEndOffset);

      if (hShredStartLine <= hPos.line && hPos.line <= hShredEndLine) {
        int hColumnOfShredEnd = hShredEndOffset - hLineStartOffset;
        int iColumnOfShredEnd = hostToInjected(hShredEndOffset) - iLineStartOffset;
        int iColumn = iColumnOfShredEnd + hPos.column - hColumnOfShredEnd;
        return new LogicalPosition(iLine, iColumn);
      }
    }
  }

  return null;
}
 
Example #25
Source File: BashEnhancedLiteralTextEscaperTest.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnescpaed() throws Exception {
    BashWordImpl psiElement = (BashWordImpl) BashPsiElementFactory.createWord(myProject, "$'a'");
    Assert.assertNotNull(psiElement);

    LiteralTextEscaper<? extends PsiLanguageInjectionHost> textEscaper = psiElement.createLiteralTextEscaper();

    StringBuilder content = new StringBuilder();
    TextRange range = psiElement.getTextContentRange();
    textEscaper.decode(range, content);
    Assert.assertEquals("a", content.toString());

    //check the offsets
    Assert.assertEquals(2, textEscaper.getOffsetInHost(0, range));
}
 
Example #26
Source File: CSharpConstantExpressionImpl.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Override
@RequiredReadAction
public PsiLanguageInjectionHost updateText(@Nonnull String s)
{
	LeafPsiElement first = (LeafPsiElement) getFirstChild();
	first.replaceWithText(s);
	return this;
}
 
Example #27
Source File: BashEnhancedLiteralTextEscaperTest.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
@NotNull
private StringBuilder unescapeContent(String source) {
    BashWordImpl psiElement = (BashWordImpl) BashPsiElementFactory.createWord(myProject, source);

    LiteralTextEscaper<? extends PsiLanguageInjectionHost> textEscaper = psiElement.createLiteralTextEscaper();

    StringBuilder content = new StringBuilder();
    TextRange range = psiElement.getTextContentRange();
    textEscaper.decode(range, content);
    return content;
}
 
Example #28
Source File: PsiRawBody.java    From reasonml-idea-plugin with MIT License 5 votes vote down vote up
@NotNull
@Override
public PsiLanguageInjectionHost updateText(@NotNull String text) {
    ASTNode valueNode = getNode().getFirstChildNode();
    assert valueNode instanceof LeafElement;

    ((LeafElement) valueNode).replaceWithText(text);
    return this;
}
 
Example #29
Source File: Place.java    From consulo with Apache License 2.0 5 votes vote down vote up
boolean isValid() {
  for (PsiLanguageInjectionHost.Shred shred : this) {
    if (!shred.isValid()) {
      return false;
    }
  }
  return true;
}
 
Example #30
Source File: BladeDirectiveReferences.java    From idea-php-laravel-plugin with MIT License 5 votes vote down vote up
@NotNull
@Override
public Collection<LookupElement> getLookupElements() {

    final List<LookupElement> lookupElementList = new ArrayList<>();

    PsiLanguageInjectionHost host = InjectedLanguageManager.getInstance(getProject()).getInjectionHost(getElement());
    if (!(host instanceof BladePsiLanguageInjectionHost)) {
        return Collections.emptyList();
    }

    final Set<String> uniqueSet = new HashSet<>();
    BladeTemplateUtil.visitUpPath(host.getContainingFile(), 10, parameter -> {
        if (!uniqueSet.contains(parameter.getContent())) {
            uniqueSet.add(parameter.getContent());

            LookupElementBuilder lookupElement = LookupElementBuilder.create(parameter.getContent()).withIcon(LaravelIcons.LARAVEL);

            for(String templateName: BladeTemplateUtil.resolveTemplateName(parameter.getPsiElement().getContainingFile())) {

                lookupElement = lookupElement.withTypeText(templateName, true);

                if(parameter.getElementType() == BladeTokenTypes.SECTION_DIRECTIVE) {
                    lookupElement = lookupElement.withTailText("(section)", true);
                } else if(parameter.getElementType() == BladeTokenTypes.YIELD_DIRECTIVE) {
                    lookupElement = lookupElement.withTailText("(yield)", true);
                } else if(parameter.getElementType() == BladeTokenTypes.STACK_DIRECTIVE) {
                    lookupElement = lookupElement.withTailText("(stack)", true);
                }

                lookupElementList.add(lookupElement);
            }
        }
    }, this.visitElements);

    return lookupElementList;
}