예제 #1
0
  public static Interval getSourceInterval(@NonNull ParseTree context) {
    Parameters.notNull("context", context);

    if (context instanceof TerminalNode) {
      TerminalNode terminalNode = (TerminalNode) context;
      Token token = terminalNode.getSymbol();
      return new Interval(token.getStartIndex(), token.getStopIndex());
    } else if (context instanceof RuleNode) {
      RuleNode ruleNode = (RuleNode) context;
      RuleContext ruleContext = ruleNode.getRuleContext();
      if (ruleContext instanceof ParserRuleContext) {
        return getSourceInterval((ParserRuleContext) ruleContext);
      } else {
        Token startSymbol = getStartSymbol(context);
        Token stopSymbol = getStopSymbol(context);
        if (startSymbol == null || stopSymbol == null) {
          return Interval.INVALID;
        }

        return new Interval(startSymbol.getStartIndex(), stopSymbol.getStopIndex());
      }
    } else {
      return Interval.INVALID;
    }
  }
예제 #2
0
  public static Token getStopSymbol(@NonNull ParseTree context) {
    Parameters.notNull("context", context);

    if (context instanceof ParserRuleContext) {
      return getStopSymbol((ParserRuleContext) context);
    } else if (context instanceof TerminalNode) {
      return ((TerminalNode) context).getSymbol();
    }

    return null;
  }
예제 #3
0
  public static boolean isInContexts(
      @NonNull ParserRuleContext context, boolean allowGaps, @NonNull int... stack) {
    Parameters.notNull("context", context);
    Parameters.notNull("stack", stack);

    if (allowGaps) {
      throw new UnsupportedOperationException("Not implemented yet.");
    }

    ParserRuleContext currentContext = context;
    for (int element : stack) {
      if (currentContext.getRuleIndex() != element) {
        return false;
      }

      currentContext = currentContext.getParent();
      if (currentContext == null) {
        return false;
      }
    }

    return true;
  }
예제 #4
0
  public static Token getStopSymbol(@NonNull ParserRuleContext context) {
    Parameters.notNull("context", context);
    if (context.stop != null) {
      return context.stop;
    }

    for (int i = context.getChildCount() - 1; i >= 0; i--) {
      Token symbol = getStopSymbol(context.getChild(i));
      if (symbol != null) {
        return symbol;
      }
    }

    return context.start;
  }
  public final @NonNull AbstractTokensTaskTaggerSnapshot<TState> translateTo(
      @NonNull DocumentSnapshot targetSnapshot) {
    Parameters.notNull("targetSnapshot", targetSnapshot);
    if (targetSnapshot.equals(snapshot)) {
      return this;
    }

    if (!targetSnapshot.getVersionedDocument().equals(snapshot.getVersionedDocument())) {
      throw new IllegalArgumentException("The target snapshot is not from the same document.");
    }

    if (targetSnapshot.getVersion().getVersionNumber() < snapshot.getVersion().getVersionNumber()) {
      throw new UnsupportedOperationException(
          "The target snapshot is from an earlier version of the document.");
    }

    return translateToImpl(targetSnapshot);
  }
  @Override
  public Iterable<TaggedPositionRegion<TokenTag<Token>>> getTags(
      NormalizedSnapshotPositionRegionCollection regions) {
    Parameters.notNull("regions", regions);
    if (regions.isEmpty()) {
      return Collections.emptyList();
    }

    DocumentSnapshot requestedSnapshot = regions.get(0).getSnapshot();
    if (!requestedSnapshot.equals(snapshot)) {
      throw new UnsupportedOperationException(
          "This tagger snapshot only supports requests from the same document.");
    }

    int startOffset = regions.get(0).getStart().getOffset();
    int endOffset = regions.get(regions.size() - 1).getEnd().getOffset();

    return getHighlights(startOffset, endOffset);
  }
예제 #7
0
  public static Interval getSourceInterval(@NonNull ParserRuleContext context) {
    Parameters.notNull("context", context);
    int startIndex = context.start.getStartIndex();
    Token stopSymbol = getStopSymbol(context);
    if (stopSymbol == null) {
      return new Interval(startIndex, startIndex - 1);
    }

    int stopIndex;
    if (stopSymbol.getType() != Token.EOF) {
      stopIndex = stopSymbol.getStopIndex();
    } else {
      TokenSource tokenSource = context.getStart().getTokenSource();
      CharStream inputStream = tokenSource != null ? tokenSource.getInputStream() : null;
      if (inputStream != null) {
        stopIndex = inputStream.size() - 1;
      } else {
        stopIndex = context.start.getStartIndex() - 1;
      }
    }

    stopIndex = Math.max(stopIndex, startIndex - 1);
    return new Interval(startIndex, stopIndex);
  }
 public static void setInstance(@NonNull final ClassPathPackageAccessor _instance) {
   Parameters.notNull("_instance", _instance); // NOI18N
   instance = _instance;
 }
  protected AbstractTokensTaskTaggerSnapshot(
      @NonNull AbstractTokensTaskTaggerSnapshot<TState> reference,
      @NonNull DocumentSnapshot snapshot) {
    Parameters.notNull("reference", reference);
    Parameters.notNull("snapshot", snapshot);

    if (!snapshot.getVersionedDocument().equals(reference.snapshot.getVersionedDocument())) {
      throw new IllegalArgumentException("The target snapshot is not from the same document.");
    }

    if (snapshot.getVersion().getVersionNumber()
        <= reference.snapshot.getVersion().getVersionNumber()) {
      throw new UnsupportedOperationException(
          "The target snapshot must be a future version of the reference document.");
    }

    this.snapshot = snapshot;
    this.lineStates.addAll(reference.lineStates);
    this.firstDirtyLine = reference.firstDirtyLine;
    this.lastDirtyLine = reference.lastDirtyLine;
    Integer firstChangedLine = null;
    Integer lastChangedLine = null;

    for (DocumentVersion version = reference.snapshot.getVersion();
        version != null && version.getVersionNumber() < snapshot.getVersion().getVersionNumber();
        version = version.getNext()) {
      DocumentSnapshot source = version.getSnapshot();
      DocumentVersion targetVersion = version.getNext();
      assert targetVersion != null;
      DocumentSnapshot target = targetVersion.getSnapshot();
      NormalizedDocumentChangeCollection changes = version.getChanges();
      assert changes != null;
      for (int i = changes.size() - 1; i >= 0; i--) {
        DocumentChange change = changes.get(i);
        int lineCountDelta = change.getLineCountDelta();
        int oldOffset = change.getOldOffset();
        int oldLength = change.getOldLength();
        int newOffset = change.getNewOffset();
        int newLength = change.getNewLength();

        /* processChange */
        int oldStartLine = source.findLineNumber(oldOffset);
        int oldEndLine =
            oldLength == 0 ? oldStartLine : source.findLineNumber(oldOffset + oldLength - 1);
        if (lineCountDelta < 0) {
          lineStates.subList(oldStartLine, oldStartLine + Math.abs(lineCountDelta)).clear();
        } else if (lineCountDelta > 0) {
          TState endLineState = lineStates.get(oldStartLine);
          ArrayList<TState> insertedElements = new ArrayList<>();
          for (int j = 0; j < lineCountDelta; j++) {
            insertedElements.add(endLineState);
          }
          lineStates.addAll(oldStartLine, insertedElements);
        }

        if (lastDirtyLine != null && lastDirtyLine > oldStartLine) {
          lastDirtyLine += lineCountDelta;
        }

        if (lastChangedLine != null && lastChangedLine > oldStartLine) {
          lastChangedLine += lineCountDelta;
        }

        for (int j = oldStartLine; j <= oldEndLine + lineCountDelta; j++) {
          TState state = lineStates.get(i);
          lineStates.set(j, state.createDirtyState());
        }

        firstChangedLine =
            firstChangedLine != null ? Math.min(firstChangedLine, oldStartLine) : oldStartLine;
        lastChangedLine =
            lastChangedLine != null ? Math.max(lastChangedLine, oldEndLine) : oldEndLine;

        /* processAfterChange */
        if (firstChangedLine != null && lastChangedLine != null) {
          int startLine = firstChangedLine;
          int endLineInclusive = Math.min(lastChangedLine, source.getLineCount() - 1);

          firstChangedLine = null;
          lastChangedLine = null;

          /* forceRehighlightLines(startRehighlightLine, endRehighlightLine); */
          firstDirtyLine = firstDirtyLine != null ? Math.min(firstDirtyLine, startLine) : startLine;
          lastDirtyLine =
              lastDirtyLine != null ? Math.max(lastDirtyLine, endLineInclusive) : endLineInclusive;

          //                    int start =
          // currentSnapshot.findLineFromLineNumber(startLine).getStart().getOffset();
          //                    int end = (endLineInclusive == lineStates.size() - 1) ?
          // currentSnapshot.length() : currentSnapshot.findLineFromLineNumber(endLineInclusive +
          // 1).getStart().getOffset();
          //                    if (FIX_HIGHLIGHTER_UPDATE_BUG) {
          //                        fireHighlightsChange(start, document.getLength());
          //                    } else {
          //                        fireHighlightsChange(start, end);
          //                    }
        }
      }
    }

    firstDirtyLine = Math.min(firstDirtyLine, snapshot.getLineCount() - 1);
    lastDirtyLine = Math.min(lastDirtyLine, snapshot.getLineCount() - 1);
  }
 public AbstractTokensTaskTaggerSnapshot(@NonNull DocumentSnapshot snapshot) {
   Parameters.notNull("snapshot", snapshot);
   this.snapshot = snapshot;
 }
예제 #11
0
  public CompiledModel(DocumentSnapshot snapshot, CompiledFileModel result) {
    Parameters.notNull("result", result);

    this.snapshot = snapshot;
    this.result = result;
  }