当前位置: 首页>>代码示例>>Java>>正文


Java CodeStyleManager.adjustLineIndent方法代码示例

本文整理汇总了Java中com.intellij.psi.codeStyle.CodeStyleManager.adjustLineIndent方法的典型用法代码示例。如果您正苦于以下问题:Java CodeStyleManager.adjustLineIndent方法的具体用法?Java CodeStyleManager.adjustLineIndent怎么用?Java CodeStyleManager.adjustLineIndent使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.intellij.psi.codeStyle.CodeStyleManager的用法示例。


在下文中一共展示了CodeStyleManager.adjustLineIndent方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: adjustLineIndent

import com.intellij.psi.codeStyle.CodeStyleManager; //导入方法依赖的package包/类
private static void adjustLineIndent(PsiFile file,
                              Document document,
                              int startOffset, int endOffset, int line, Project project) {
  CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
  if (startOffset == endOffset) {
    int lineStart = document.getLineStartOffset(line);
    if (codeStyleManager.isLineToBeIndented(file, lineStart)) {
      codeStyleManager.adjustLineIndent(file, lineStart);
    }
  } else {
    codeStyleManager.adjustLineIndent(file, new TextRange(DocumentUtil.getLineStartOffset(startOffset, document), endOffset));
  }
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:14,代码来源:AutoIndentLinesHandler.java

示例2: indentEachLine

import com.intellij.psi.codeStyle.CodeStyleManager; //导入方法依赖的package包/类
private static void indentEachLine(Project project, Editor editor, int startOffset, int endOffset) {
  PsiDocumentManager.getInstance(project).commitAllDocuments();
  PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());

  CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
  final CharSequence text = editor.getDocument().getCharsSequence();
  if (startOffset > 0 && endOffset > startOffset + 1 && text.charAt(endOffset - 1) == '\n' && text.charAt(startOffset - 1) == '\n') {
    // There is a possible situation that pasted text ends by a line feed. We don't want to proceed it when a text is
    // pasted at the first line column.
    // Example:
    //    text to paste:
    //'if (true) {
    //'
    //    source:
    // if (true) {
    //     int i = 1;
    //     int j = 1;
    // }
    //
    //
    // We get the following on paste then:
    // if (true) {
    //     if (true) {
    //         int i = 1;
    //     int j = 1;
    // }
    //
    // We don't want line 'int i = 1;' to be indented here.
    endOffset--;
  }
  try {
    codeStyleManager.adjustLineIndent(file, new TextRange(startOffset, endOffset));
  }
  catch (IncorrectOperationException e) {
    LOG.error(e);
  }
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:38,代码来源:PasteHandler.java

示例3: indentLinesIn

import com.intellij.psi.codeStyle.CodeStyleManager; //导入方法依赖的package包/类
private static void indentLinesIn(final Editor editor, final PsiFile file, final Document document, final Project project, RangeMarker range) {
  final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
  int line1 = editor.offsetToLogicalPosition(range.getStartOffset()).line;
  int line2 = editor.offsetToLogicalPosition(range.getEndOffset()).line;

  while (!lineContainsNonSpaces(document, line1) && line1 < line2) line1++;
  while (!lineContainsNonSpaces(document, line2) && line1 < line2) line2--;

  final FileViewProvider provider = file.getViewProvider();
  PsiFile rootToAdjustIndentIn = provider.getPsi(provider.getBaseLanguage());
  codeStyleManager.adjustLineIndent(rootToAdjustIndentIn, new TextRange(document.getLineStartOffset(line1), document.getLineStartOffset(line2)));
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:13,代码来源:MoverWrapper.java

示例4: invoke

import com.intellij.psi.codeStyle.CodeStyleManager; //导入方法依赖的package包/类
@Override
public void invoke(@NotNull final Project project, @NotNull final Editor editor, @NotNull final PsiFile file) {
  if (!CodeInsightUtilBase.prepareEditorForWrite(editor)) return;
  PsiDocumentManager.getInstance(project).commitAllDocuments();

  if (!FileDocumentManager.getInstance().requestWriting(editor.getDocument(), project)) {
    return;
  }

  EmacsProcessingHandler emacsProcessingHandler = LanguageEmacsExtension.INSTANCE.forLanguage(file.getLanguage());
  if (emacsProcessingHandler != null) {
    EmacsProcessingHandler.Result result = emacsProcessingHandler.changeIndent(project, editor, file);
    if (result == EmacsProcessingHandler.Result.STOP) {
      return;
    }
  }

  final Document document = editor.getDocument();
  final int startOffset = editor.getCaretModel().getOffset();
  final int line = editor.offsetToLogicalPosition(startOffset).line;
  final int lineStart = document.getLineStartOffset(line);
  try{
    final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
    final int newPos = codeStyleManager.adjustLineIndent(file, lineStart);
    if (editor.getCaretModel().getOffset() < newPos) {
      editor.getCaretModel().moveToOffset(newPos);
      editor.getSelectionModel().removeSelection();
      editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
    }
  }
  catch(IncorrectOperationException e){
    LOG.error(e);
  }
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:35,代码来源:EmacsStyleIndentAction.java

示例5: createComment

import com.intellij.psi.codeStyle.CodeStyleManager; //导入方法依赖的package包/类
@Nullable
private PsiComment createComment(final CharSequence buffer, final CodeInsightSettings settings)
  throws IncorrectOperationException {
  myDocument.insertString(myOffset, buffer);

  PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
  CodeStyleManager.getInstance(getProject()).adjustLineIndent(myFile, myOffset + buffer.length() - 2);

  PsiComment comment = PsiTreeUtil.getNonStrictParentOfType(myFile.findElementAt(myOffset), PsiComment.class);

  comment = createJavaDocStub(settings, comment, getProject());
  if (comment == null) {
    return null;
  }

  CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(getProject());
  CodeStyleSettings codeStyleSettings = CodeStyleSettingsManager.getSettings(getProject());
  boolean old = codeStyleSettings.ENABLE_JAVADOC_FORMATTING;
  codeStyleSettings.ENABLE_JAVADOC_FORMATTING = false;

  try {
    RangeMarker commentMarker = myDocument.createRangeMarker(comment.getTextRange().getStartOffset(),
                                                             comment.getTextRange().getEndOffset());
    codeStyleManager.reformatNewlyAddedElement(comment.getNode().getTreeParent(), comment.getNode());
    comment = PsiTreeUtil.getNonStrictParentOfType(myFile.findElementAt(commentMarker.getStartOffset()), PsiComment.class);
    commentMarker.dispose();
  }
  finally {
    codeStyleSettings.ENABLE_JAVADOC_FORMATTING = old;
  }
  PsiElement next = comment.getNextSibling();
  if (next == null && comment.getParent().getClass() == comment.getClass()) {
    next = comment.getParent().getNextSibling(); // expanding chameleon comment produces comment under comment
  }
  if (next != null) {
    next = myFile.findElementAt(next.getTextRange().getStartOffset()); // maybe switch to another tree
  }
  if (next != null && (!FormatterUtil.containsWhiteSpacesOnly(next.getNode()) || !next.getText().contains(LINE_SEPARATOR))) {
    int lineBreakOffset = comment.getTextRange().getEndOffset();
    myDocument.insertString(lineBreakOffset, LINE_SEPARATOR);
    PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
    codeStyleManager.adjustLineIndent(myFile, lineBreakOffset + 1);
    comment = PsiTreeUtil.getNonStrictParentOfType(myFile.findElementAt(myOffset), PsiComment.class);
  }
  return comment;
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:47,代码来源:EnterHandler.java

示例6: reformat

import com.intellij.psi.codeStyle.CodeStyleManager; //导入方法依赖的package包/类
private void reformat(RangeMarker rangeMarkerToReformat) {
  final PsiFile file = getPsiFile();
  if (file != null) {
    CodeStyleManager style = CodeStyleManager.getInstance(myProject);
    DumbService.getInstance(myProject).withAlternativeResolveEnabled(new Runnable() {
      @Override
      public void run() {
        for (TemplateOptionalProcessor optionalProcessor : Extensions.getExtensions(TemplateOptionalProcessor.EP_NAME)) {
          optionalProcessor.processText(myProject, myTemplate, myDocument, myTemplateRange, myEditor);
        }
      }
    });
    PsiDocumentManager.getInstance(myProject).doPostponedOperationsAndUnblockDocument(myDocument);
    // for Python, we need to indent the template even if reformatting is enabled, because otherwise indents would be broken
    // and reformat wouldn't be able to fix them
    if (myTemplate.isToIndent()) {
      if (!myTemplateIndented) {
        LOG.assertTrue(myTemplateRange.isValid(), presentTemplate(myTemplate));
        smartIndent(myTemplateRange.getStartOffset(), myTemplateRange.getEndOffset());
        myTemplateIndented = true;
      }
    }
    if (myTemplate.isToReformat()) {
      try {
        int endSegmentNumber = myTemplate.getEndSegmentNumber();
        PsiDocumentManager.getInstance(myProject).commitDocument(myDocument);
        RangeMarker dummyAdjustLineMarkerRange = null;
        int endVarOffset = -1;
        if (endSegmentNumber >= 0) {
          endVarOffset = mySegments.getSegmentStart(endSegmentNumber);
          TextRange range = CodeStyleManagerImpl.insertNewLineIndentMarker(file, myDocument, endVarOffset);
          if (range != null) dummyAdjustLineMarkerRange = myDocument.createRangeMarker(range);
        }
        int reformatStartOffset = myTemplateRange.getStartOffset();
        int reformatEndOffset = myTemplateRange.getEndOffset();
        if (rangeMarkerToReformat != null) {
          reformatStartOffset = rangeMarkerToReformat.getStartOffset();
          reformatEndOffset = rangeMarkerToReformat.getEndOffset();
        }
        if (dummyAdjustLineMarkerRange == null && endVarOffset >= 0) {
          // There is a possible case that indent marker element was not inserted (e.g. because there is no blank line
          // at the target offset). However, we want to reformat white space adjacent to the current template (if any).
          PsiElement whiteSpaceElement = CodeStyleManagerImpl.findWhiteSpaceNode(file, endVarOffset);
          if (whiteSpaceElement != null) {
            TextRange whiteSpaceRange = whiteSpaceElement.getTextRange();
            if (whiteSpaceElement.getContainingFile() != null) {
              // Support injected white space nodes.
              whiteSpaceRange = InjectedLanguageManager.getInstance(file.getProject()).injectedToHost(whiteSpaceElement, whiteSpaceRange);
            }
            reformatStartOffset = Math.min(reformatStartOffset, whiteSpaceRange.getStartOffset());
            reformatEndOffset = Math.max(reformatEndOffset, whiteSpaceRange.getEndOffset());
          }
        }
        style.reformatText(file, reformatStartOffset, reformatEndOffset);
        PsiDocumentManager.getInstance(myProject).commitDocument(myDocument);
        PsiDocumentManager.getInstance(myProject).doPostponedOperationsAndUnblockDocument(myDocument);

        if (dummyAdjustLineMarkerRange != null && dummyAdjustLineMarkerRange.isValid()) {
          //[ven] TODO: [max] correct javadoc reformatting to eliminate isValid() check!!!
          mySegments.replaceSegmentAt(endSegmentNumber, dummyAdjustLineMarkerRange.getStartOffset(), dummyAdjustLineMarkerRange.getEndOffset());
          myDocument.deleteString(dummyAdjustLineMarkerRange.getStartOffset(), dummyAdjustLineMarkerRange.getEndOffset());
          PsiDocumentManager.getInstance(myProject).commitDocument(myDocument);
        }
        if (endSegmentNumber >= 0) {
          final int offset = mySegments.getSegmentStart(endSegmentNumber);
          final int lineStart = myDocument.getLineStartOffset(myDocument.getLineNumber(offset));
          // if $END$ is at line start, put it at correct indentation
          if (myDocument.getCharsSequence().subSequence(lineStart, offset).toString().trim().isEmpty()) {
            final int adjustedOffset = style.adjustLineIndent(file, offset);
            mySegments.replaceSegmentAt(endSegmentNumber, adjustedOffset, adjustedOffset);
          }
        }
      }
      catch (IncorrectOperationException e) {
        LOG.error(e);
      }
    }
  }
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:80,代码来源:TemplateState.java


注:本文中的com.intellij.psi.codeStyle.CodeStyleManager.adjustLineIndent方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。