Java Code Examples for com.intellij.openapi.util.text.StringUtil#offsetToLineNumber()

The following examples show how to use com.intellij.openapi.util.text.StringUtil#offsetToLineNumber() . 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: ScenarioExecutionProducer.java    From Intellij-Plugin with Apache License 2.0 6 votes vote down vote up
private int getScenarioIdentifier(ConfigurationContext context, PsiFile file) {
    int count = NO_SCENARIOS;
    PsiElement selectedElement = context.getPsiLocation();
    if (selectedElement == null) return NON_SCENARIO_CONTEXT;
    String scenarioHeading = (!selectedElement.getClass().equals(SpecScenarioImpl.class)) ? getScenarioHeading(selectedElement) : selectedElement.getText();
    if (scenarioHeading.equals(""))
        return getNumberOfScenarios(file) == 0 ? NO_SCENARIOS : NON_SCENARIO_CONTEXT;
    for (PsiElement psiElement : file.getChildren()) {
        if (psiElement.getClass().equals(SpecScenarioImpl.class)) {
            count++;
            if (psiElement.getNode().getFirstChildNode().getText().equals(scenarioHeading)) {
                return StringUtil.offsetToLineNumber(psiElement.getContainingFile().getText(), psiElement.getTextOffset()) + 1;
            }
        }
    }
    return count == NO_SCENARIOS ? NO_SCENARIOS : NON_SCENARIO_CONTEXT;
}
 
Example 2
Source File: HaxePsiBuilder.java    From intellij-haxe with Apache License 2.0 6 votes vote down vote up
private void printErrorInfo(int offset, String token, String message) {

    PsiDocumentManager mgr = PsiDocumentManager.getInstance(getProject());
    Document doc = null != mgr ? mgr.getDocument(psiFile) : null;
    int line = null != doc ? doc.getLineNumber(offset) + 1 : StringUtil.offsetToLineNumber(getOriginalText(), offset);

    StringBuilder s = new StringBuilder();
    s.append("Parsing error at ");
    s.append(null != psiFile ? psiFile.getName() : "<text>");
    s.append(", ");
    s.append(line);
    s.append(": '");
    s.append(null != token ? token : "<unknown token>");
    s.append("'.  ");
    s.append(null != message ? message : "<no message>");

    String error = s.toString();
    boolean needToReport;
    synchronized (this) {
      needToReport = reportedErrors.add(error);
    }
    if (needToReport) {
      LOG.debug(error);
    }
  }
 
Example 3
Source File: HaskellParserUtilBase.java    From intellij-haskforce with Apache License 2.0 5 votes vote down vote up
/**
 * Called when the parser gets confused from layout rules.
 *
 * Increases the debt of rbraces in the token remapper in
 * HaskellParserWrapper. When the remapper is in debt it will swallow
 * (=remap them to TokenType.WHITE_SPACE) synthetic rbraces until no debt
 * remains.
 */
public static boolean stateHackMess(@NotNull PsiBuilder builder,  int level) {
    if (!(builder instanceof Builder)) return false;
    PsiParser wrapper = ((Builder) builder).parser;
    if (!(wrapper instanceof HaskellParserWrapper)) return false;

    // IElementType tok = builder.getTokenType();
    int offs = builder.getCurrentOffset();
    int line = StringUtil.offsetToLineNumber(builder.getOriginalText(), offs);
    int lineStart = StringUtil.lineColToOffset(builder.getOriginalText(), line, 1);
    Pair<Integer, Integer> p = ((HaskellParserWrapper) wrapper).debtPoints.get(offs);
    if (p != null && p.getSecond() == 0 && !((HaskellParserWrapper) wrapper).regressed) {
        ((HaskellParserWrapper) wrapper).maxRbraceDebt = ((HaskellParserWrapper) wrapper).debtPoints.get(offs).getFirst();
        ((HaskellParserWrapper) wrapper).rbraceDebt = ((HaskellParserWrapper) wrapper).rbraceDebt - ((HaskellParserWrapper) wrapper).maxRbraceDebt;
        ((HaskellParserWrapper) wrapper).debtPoints.put(offs, Pair.create(((HaskellParserWrapper) wrapper).maxRbraceDebt, ((HaskellParserWrapper) wrapper).maxRbraceDebt));
    } else if (((HaskellParserWrapper) wrapper).maxRbraceDebt == -1) {
        int numOpen = findBraces(((HaskellParserWrapper) wrapper).lexer.openBraces, offs, line, lineStart);
        ((HaskellParserWrapper) wrapper).maxRbraceDebt = numOpen;
    }
    // System.out.println("Confused at: " + offs + " line " + line + " on token " + tok
    //        + " regressed: " + ((HaskellParserWrapper) wrapper).regressed + " max: "
    //        + ((HaskellParserWrapper) wrapper).maxRbraceDebt);

    boolean ret = ((HaskellParserWrapper) wrapper).increaseRbraceDebt(builder.getCurrentOffset());
    if (((HaskellParserWrapper) wrapper).maxRbraceDebt == 0) ((HaskellParserWrapper) wrapper).maxRbraceDebt = -1;
    return ret;
}
 
Example 4
Source File: HaskellParserUtilBase.java    From intellij-haskforce with Apache License 2.0 5 votes vote down vote up
public static boolean indented(@NotNull PsiBuilder builder, int level, Parser geq) {
    if (!(builder instanceof Builder)) return false;
    PsiParser wrapper = ((Builder) builder).parser;
    if (!(wrapper instanceof HaskellParserWrapper)) return false;
    if (builder.eof()) return false;

    // IElementType currtok = builder.getTokenType();
    Pair<Integer, IElementType> prevtok = previousElem(builder);
    if (prevtok == null) return true;

    int offs = builder.getCurrentOffset();
    int line = StringUtil.offsetToLineNumber(builder.getOriginalText(), offs);
    int prevline = StringUtil.offsetToLineNumber(builder.getOriginalText(), offs + prevtok.getFirst());
    if (prevline == line) return true;

    int thisLineStart = StringUtil.lineColToOffset(builder.getOriginalText(), line, 0);
    int prevLineStart = StringUtil.lineColToOffset(builder.getOriginalText(), prevline, 0);
    // CharSequence lineStuff = builder.getOriginalText().subSequence(thisLineStart, offs);
    CharSequence prevLineStuff = builder.getOriginalText().subSequence(prevLineStart, thisLineStart - 1);
    int indentation = indentationLevel(prevLineStuff);
    int myindentation = offs - thisLineStart;
    // String tokName = builder.getTokenText();

    boolean geqVal = geq.parse(builder, level);
    if (geqVal && myindentation >= indentation ||
            !geqVal && myindentation > indentation) return true;
    return false;
}
 
Example 5
Source File: ExtractConceptHandler.java    From Intellij-Plugin with Apache License 2.0 5 votes vote down vote up
private Api.ExtractConceptResponse makeExtractConceptRequest(List<PsiElement> specSteps, String fileName, String concept, boolean refactorOtherUsages, final PsiFile element) {
    PsiElement firstStep = specSteps.get(0);
    int startLine = StringUtil.offsetToLineNumber(psiFile.getText(), firstStep.getTextOffset()) + 1;
    PsiElement lastStep = specSteps.get(specSteps.size() - 1);
    int endLine = StringUtil.offsetToLineNumber(psiFile.getText(), lastStep.getTextOffset() + lastStep.getTextLength());
    Api.textInfo textInfo = gauge.messages.Api.textInfo.newBuilder().setFileName(psiFile.getVirtualFile().getPath()).setStartingLineNo(startLine).setEndLineNo(endLine).build();
    ExtractConceptRequest request = new ExtractConceptRequest(fileName, concept, refactorOtherUsages, textInfo);
    request.convertToSteps(specSteps, builder.getTableMap());
    return request.makeExtractConceptRequest(element);
}
 
Example 6
Source File: HaxeDebugUtil.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
public static String elementLocation(PsiElement element) {
  if (null == element) {
    return "(unknown file) : 0";
  }

  final PsiFile file = element.getContainingFile();
  final String contents = null != file ? file.getText() : null;
  final int line = null != contents ? StringUtil.offsetToLineNumber(contents, element.getTextOffset()) : 0;

  StringBuilder builder = new StringBuilder(null != file && file.isPhysical() ? file.getName() : "(virtual file)");
  builder.append(" : ");
  builder.append(line);

  return builder.toString();
}
 
Example 7
Source File: LightPlatformCodeInsightTestCase.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void setupCaret(final RangeMarker caretMarker, String fileText) {
  if (caretMarker != null) {
    int caretLine = StringUtil.offsetToLineNumber(fileText, caretMarker.getStartOffset());
    int caretCol = EditorUtil.calcColumnNumber(null, myEditor.getDocument().getText(),
                                               myEditor.getDocument().getLineStartOffset(caretLine), caretMarker.getStartOffset(),
                                               CodeStyleSettingsManager.getSettings(getProject()).getIndentOptions(InternalStdFileTypes.JAVA).TAB_SIZE);
    LogicalPosition pos = new LogicalPosition(caretLine, caretCol);
    myEditor.getCaretModel().moveToLogicalPosition(pos);
  }
}
 
Example 8
Source File: LightPlatformCodeInsightTestCase.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void checkSelection(final RangeMarker selStartMarker, final RangeMarker selEndMarker, String newFileText, String message) {
  if (selStartMarker != null && selEndMarker != null) {
    int selStartLine = StringUtil.offsetToLineNumber(newFileText, selStartMarker.getStartOffset());
    int selStartCol = selStartMarker.getStartOffset() - StringUtil.lineColToOffset(newFileText, selStartLine, 0);

    int selEndLine = StringUtil.offsetToLineNumber(newFileText, selEndMarker.getEndOffset());
    int selEndCol = selEndMarker.getEndOffset() - StringUtil.lineColToOffset(newFileText, selEndLine, 0);

    assertEquals(
        getMessage("selectionStartLine", message),
        selStartLine + 1,
        StringUtil.offsetToLineNumber(newFileText, myEditor.getSelectionModel().getSelectionStart()) + 1);

    assertEquals(
        getMessage("selectionStartCol", message),
        selStartCol + 1,
        myEditor.getSelectionModel().getSelectionStart() -
        StringUtil.lineColToOffset(newFileText, selStartLine, 0) +
                                                                 1);

    assertEquals(
        getMessage("selectionEndLine", message),
        selEndLine + 1,
        StringUtil.offsetToLineNumber(newFileText, myEditor.getSelectionModel().getSelectionEnd()) + 1);

    assertEquals(
        getMessage("selectionEndCol", message),
        selEndCol + 1,
        myEditor.getSelectionModel().getSelectionEnd() - StringUtil.lineColToOffset(newFileText, selEndLine, 0) +
        1);
  }
  else {
    assertTrue(getMessage("must not have selection", message), !myEditor.getSelectionModel().hasSelection());
  }
}
 
Example 9
Source File: LightPlatformCodeInsightTestCase.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void checkCaretPosition(final RangeMarker caretMarker, String newFileText, String message) {
  if (caretMarker != null) {
    int caretLine = StringUtil.offsetToLineNumber(newFileText, caretMarker.getStartOffset());
    //int caretCol = caretMarker.getStartOffset() - StringUtil.lineColToOffset(newFileText, caretLine, 0);
    int caretCol = EditorUtil.calcColumnNumber(null, newFileText,
                                               StringUtil.lineColToOffset(newFileText, caretLine, 0),
                                               caretMarker.getStartOffset(),
                                               CodeStyleSettingsManager.getSettings(getProject()).getIndentOptions(InternalStdFileTypes.JAVA).TAB_SIZE);

    assertEquals(getMessage("caretLine", message), caretLine, myEditor.getCaretModel().getLogicalPosition().line);
    assertEquals(getMessage("caretColumn", message), caretCol, myEditor.getCaretModel().getLogicalPosition().column);
  }
}
 
Example 10
Source File: CodeInsightTestFixtureImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
private void checkResult(final String expectedFile, final boolean stripTrailingSpaces, final SelectionAndCaretMarkupLoader loader, String actualText) {
  assertInitialized();
  Project project = getProject();
  PsiFile file = getFile();
  Editor editor = getEditor();
  if (editor instanceof EditorWindow) {
    editor = ((EditorWindow)editor).getDelegate();
    file = InjectedLanguageUtil.getTopLevelFile(file);
  }

  project.getComponent(PostprocessReformattingAspect.class).doPostponedFormatting();
  if (stripTrailingSpaces) {
    actualText = stripTrailingSpaces(actualText, project);
  }

  PsiDocumentManager.getInstance(project).commitAllDocuments();

  String newFileText1 = loader.newFileText;
  if (stripTrailingSpaces) {
    newFileText1 = stripTrailingSpaces(newFileText1, project);
  }

  actualText = StringUtil.convertLineSeparators(actualText);

  if (!Comparing.equal(newFileText1, actualText)) {
    if (loader.filePath != null) {
      throw new FileComparisonFailure(expectedFile, newFileText1, actualText, loader.filePath);
    }
    else {
      throw new ComparisonFailure(expectedFile, newFileText1, actualText);
    }
  }

  if (loader.caretMarker != null) {
    final int tabSize = CodeStyleSettingsManager.getSettings(getProject()).getIndentOptions(InternalStdFileTypes.JAVA).TAB_SIZE;

    int caretLine = StringUtil.offsetToLineNumber(loader.newFileText, loader.caretMarker.getStartOffset());
    int caretCol = EditorUtil
            .calcColumnNumber(null, loader.newFileText, StringUtil.lineColToOffset(loader.newFileText, caretLine, 0), loader.caretMarker.getStartOffset(),
                              tabSize);

    final int actualLine = editor.getCaretModel().getLogicalPosition().line;
    final int actualCol = editor.getCaretModel().getLogicalPosition().column;
    boolean caretPositionEquals = caretLine == actualLine && caretCol == actualCol;
    Assert.assertTrue("Caret position in " + expectedFile + " differs. Expected " + genCaretPositionPresentation(caretLine, caretCol) +
                      ". Actual " + genCaretPositionPresentation(actualLine, actualCol), caretPositionEquals);
  }

  if (loader.selStartMarker != null && loader.selEndMarker != null) {
    int selStartLine = StringUtil.offsetToLineNumber(loader.newFileText, loader.selStartMarker.getStartOffset());
    int selStartCol = loader.selStartMarker.getStartOffset() - StringUtil.lineColToOffset(loader.newFileText, selStartLine, 0);

    int selEndLine = StringUtil.offsetToLineNumber(loader.newFileText, loader.selEndMarker.getEndOffset());
    int selEndCol = loader.selEndMarker.getEndOffset() - StringUtil.lineColToOffset(loader.newFileText, selEndLine, 0);

    int selectionStart;
    int selectionEnd;
    selectionStart = editor.getSelectionModel().getSelectionStart();
    selectionEnd = editor.getSelectionModel().getSelectionEnd();

    final int selStartLineActual = StringUtil.offsetToLineNumber(loader.newFileText, selectionStart);
    final int selStartColActual = selectionStart - StringUtil.lineColToOffset(loader.newFileText, selStartLineActual, 0);

    final int selEndLineActual = StringUtil.offsetToLineNumber(loader.newFileText, selectionEnd);
    final int selEndColActual = selectionEnd - StringUtil.lineColToOffset(loader.newFileText, selEndLineActual, 0);

    final boolean selectionEquals = selStartCol == selStartColActual &&
                                    selStartLine == selStartLineActual &&
                                    selEndCol == selEndColActual &&
                                    selEndLine == selEndLineActual;
    Assert.assertTrue("selection in " + expectedFile +
                      " differs. Expected " + genSelectionPresentation(selStartLine, selStartCol, selEndLine, selEndCol) +
                      ". Actual " + genSelectionPresentation(selStartLineActual, selStartColActual, selEndLineActual, selEndColActual), selectionEquals);
  }
  else if (editor != null) {
    Assert.assertTrue("has no selection in " + expectedFile, !editor.getSelectionModel().hasSelection());
  }
}