Search in sources :

Example 1 with IndentOptions

use of com.intellij.psi.codeStyle.CommonCodeStyleSettings.IndentOptions in project intellij-community by JetBrains.

the class IndentOptionsDetectorImpl method getIndentOptions.

@Override
@NotNull
public IndentOptions getIndentOptions() {
    IndentOptions indentOptions = (IndentOptions) CodeStyleSettingsManager.getSettings(myProject).getIndentOptions(myFile.getFileType()).clone();
    IndentOptionsAdjuster adjuster = getIndentOptionsAdjuster();
    if (adjuster != null) {
        adjuster.adjust(indentOptions);
    }
    return indentOptions;
}
Also used : IndentOptions(com.intellij.psi.codeStyle.CommonCodeStyleSettings.IndentOptions) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with IndentOptions

use of com.intellij.psi.codeStyle.CommonCodeStyleSettings.IndentOptions in project intellij-community by JetBrains.

the class DetectAndAdjustIndentOptionsTask method adjustOptions.

private void adjustOptions(IndentOptionsAdjuster adjuster) {
    final PsiFile file = getFile();
    if (file == null)
        return;
    final IndentOptions currentDefault = getDefaultIndentOptions(file, myDocument);
    myOptionsToAdjust.copyFrom(currentDefault);
    adjuster.adjust(myOptionsToAdjust);
    if (myOptionsToAdjust instanceof TimeStampedIndentOptions) {
        TimeStampedIndentOptions cachedInDocument = (TimeStampedIndentOptions) myOptionsToAdjust;
        cachedInDocument.setTimeStamp(myDocument.getModificationStamp());
        cachedInDocument.setOriginalIndentOptionsHash(currentDefault.hashCode());
    }
}
Also used : IndentOptions(com.intellij.psi.codeStyle.CommonCodeStyleSettings.IndentOptions) PsiFile(com.intellij.psi.PsiFile)

Example 3 with IndentOptions

use of com.intellij.psi.codeStyle.CommonCodeStyleSettings.IndentOptions in project intellij-community by JetBrains.

the class DetectableIndentOptionsProvider method getValidCachedIndentOptions.

public IndentOptions getValidCachedIndentOptions(PsiFile file, Document document) {
    IndentOptions options = IndentOptions.retrieveFromAssociatedDocument(file);
    if (options instanceof TimeStampedIndentOptions) {
        final IndentOptions defaultIndentOptions = getDefaultIndentOptions(file, document);
        final TimeStampedIndentOptions cachedInDocument = (TimeStampedIndentOptions) options;
        if (!cachedInDocument.isOutdated(document, defaultIndentOptions)) {
            return cachedInDocument;
        }
    }
    return null;
}
Also used : DetectAndAdjustIndentOptionsTask.getDefaultIndentOptions(com.intellij.psi.codeStyle.DetectAndAdjustIndentOptionsTask.getDefaultIndentOptions) IndentOptions(com.intellij.psi.codeStyle.CommonCodeStyleSettings.IndentOptions)

Example 4 with IndentOptions

use of com.intellij.psi.codeStyle.CommonCodeStyleSettings.IndentOptions in project intellij by bazelbuild.

the class BuildEnterHandler method preprocessEnter.

@Override
public Result preprocessEnter(PsiFile file, Editor editor, Ref<Integer> caretOffset, Ref<Integer> caretAdvance, DataContext dataContext, EditorActionHandler originalHandler) {
    int offset = caretOffset.get();
    if (editor instanceof EditorWindow) {
        file = InjectedLanguageManager.getInstance(file.getProject()).getTopLevelFile(file);
        editor = InjectedLanguageUtil.getTopLevelEditor(editor);
        offset = editor.getCaretModel().getOffset();
    }
    if (!isApplicable(file, dataContext)) {
        return Result.Continue;
    }
    // Previous enter handler's (e.g. EnterBetweenBracesHandler) can introduce a mismatch
    // between the editor's caret model and the offset we've been provided with.
    editor.getCaretModel().moveToOffset(offset);
    Document doc = editor.getDocument();
    PsiDocumentManager.getInstance(file.getProject()).commitDocument(doc);
    CodeStyleSettings currentSettings = CodeStyleSettingsManager.getSettings(file.getProject());
    IndentOptions indentOptions = currentSettings.getIndentOptions(file.getFileType());
    Integer indent = determineIndent(file, editor, offset, indentOptions);
    if (indent == null) {
        return Result.Continue;
    }
    removeTrailingWhitespace(doc, file, offset);
    originalHandler.execute(editor, editor.getCaretModel().getCurrentCaret(), dataContext);
    LogicalPosition position = editor.getCaretModel().getLogicalPosition();
    if (position.column == indent) {
        return Result.Stop;
    }
    if (position.column > indent) {
        // default enter handler has added too many spaces -- remove them
        int excess = position.column - indent;
        doc.deleteString(editor.getCaretModel().getOffset() - excess, editor.getCaretModel().getOffset());
    } else if (position.column < indent) {
        String spaces = StringUtil.repeatSymbol(' ', indent - position.column);
        doc.insertString(editor.getCaretModel().getOffset(), spaces);
    }
    editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(position.line, indent));
    return Result.Stop;
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings) IndentOptions(com.intellij.psi.codeStyle.CommonCodeStyleSettings.IndentOptions) Document(com.intellij.openapi.editor.Document) EditorWindow(com.intellij.injected.editor.EditorWindow)

Example 5 with IndentOptions

use of com.intellij.psi.codeStyle.CommonCodeStyleSettings.IndentOptions in project intellij-community by JetBrains.

the class DetectableIndentOptionsProvider method getIndentOptions.

@Nullable
@Override
public IndentOptions getIndentOptions(@NotNull CodeStyleSettings settings, @NotNull PsiFile file) {
    if (!isEnabled(settings, file)) {
        return null;
    }
    Project project = file.getProject();
    PsiDocumentManager psiManager = PsiDocumentManager.getInstance(project);
    Document document = psiManager.getDocument(file);
    if (document == null) {
        return null;
    }
    IndentOptions options = getValidCachedIndentOptions(file, document);
    if (options != null) {
        return options;
    }
    TimeStampedIndentOptions indentOptions = getDefaultIndentOptions(file, document);
    indentOptions.associateWithDocument(document);
    DetectAndAdjustIndentOptionsTask task = new DetectAndAdjustIndentOptionsTask(project, document, indentOptions, BOUNDED_EXECUTOR);
    task.scheduleInBackgroundForCommittedDocument();
    return indentOptions;
}
Also used : Project(com.intellij.openapi.project.Project) DetectAndAdjustIndentOptionsTask.getDefaultIndentOptions(com.intellij.psi.codeStyle.DetectAndAdjustIndentOptionsTask.getDefaultIndentOptions) IndentOptions(com.intellij.psi.codeStyle.CommonCodeStyleSettings.IndentOptions) Document(com.intellij.openapi.editor.Document) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

IndentOptions (com.intellij.psi.codeStyle.CommonCodeStyleSettings.IndentOptions)5 Document (com.intellij.openapi.editor.Document)2 DetectAndAdjustIndentOptionsTask.getDefaultIndentOptions (com.intellij.psi.codeStyle.DetectAndAdjustIndentOptionsTask.getDefaultIndentOptions)2 EditorWindow (com.intellij.injected.editor.EditorWindow)1 LogicalPosition (com.intellij.openapi.editor.LogicalPosition)1 Project (com.intellij.openapi.project.Project)1 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)1 PsiFile (com.intellij.psi.PsiFile)1 CodeStyleSettings (com.intellij.psi.codeStyle.CodeStyleSettings)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1