/*
 * 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.execution.filters;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;

public final class OpenFileHyperlinkInfo implements FileHyperlinkInfo {
  private final Project myProject;
  private final VirtualFile myFile;
  private final int myDocumentLine;
  private final int myDocumentColumn;

  public OpenFileHyperlinkInfo(@NotNull OpenFileDescriptor descriptor) {
    this(descriptor.getProject(), descriptor.getFile(), descriptor.getLine(), descriptor.getColumn());
  }

  public OpenFileHyperlinkInfo(@NotNull Project project, @NotNull VirtualFile file,
                               int documentLine, int documentColumn) {
    myProject = project;
    myFile = file;
    myDocumentLine = documentLine;
    myDocumentColumn = documentColumn;
  }

  public OpenFileHyperlinkInfo(@NotNull Project project, @NotNull final VirtualFile file, final int line) {
    this(project, file, line, 0);
  }

  public OpenFileDescriptor getDescriptor() {
    int offset = calculateOffset(myFile, myDocumentLine, myDocumentColumn);
    if (offset != -1) {
      return new OpenFileDescriptor(myProject, myFile, offset);
    }
    // although document position != logical position, it seems better than returning 'null'
    return new OpenFileDescriptor(myProject, myFile, myDocumentLine, myDocumentColumn);
  }

  public void navigate(final Project project) {
    ApplicationManager.getApplication().runReadAction(new Runnable() {
      public void run() {
        final VirtualFile file = myFile;
        if (file.isValid()) {
          FileEditorManager.getInstance(project).openTextEditor(getDescriptor(), true);
        }
      }
    });
  }

  private static int calculateOffset(@NotNull final VirtualFile file,
                                     final int documentLine, final int documentColumn) {
    return ApplicationManager.getApplication().runReadAction(new Computable<Integer>() {

      @Override
      public Integer compute() {
        Document document = FileDocumentManager.getInstance().getDocument(file);
        if (document != null) {
          int lineStartOffset = document.getLineStartOffset(documentLine);
          int lineEndOffset = document.getLineEndOffset(documentLine);
          int fixedColumn = Math.min(Math.max(documentColumn, 0), lineEndOffset - lineStartOffset);
          return lineStartOffset + fixedColumn;
        }
        return -1;
      }
    });
  }
}

@@ -3063,24 +3063,140 @@
  != null) %7B%0A
+          int lineCount = document.getLineCount();%0A          if (0 %3C= documentLine && documentLine %3C lineCount) %7B%0A  
           in
@@ -3250,32 +3250,34 @@
 (documentLine);%0A
+  
           int li
@@ -3337,24 +3337,26 @@
 ;%0A          
+  
 int fixedCol
@@ -3439,24 +3439,26 @@
 ;%0A          
+  
 return lineS
@@ -3483,16 +3483,28 @@
 Column;%0A
+          %7D%0A
         

check documentValue for inclusion in [0, document line count)