This repository has been archived by the owner on Apr 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
violation-view.coffee
174 lines (144 loc) · 6.23 KB
/
violation-view.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
_ = require 'lodash'
{$, View, Range, Point} = require 'atom'
ViolationTooltip = require './violation-tooltip'
module.exports =
class ViolationView extends View
@content: ->
@div class: 'violation', =>
@div class: 'violation-arrow'
@div class: 'violation-area'
initialize: (@violation, @lintView) ->
@lintView.append(this)
@editorView = @lintView.editorView
@editor = @editorView.getEditor()
@initializeSubviews()
@initializeStates()
@trackEdit()
@trackCursor()
@showHighlight()
@toggleTooltipWithCursorPosition()
initializeSubviews: ->
@arrow = @find('.violation-arrow')
@arrow.addClass("violation-#{@violation.severity}")
@area = @find('.violation-area')
@area.addClass("violation-#{@violation.severity}")
initializeStates: ->
screenRange = @editor.screenRangeForBufferRange(@violation.bufferRange)
@screenStartPosition = screenRange.start
@screenEndPosition = screenRange.end
@isValid = true
trackEdit: ->
# :persistent -
# Whether to include this marker when serializing the buffer. Defaults to true.
#
# :invalidate -
# Determines the rules by which changes to the buffer *invalidate* the
# marker. Defaults to 'overlap', but can be any of the following:
# * 'never':
# The marker is never marked as invalid. This is a good choice for
# markers representing selections in an editor.
# * 'surround':
# The marker is invalidated by changes that completely surround it.
# * 'overlap':
# The marker is invalidated by changes that surround the start or
# end of the marker. This is the default.
# * 'inside':
# The marker is invalidated by a change that touches the marked
# region in any way. This is the most fragile strategy.
options = { invalidate: 'inside', persistent: false }
@marker = @editor.markScreenRange(@getCurrentScreenRange(), options)
@editor.decorateMarker(@marker, { type: 'gutter', class: "lint-#{@violation.severity}" })
@marker.on 'changed', (event) =>
# Head and Tail: Markers always have a head and sometimes have a tail.
# If you think of a marker as an editor selection, the tail is the part that's stationary
# and the head is the part that moves when the mouse is moved.
# A marker without a tail always reports an empty range at the head position.
# A marker with a head position greater than the tail is in a "normal" orientation.
# If the head precedes the tail the marker is in a "reversed" orientation.
@screenStartPosition = event.newTailScreenPosition
@screenEndPosition = event.newHeadScreenPosition
@isValid = event.isValid
if @isValid
if @isVisibleMarkerChange(event)
# TODO: EditorView::pixelPositionForScreenPosition lies when a line above the marker was
# removed and it was invoked from this marker's "changed" event.
setImmediate =>
@showHighlight()
@toggleTooltipWithCursorPosition()
else
# Defer repositioning views that are currently outside of visibile area of scroll view.
# This is important to avoid UI freeze when so many markers are changed by a single
# modification (e.g. inserting/deleting the first line in the file).
# Hide the views for now, so that the repositioning-pending views won't be shown in the
# visible area of the scroll view.
@hide()
# This should be held by each ViolationView instance. Otherwise it will be called only
# once for all instance events.
@scheduleDeferredShowHighlight ?= _.debounce(@showHighlight, 500)
@scheduleDeferredShowHighlight()
else
@hideHighlight()
@violationTooltip?.hide()
isVisibleMarkerChange: (event) ->
editorFirstVisibleRow = @editorView.getFirstVisibleScreenRow()
editorLastVisibleRow = @editorView.getLastVisibleScreenRow()
[event.oldTailScreenPosition, event.newTailScreenPosition].some (position) ->
editorFirstVisibleRow <= position.row <= editorLastVisibleRow
trackCursor: ->
@subscribe @editor.getCursor(), 'moved', =>
if @isValid
@toggleTooltipWithCursorPosition()
else
@violationTooltip?.hide()
showHighlight: ->
@updateHighlight()
@show()
hideHighlight: ->
@hide()
updateHighlight: ->
startPixelPosition = @editorView.pixelPositionForScreenPosition(@screenStartPosition)
endPixelPosition = @editorView.pixelPositionForScreenPosition(@screenEndPosition)
arrowSize = @editorView.charWidth / 2
verticalOffset = @editorView.lineHeight + Math.floor(arrowSize / 4)
@css
'top': startPixelPosition.top
'left': startPixelPosition.left
'width': @editorView.charWidth - (@editorView.charWidth % 2) # Adjust toolbar tip center
'height': verticalOffset
@arrow.css
'border-right-width': arrowSize
'border-bottom-width': arrowSize
'border-left-width': arrowSize
borderThickness = 1
borderOffset = arrowSize / 2
@area.css
'left': borderOffset # Avoid protruding left edge of the border from the arrow
'width': endPixelPosition.left - startPixelPosition.left - borderOffset
'height': verticalOffset
if @screenEndPosition.column - @screenStartPosition.column > 1
@area.addClass("violation-border")
else
@area.removeClass("violation-border")
toggleTooltipWithCursorPosition: ->
cursorPosition = @editor.getCursor().getScreenPosition()
if cursorPosition.row is @screenStartPosition.row &&
cursorPosition.column is @screenStartPosition.column
# @tooltip conflicts with View's @tooltip function.
@violationTooltip ?= @createViolationTooltip()
@violationTooltip.show()
else
@violationTooltip?.hide()
getCurrentBufferStartPosition: ->
@editor.bufferPositionForScreenPosition(@screenStartPosition)
getCurrentScreenRange: ->
new Range(@screenStartPosition, @screenEndPosition)
beforeRemove: ->
@marker?.destroy()
@violationTooltip?.destroy()
createViolationTooltip: ->
options =
violation: @violation
container: @lintView
selector: @find('.violation-area')
editorView: @editorView
new ViolationTooltip(this, options)