Java Code Examples for com.intellij.psi.PsiLanguageInjectionHost#Shred

The following examples show how to use com.intellij.psi.PsiLanguageInjectionHost#Shred . 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: 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 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: 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 4
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 5
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 6
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 7
Source File: DocumentWindowImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public int getTextLength() {
  int length = 0;
  synchronized (myLock) {
    for (PsiLanguageInjectionHost.Shred shred : myShreds) {
      Segment hostRange = shred.getHostRangeMarker();
      if (hostRange == null) continue;
      length += shred.getPrefix().length();
      length += hostRange.getEndOffset() - hostRange.getStartOffset();
      length += shred.getSuffix().length();
    }
  }
  return length;
}
 
Example 8
Source File: DocumentWindowImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public int getLineNumber(int offset) {
  int lineNumber = 0;
  String hostText = myDelegate.getText();
  synchronized (myLock) {
    for (PsiLanguageInjectionHost.Shred shred : myShreds) {
      String prefix = shred.getPrefix();
      String suffix = shred.getSuffix();
      lineNumber += StringUtil.getLineBreakCount(prefix.substring(0, Math.min(offset, prefix.length())));
      if (offset < prefix.length()) {
        return lineNumber;
      }
      offset -= prefix.length();

      Segment currentRange = shred.getHostRangeMarker();
      if (currentRange == null) continue;
      int rangeLength = currentRange.getEndOffset() - currentRange.getStartOffset();
      CharSequence rangeText = hostText.subSequence(currentRange.getStartOffset(), currentRange.getEndOffset());

      lineNumber += StringUtil.getLineBreakCount(rangeText.subSequence(0, Math.min(offset, rangeLength)));
      if (offset < rangeLength) {
        return lineNumber;
      }
      offset -= rangeLength;

      lineNumber += StringUtil.getLineBreakCount(suffix.substring(0, Math.min(offset, suffix.length())));
      if (offset < suffix.length()) {
        return lineNumber;
      }

      offset -= suffix.length();
    }
  }
  lineNumber = getLineCount() - 1;
  return lineNumber < 0 ? 0 : lineNumber;
}
 
Example 9
Source File: DocumentWindowImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public TextRange getHostRange(int hostOffset) {
  synchronized (myLock) {
    for (PsiLanguageInjectionHost.Shred shred : myShreds) {
      Segment currentRange = shred.getHostRangeMarker();
      if (currentRange == null) continue;
      TextRange textRange = ProperTextRange.create(currentRange);
      if (textRange.grown(1).contains(hostOffset)) return textRange;
    }
  }
  return null;
}
 
Example 10
Source File: DocumentWindowImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
public Segment[] getHostRanges() {
  synchronized (myLock) {
    List<Segment> markers = new ArrayList<>(myShreds.size());
    for (PsiLanguageInjectionHost.Shred shred : myShreds) {
      Segment hostMarker = shred.getHostRangeMarker();
      if (hostMarker != null) {
        markers.add(hostMarker);
      }
    }
    return markers.isEmpty() ? Segment.EMPTY_ARRAY : markers.toArray(Segment.EMPTY_ARRAY);
  }
}
 
Example 11
Source File: DocumentWindowImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean containsRange(int hostStart, int hostEnd) {
  synchronized (myLock) {
    ProperTextRange query = new ProperTextRange(hostStart, hostEnd);
    for (PsiLanguageInjectionHost.Shred shred : myShreds) {
      Segment hostRange = shred.getHostRangeMarker();
      if (hostRange == null) continue;
      TextRange textRange = ProperTextRange.create(hostRange);
      if (textRange.contains(query)) return true;
    }
    return false;
  }
}
 
Example 12
Source File: DocumentWindowImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
private String getRangeText(@Nonnull String hostText, int hostNum) {
  synchronized (myLock) {
    PsiLanguageInjectionHost.Shred shred = myShreds.get(hostNum);
    Segment hostRangeMarker = shred.getHostRangeMarker();
    return shred.getPrefix() + (hostRangeMarker == null ? "" : hostText.substring(hostRangeMarker.getStartOffset(), hostRangeMarker.getEndOffset())) + shred.getSuffix();
  }
}
 
Example 13
Source File: GraphQLInjectedLanguageBlockBuilder.java    From js-graphql-intellij-plugin with MIT License 4 votes vote down vote up
private List<PsiLanguageInjectionHost.Shred> mergePlacesIntoOne(List<PsiLanguageInjectionHost.Shred> places) {

        final PsiLanguageInjectionHost.Shred mergedShred = new PsiLanguageInjectionHost.Shred() {

            private PsiLanguageInjectionHost.Shred getFirst() {
                return places.get(0);
            }

            private PsiLanguageInjectionHost.Shred getLast() {
                return places.get(places.size()-1);
            }

            @Nullable
            @Override
            public Segment getHostRangeMarker() {
                if(getFirst().getHostRangeMarker() != null && getLast().getHostRangeMarker() != null) {
                    return new Segment() {
                        @Override
                        public int getStartOffset() {
                            return getFirst().getHostRangeMarker().getStartOffset();
                        }

                        @Override
                        public int getEndOffset() {
                            return getLast().getHostRangeMarker().getEndOffset();
                        }
                    };
                }
                return null;
            }

            @NotNull
            @Override
            public TextRange getRangeInsideHost() {
                return TextRange.create(getFirst().getRangeInsideHost().getStartOffset(), getLast().getRangeInsideHost().getEndOffset());
            }

            @NotNull
            @Override
            public TextRange getRange() {
                return TextRange.create(getFirst().getRange().getStartOffset(), getLast().getRange().getEndOffset());
            }

            @Override
            public boolean isValid() {
                for (PsiLanguageInjectionHost.Shred place : places) {
                    if(!place.isValid()) {
                        return false;
                    }
                }
                return true;
            }

            @Override
            public void dispose() {
                places.forEach(PsiLanguageInjectionHost.Shred::dispose);
            }

            @Nullable
            @Override
            public PsiLanguageInjectionHost getHost() {
                return getFirst().getHost();
            }

            @NotNull
            @Override
            public String getPrefix() {
                return getFirst().getPrefix();
            }

            @NotNull
            @Override
            public String getSuffix() {
                return getLast().getSuffix();
            }
        };
        return Collections.singletonList(mergedShred);
    }
 
Example 14
Source File: Place.java    From consulo with Apache License 2.0 4 votes vote down vote up
void dispose() {
  for (PsiLanguageInjectionHost.Shred shred : this) {
    shred.dispose();
  }
}
 
Example 15
Source File: InjectedReferenceVisitor.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(@Nonnull PsiFile injectedPsi, @Nonnull List<PsiLanguageInjectionHost.Shred> places) {
}
 
Example 16
Source File: InjectedReferenceVisitor.java    From consulo with Apache License 2.0 votes vote down vote up
public abstract void visitInjectedReference(@Nonnull ReferenceInjector injector, @Nonnull List<? extends PsiLanguageInjectionHost.Shred> places);