Java Code Examples for com.intellij.openapi.util.text.StringUtil#lineColToOffset()
The following examples show how to use
com.intellij.openapi.util.text.StringUtil#lineColToOffset() .
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: ESLintExternalAnnotator.java From eslint-plugin with MIT License | 5 votes |
@Override public void apply(@NotNull PsiFile file, ExternalLintAnnotationResult<Result> annotationResult, @NotNull AnnotationHolder holder) { if (annotationResult == null) { return; } InspectionProjectProfileManager inspectionProjectProfileManager = InspectionProjectProfileManager.getInstance(file.getProject()); SeverityRegistrar severityRegistrar = inspectionProjectProfileManager.getSeverityRegistrar(); HighlightDisplayKey inspectionKey = getHighlightDisplayKeyByClass(); EditorColorsScheme colorsScheme = annotationResult.input.colorsScheme; Document document = PsiDocumentManager.getInstance(file.getProject()).getDocument(file); if (document == null) { return; } ESLintProjectComponent component = annotationResult.input.project.getComponent(ESLintProjectComponent.class); for (VerifyMessage warn : annotationResult.result.warns) { HighlightSeverity severity = getHighlightSeverity(warn, component.treatAsWarnings); TextAttributes forcedTextAttributes = JSLinterUtil.getTextAttributes(colorsScheme, severityRegistrar, severity); Annotation annotation = createAnnotation(holder, file, document, warn, severity, forcedTextAttributes, false); if (annotation != null) { int offset = StringUtil.lineColToOffset(document.getText(), warn.line - 1, warn.column); PsiElement lit = PsiUtil.getElementAtOffset(file, offset); BaseActionFix actionFix = Fixes.getFixForRule(warn.ruleId, lit); if (actionFix != null) { annotation.registerFix(actionFix, null, inspectionKey); } annotation.registerFix(new SuppressActionFix(warn.ruleId, lit), null, inspectionKey); annotation.registerFix(new SuppressLineActionFix(warn.ruleId, lit), null, inspectionKey); } } }
Example 2
Source File: HaskellParserUtilBase.java From intellij-haskforce with Apache License 2.0 | 5 votes |
/** * 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 3
Source File: HaskellParserUtilBase.java From intellij-haskforce with Apache License 2.0 | 5 votes |
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 4
Source File: LightPlatformCodeInsightTestCase.java From consulo with Apache License 2.0 | 5 votes |
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 5
Source File: HaskellProblem.java From intellij-haskforce with Apache License 2.0 | 4 votes |
default int getOffsetStart(String fileText) { return StringUtil.lineColToOffset(fileText, getStartLine() - 1, getStartColumn() - 1); }
Example 6
Source File: HLint.java From intellij-haskforce with Apache License 2.0 | 4 votes |
public int getOffsetEnd(int offsetStart, String fileText) { final int offsetEnd = useJson ? StringUtil.lineColToOffset(fileText, endLine - 1, endColumn - 1) : getOffsetEndFallback(offsetStart, fileText); return offsetEnd == -1 ? fileText.length() : offsetEnd; }
Example 7
Source File: GaugeError.java From Intellij-Plugin with Apache License 2.0 | 4 votes |
int getOffset(String text) { return StringUtil.lineColToOffset(text, lineNumber - 1, 0); }
Example 8
Source File: CodeInsightTestFixtureImpl.java From consulo with Apache License 2.0 | 4 votes |
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()); } }