Java Code Examples for com.intellij.openapi.fileEditor.impl.LoadTextUtil#detectLineSeparator()
The following examples show how to use
com.intellij.openapi.fileEditor.impl.LoadTextUtil#detectLineSeparator() .
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: HaxeCompilerServices.java From intellij-haxe with Apache License 2.0 | 6 votes |
private int recalculateFileOffset(@NotNull PsiFile file, @NotNull PsiElement position, Editor editor) { int offset = position.getTextOffset();; VirtualFile virtualFile = file.getVirtualFile(); if (null == virtualFile) { // In memory file, the position doesn't change. return offset; } // Get the separator, checking the file if we don't know yet. May still return null. String separator = LoadTextUtil.detectLineSeparator(virtualFile, true); // IntelliJ IDEA normalizes file line endings, so if file line endings is // CRLF - then we have to shift an offset so Haxe compiler could get proper offset if (LineSeparator.CRLF.getSeparatorString().equals(separator)) { int lineNumber = com.intellij.openapi.util.text.StringUtil.offsetToLineNumber(editor.getDocument().getText(), offset); offset += lineNumber; } return offset; }
Example 2
Source File: LineSeparatorPanel.java From consulo with Apache License 2.0 | 5 votes |
@Nonnull @Override protected WidgetState getWidgetState(@Nullable VirtualFile file) { if (file == null) { return WidgetState.HIDDEN; } String lineSeparator = LoadTextUtil.detectLineSeparator(file, true); if (lineSeparator == null) { return WidgetState.HIDDEN; } String toolTipText = String.format("Line Separator: %s", StringUtil.escapeLineBreak(lineSeparator)); String panelText = LineSeparator.fromString(lineSeparator).toString(); return new WidgetState(toolTipText, panelText, true); }
Example 3
Source File: AbstractConvertLineSeparatorsAction.java From consulo with Apache License 2.0 | 5 votes |
public static void changeLineSeparators(@Nonnull final Project project, @Nonnull final VirtualFile virtualFile, @Nonnull final String newSeparator) { FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance(); Document document = fileDocumentManager.getCachedDocument(virtualFile); if (document != null) { fileDocumentManager.saveDocument(document); } String currentSeparator = LoadTextUtil.detectLineSeparator(virtualFile, false); final String commandText; if (StringUtil.isEmpty(currentSeparator)) { commandText = "Changed line separators to " + LineSeparator.fromString(newSeparator); } else { commandText = String.format("Changed line separators from %s to %s", LineSeparator.fromString(currentSeparator), LineSeparator.fromString(newSeparator)); } new WriteCommandAction(project, commandText) { @Override protected void run(@Nonnull Result result) throws Throwable { try { LoadTextUtil.changeLineSeparators(project, virtualFile, newSeparator, this); } catch (IOException e) { LOG.info(e); } } }.execute(); }
Example 4
Source File: InconsistentLineSeparatorsInspection.java From consulo with Apache License 2.0 | 5 votes |
@Nonnull @Override public PsiElementVisitor buildVisitor(@Nonnull final ProblemsHolder holder, boolean isOnTheFly) { return new PsiElementVisitor() { @Override public void visitFile(PsiFile file) { if (!file.getLanguage().equals(file.getViewProvider().getBaseLanguage())) { // There is a possible case that more than a single virtual file/editor contains more than one language (e.g. php and html). // We want to process a virtual file once than, hence, ignore all non-base psi files. return; } final Project project = holder.getProject(); final String projectLineSeparator = CodeStyleFacade.getInstance(project).getLineSeparator(); if (projectLineSeparator == null) { return; } final VirtualFile virtualFile = file.getVirtualFile(); if (virtualFile == null || !AbstractConvertLineSeparatorsAction.shouldProcess(virtualFile, project)) { return; } final String curLineSeparator = LoadTextUtil.detectLineSeparator(virtualFile, true); if (curLineSeparator != null && !curLineSeparator.equals(projectLineSeparator)) { holder.registerProblem( file, "Line separators in the current file (" + StringUtil.escapeStringCharacters(curLineSeparator) + ") " + "differ from the project defaults (" + StringUtil.escapeStringCharacters(projectLineSeparator) + ")", SET_PROJECT_LINE_SEPARATORS); } } }; }
Example 5
Source File: FileDocumentContentImpl.java From consulo with Apache License 2.0 | 4 votes |
@javax.annotation.Nullable private static LineSeparator getSeparator(@Nonnull VirtualFile file) { String s = LoadTextUtil.detectLineSeparator(file, true); if (s == null) return null; return LineSeparator.fromString(s); }