/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.openapi.vcs.impl;

import com.intellij.openapi.editor.Document;
import com.intellij.openapi.localVcs.UpToDateLineNumberProvider;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vcs.ex.LineStatusTracker;

/**
 * author: lesya
 */
public class UpToDateLineNumberProviderImpl implements UpToDateLineNumberProvider {
  private final Document myDocument;
  private final LineStatusTrackerManagerI myLineStatusTrackerManagerI;

  public UpToDateLineNumberProviderImpl(Document document, Project project) {
    myDocument = document;
    myLineStatusTrackerManagerI = LineStatusTrackerManager.getInstance(project);
  }

  @Override
  public boolean isRangeChanged(final int start, final int end) {
    LineStatusTracker tracker = myLineStatusTrackerManagerI.getLineStatusTracker(myDocument);
    if (tracker == null || !tracker.isOperational()) {
      return false;
    }
    return tracker.isRangeModified(start, end);
  }

  @Override
  public boolean isLineChanged(int currentNumber) {
    LineStatusTracker tracker = myLineStatusTrackerManagerI.getLineStatusTracker(myDocument);
    if (tracker == null || !tracker.isOperational()) {
      return false;
    }
    return tracker.isLineModified(currentNumber);
  }

  @Override
  public int getLineNumber(int currentNumber) {
    LineStatusTracker tracker = myLineStatusTrackerManagerI.getLineStatusTracker(myDocument);
    if (tracker == null || !tracker.isOperational()) {
      return currentNumber;
    }
    return tracker.transferLineToVcs(currentNumber, false);
  }

  @Override
  public int getLineCount() {
    LineStatusTracker tracker = myLineStatusTrackerManagerI.getLineStatusTracker(myDocument);
    if (tracker == null || !tracker.isOperational()) {
      return myDocument.getLineCount();
    }
    return tracker.getVcsDocument().getLineCount();
  }
}

@@ -847,33 +847,51 @@
 er;%0A
-%0A/**%0A * author: lesya%0A */
+import org.jetbrains.annotations.Nullable;%0A
 %0Apub
@@ -1385,121 +1385,45 @@
 r = 
-myLineStatusTrackerManagerI.getLineStatusTracker(myDocument);%0A    if (tracker == null %7C%7C !tracker.isOperational()
+getTracker();%0A    if (tracker == null
 ) %7B%0A
@@ -1440,32 +1440,45 @@
 rn false;%0A    %7D%0A
+    else %7B%0A  
     return track
@@ -1509,16 +1509,22 @@
 , end);%0A
+    %7D%0A
   %7D%0A%0A  @
@@ -1620,121 +1620,45 @@
 r = 
-myLineStatusTrackerManagerI.getLineStatusTracker(myDocument);%0A    if (tracker == null %7C%7C !tracker.isOperational()
+getTracker();%0A    if (tracker == null
 ) %7B%0A
@@ -1675,32 +1675,45 @@
 rn false;%0A    %7D%0A
+    else %7B%0A  
     return track
@@ -1746,16 +1746,22 @@
 umber);%0A
+    %7D%0A
   %7D%0A%0A  @
@@ -1853,121 +1853,45 @@
 r = 
-myLineStatusTrackerManagerI.getLineStatusTracker(myDocument);%0A    if (tracker == null %7C%7C !tracker.isOperational()
+getTracker();%0A    if (tracker == null
 ) %7B%0A
@@ -1916,32 +1916,45 @@
 ntNumber;%0A    %7D%0A
+    else %7B%0A  
     return track
@@ -1997,16 +1997,22 @@
 false);%0A
+    %7D%0A
   %7D%0A%0A  @
@@ -2086,122 +2086,103 @@
 r = 
-myLineStatusTrackerManagerI.getLineStatusTracker(myDocument);%0A    if (tracker == null %7C%7C !tracker.isOperational())
+getTracker();%0A    if (tracker == null) %7B%0A      return myDocument.getLineCount();%0A    %7D%0A    else
  %7B%0A 
@@ -2189,34 +2189,46 @@
      return 
-my
+tracker.getVcs
 Document
 .getLineCoun
@@ -2207,32 +2207,34 @@
 r.getVcsDocument
+()
 .getLineCount();
@@ -2246,56 +2246,230 @@
 %7D%0A  
-  return tracker.getVcsDocument().getLineCount()
+%7D%0A%0A  @Nullable%0A  private LineStatusTracker getTracker() %7B%0A    LineStatusTracker tracker = myLineStatusTrackerManagerI.getLineStatusTracker(myDocument);%0A    return tracker != null && tracker.isOperational() ? tracker : null
 ;%0A  

remove duplication