2 * Copyright 2000-2013 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.formatting;
18 import com.intellij.lang.ASTNode;
19 import com.intellij.openapi.application.ApplicationManager;
20 import com.intellij.openapi.command.WriteCommandAction;
21 import com.intellij.openapi.editor.Document;
22 import com.intellij.openapi.editor.impl.DocumentImpl;
23 import com.intellij.openapi.util.TextRange;
24 import com.intellij.psi.formatter.CompositeWhiteSpaceFormattingStrategy;
25 import com.intellij.psi.formatter.StaticSymbolWhiteSpaceDefinitionStrategy;
26 import com.intellij.psi.formatter.WhiteSpaceFormattingStrategy;
27 import org.jetbrains.annotations.NotNull;
29 import java.util.Arrays;
31 public class TestFormattingModel implements FormattingModel, FormattingDocumentModel{
33 private final CompositeWhiteSpaceFormattingStrategy myWhiteSpaceStrategy = new CompositeWhiteSpaceFormattingStrategy(
34 Arrays.<WhiteSpaceFormattingStrategy>asList(
35 new StaticSymbolWhiteSpaceDefinitionStrategy(' ', '\t', '\n')
38 private final Document myDocument;
39 private Block myRootBlock;
41 public TestFormattingModel(String text) {
42 myDocument = new DocumentImpl(text);
45 public TestFormattingModel(final Document document) {
46 myDocument = document;
49 public void setRootBlock(final Block rootBlock) {
50 myRootBlock = rootBlock;
54 public int getLineNumber(int offset) {
55 return myDocument.getLineNumber(offset);
59 public int getLineStartOffset(int line) {
60 return myDocument.getLineStartOffset(line);
64 public TextRange replaceWhiteSpace(final TextRange textRange,
65 final String whiteSpace
67 if (ApplicationManager.getApplication() != null) {
68 WriteCommandAction.runWriteCommandAction(null, new Runnable() {
71 myDocument.replaceString(textRange.getStartOffset(), textRange.getEndOffset(), whiteSpace);
75 myDocument.replaceString(textRange.getStartOffset(), textRange.getEndOffset(), whiteSpace);
78 return new TextRange(textRange.getStartOffset(), textRange.getStartOffset() + whiteSpace.length());
82 public CharSequence getText(final TextRange textRange) {
83 return myDocument.getCharsSequence().subSequence(textRange.getStartOffset(), textRange.getEndOffset());
88 public FormattingDocumentModel getDocumentModel() {
94 public Block getRootBlock() {
99 public void commitChanges() {
103 public int getTextLength() {
104 return myDocument.getTextLength();
109 public Document getDocument() {
114 public TextRange shiftIndentInsideRange(ASTNode node, TextRange range, int indent) {
119 public boolean containsWhiteSpaceSymbolsOnly(int startOffset, int endOffset) {
120 return myWhiteSpaceStrategy.check(myDocument.getCharsSequence(), startOffset, endOffset) >= endOffset;
125 public CharSequence adjustWhiteSpaceIfNecessary(@NotNull CharSequence whiteSpaceText, int startOffset, int endOffset,
126 ASTNode nodeAfter, boolean changedViaPsi) {
127 return whiteSpaceText;
131 //public boolean isWhiteSpaceSymbol(char symbol) {
132 // return containsWhiteSpaceSymbolsOnly(CharBuffer.wrap(new char[] {symbol}), 0, 1);
135 //private boolean containsWhiteSpaceSymbolsOnly(CharSequence text, int startOffset, int endOffset) {
136 // return myWhiteSpaceStrategy.check(myDocument.getCharsSequence(), startOffset, endOffset) >= endOffset;