# coding=utf8

import sublime
from .Base import Base
from ...Utils import Debug

class Error(Base):

	points = None
	ts_view = None

	def __init__(self,name,view):
		super(Error, self).__init__(name,view)

	def setup(self, ts_view, files, points):

		self.ts_view = ts_view
		self.window = self.ts_view.window()
		self.files =  files
		self.points = points

	def on_click(self,line):
		if not self.points:
			if self.ts_view: 
				if self.ts_view.window():
					self.ts_view.window().focus_view(self.ts_view)
			return

		if line in self.points:
			(group,index) = self.window.get_view_index(self.ts_view)
			self.window.focus_group(group)
			Debug('errorpanel', "focus group %i" % group)
			view = self.window.open_file(self.files[line])
			Debug('errorpanel', "open file %s" % self.files[line])
			self.open_view(view,*self.points[line])

		self.ts_view.window().focus_view(self.ts_view)
		

	def open_view(self,view,begin,end):
		if view.is_loading():
			sublime.set_timeout(lambda: self.open_view(view,begin,end), 100)
			return
		else:
			a = view.text_point(*begin)
			b = view.text_point(*end) 
			region = sublime.Region(a,b)
			Debug('errorpanel', "focus view to view %i" % view.id())
			self.ts_view.window().focus_view(view)
			Debug('errorpanel', "focus region, begin @pos %i, (%s -> %s)" % (region.begin(), begin, end))
			#view.show(region)
			sel = view.sel()
			sel.clear()
			sel.add(region)
			sel.clear()
			sel.add(region)

@@ -813,16 +813,17 @@
 ew(view,
+ 
 *self.po
@@ -839,60 +839,8 @@
 %5D)%0A%0A
-%09%09self.ts_view.window().focus_view(self.ts_view)%0A%09%09%0A
 %0A%09de
@@ -856,27 +856,30 @@
 ew(self,
+ 
 view,
+ 
 begin,
+ 
 end):%0A%09%09
@@ -1139,35 +1139,32 @@
 w.id())%0A%09%09%09self.
-ts_
 view.window().fo
@@ -1278,17 +1278,16 @@
 nd))%0A%09%09%09
-#
 view.sho
@@ -1287,16 +1287,26 @@
 iew.show
+_at_center
 (region)
@@ -1356,46 +1356,26 @@
 add(
-region)%0A%09%09%09sel.clear()%0A%09%09%09sel.add(region)
+sublime.Region(a,a))%0A
 %0A

fix error with error click and file switch