// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.debugger.streams.psi.impl;

import com.intellij.debugger.DebuggerManagerEx;
import com.intellij.debugger.SourcePosition;
import com.intellij.debugger.impl.DebuggerContextImpl;
import com.intellij.debugger.impl.DebuggerUtilsEx;
import com.intellij.debugger.streams.psi.DebuggerPositionResolver;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.xdebugger.XDebugSession;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * @author Vitaliy.Bibaev
 */
public class DebuggerPositionResolverImpl implements DebuggerPositionResolver {
  @Nullable
  @Override
  public PsiElement getNearestElementToBreakpoint(@NotNull XDebugSession session) {
    final Project project = session.getProject();
    final DebuggerContextImpl debuggerContext = DebuggerManagerEx.getInstanceEx(session.getProject()).getContext();
    final SourcePosition position = debuggerContext.getSourcePosition();

    if (position == null) {
      return null;
    }

    final int line = position.getLine();
    final PsiFile psiFile = position.getFile();
    final VirtualFile file = psiFile.getVirtualFile();

    if (line >= 0 && file != null) {
      final Document document = FileDocumentManager.getInstance().getDocument(file);
      if (document != null && line < document.getLineCount()) {
        final int offset = document.getLineStartOffset(line);
        return DebuggerUtilsEx.findElementAt(PsiDocumentManager.getInstance(project).getPsiFile(document), offset);
      }
    }

    return null;
  }
}

@@ -216,438 +216,176 @@
 ger.
-DebuggerManagerEx;%0Aimport com.intellij.debugger.SourcePosition;%0Aimport com.intellij.debugger.impl.DebuggerContextImpl;%0Aimport com.intellij.debugger.impl.DebuggerUtilsEx;%0Aimport com.intellij.debugger.streams.psi.DebuggerPositionResolver;%0Aimport com.intellij.openapi.editor.Document;%0Aimport com.intellij.openapi.fileEditor.FileDocumentManager;%0Aimport com.intellij.openapi.project.Project;%0Aimport com.intellij.openapi.vfs.VirtualFile
+impl.DebuggerUtilsEx;%0Aimport com.intellij.debugger.streams.psi.DebuggerPositionResolver;%0Aimport com.intellij.openapi.vfs.VirtualFile;%0Aimport com.intellij.psi.PsiElement
 ;%0Aim
@@ -409,24 +409,16 @@
 .psi.Psi
-Document
 Manager;
@@ -442,55 +442,31 @@
 lij.
-psi.PsiElement;%0Aimport com.intellij.psi.PsiFile
+xdebugger.XDebugSession
 ;%0Aim
@@ -490,33 +490,35 @@
 .xdebugger.X
-DebugSess
+SourcePosit
 ion;%0Aimport 
@@ -832,174 +832,9 @@
 nal 
-Project project = session.getProject();%0A    final DebuggerContextImpl debuggerContext = DebuggerManagerEx.getInstanceEx(session.getProject()).getContext();%0A    final 
+X
 Sour
@@ -859,33 +859,26 @@
 n = 
-debuggerContext.getSource
+session.getCurrent
 Posi
@@ -881,25 +881,24 @@
 Position();%0A
-%0A
     if (posi
@@ -910,24 +910,16 @@
 == null)
- %7B%0A     
  return 
@@ -928,100 +928,44 @@
 ll;%0A
-    %7D%0A%0A    final int line = position.getLine();%0A    final PsiFile psiFile = position.getFile
+%0A    int offset = position.getOffset
 ();%0A
@@ -998,33 +998,27 @@
  = p
+o
 si
-File.getVirtual
+tion.get
 File();%0A
 %0A   
@@ -1013,25 +1013,24 @@
 File();%0A
-%0A
     if (
 line %3E= 
@@ -1025,250 +1025,52 @@
 if (
-line %3E= 0 && file != null) %7B%0A      final Document document = FileDocumentManager.getInstance().getDocument(file);%0A      if (document != null && line %3C document.getLineCount()) %7B%0A        final int offset = document.getLineStartOffset(line);%0A  
+0 %3C= offset && offset %3C file.getLength()) %7B%0A
     
@@ -1115,16 +1115,8 @@
 (Psi
-Document
 Mana
@@ -1135,36 +1135,43 @@
 nce(
-project).getPsiFile(document
+session.getProject()).findFile(file
 ), o
@@ -1182,16 +1182,8 @@
 t);%0A
-      %7D%0A
     

Simplify search of the nearest psi element to debugger location