deda8e1479bfb88ee722733f63adc89ae31be27d
[fedora-idea.git] / platform / platform-impl / src / com / intellij / openapi / editor / impl / PersistentRangeMarker.java
blobdeda8e1479bfb88ee722733f63adc89ae31be27d
1 /*
2 * Copyright 2000-2009 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.openapi.editor.impl;
18 import com.intellij.openapi.editor.event.DocumentEvent;
19 import com.intellij.openapi.editor.ex.DocumentEx;
20 import com.intellij.openapi.editor.impl.event.DocumentEventImpl;
22 /**
23 * @author max
25 public class PersistentRangeMarker extends RangeMarkerImpl {
26 private int myStartLine;
27 private int myStartColumn;
28 private int myEndLine;
29 private int myEndColumn;
31 public PersistentRangeMarker(DocumentEx document, int startOffset, int endOffset) {
32 super(document, startOffset, endOffset);
33 storeLinesAndCols();
36 private void storeLinesAndCols() {
37 // document might have been changed already
38 if (getStartOffset() < myDocument.getTextLength()) {
39 myStartLine = myDocument.getLineNumber(getStartOffset());
40 myStartColumn = getStartOffset() - myDocument.getLineStartOffset(myStartLine);
42 if (getEndOffset() < myDocument.getTextLength()) {
43 myEndLine = myDocument.getLineNumber(getEndOffset());
44 myEndColumn = getEndOffset() - myDocument.getLineStartOffset(myEndLine);
48 @Override
49 protected void changedUpdateImpl(DocumentEvent e) {
50 DocumentEventImpl event = (DocumentEventImpl)e;
51 if (event.isWholeTextReplaced()){
52 myStartLine = event.translateLineViaDiffStrict(myStartLine);
53 if (myStartLine < 0 || myStartLine >= getDocument().getLineCount()){
54 invalidate();
56 else{
57 myStart = getDocument().getLineStartOffset(myStartLine) + myStartColumn;
60 myEndLine = event.translateLineViaDiffStrict(myEndLine);
61 if (myEndLine < 0 || myEndLine >= getDocument().getLineCount()){
62 invalidate();
64 else{
65 myEnd = getDocument().getLineStartOffset(myEndLine) + myEndColumn;
67 if (myEnd < myStart) {
68 invalidate();
71 else{
72 super.changedUpdateImpl(e);
73 if (isValid()){
74 storeLinesAndCols();