/
NECModel.class.st
206 lines (174 loc) · 4.08 KB
/
NECModel.class.st
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
"
I'm an abstract class that stores the entries to be completed.
"
Class {
#name : #NECModel,
#superclass : #Object,
#instVars : [
'clazz',
'selectors',
'narrowString',
'entries'
],
#category : #'NECompletion-Model'
}
{ #category : #'instance creation' }
NECModel class >> class: aClass [
| newInstance |
newInstance := self basicNew initialize.
newInstance setClass: aClass.
^ newInstance
]
{ #category : #private }
NECModel >> addSelectors [
self initializeSelectors.
self addToEntries: selectors
]
{ #category : #private }
NECModel >> addToEntries: aCollection [
"This method add some elements to the entries collection. Elements are added first in the entry collection."
| temp |
temp := aCollection
select: [ :each |
each contents
occursInWithEmpty: narrowString
caseSensitive: self isCaseSensitive ].
entries addAllFirst: temp
]
{ #category : #accessing }
NECModel >> at: aNumber [
^ entries at: aNumber
]
{ #category : #accessing }
NECModel >> commonPrefix [
"Longest match among the typed text and all menu entries"
| seed matchLengths prefixLength |
seed := self entries first contents asString.
matchLengths := entries allButFirst
collect: [ :entry | seed charactersExactlyMatching: entry contents asString ]
thenSelect: [ :e | e >= narrowString size ].
prefixLength := matchLengths isEmpty
ifTrue: [ 0 ]
ifFalse: [ matchLengths min ].
^ seed first: prefixLength.
]
{ #category : #action }
NECModel >> completionAt: aNumber [
| entry |
entry := (self at: aNumber) completion separateKeywords.
^ NECPreferences spaceAfterCompletion
ifTrue: [ entry, ' ' ]
ifFalse: [ entry ].
]
{ #category : #accessing }
NECModel >> entries [
^entries
]
{ #category : #accessing }
NECModel >> entriesOfType: aSymbol [
| collection |
collection := entries select: [ :each | each type == aSymbol ].
^ collection collect: [ :each | each contents ]
]
{ #category : #accessing }
NECModel >> entryCount [
^entries size
]
{ #category : #testing }
NECModel >> hasMessage [
^ self message notNil
]
{ #category : #initialization }
NECModel >> initialize [
self reset
]
{ #category : #initialization }
NECModel >> initializeSelectors [
self subclassResponsibility
]
{ #category : #private }
NECModel >> isCaseSensitive [
"if you want eCompletion to be case sensitive or not."
^ NECPreferences caseSensitive
]
{ #category : #testing }
NECModel >> isEmpty [
^ entries isEmpty
]
{ #category : #accessing }
NECModel >> message [
^self isEmpty
ifTrue: ['no completions found']
ifFalse: [nil]
]
{ #category : #accessing }
NECModel >> narrowString [
^ narrowString
]
{ #category : #private }
NECModel >> narrowString: aString [
narrowString := aString
]
{ #category : #action }
NECModel >> narrowWith: aString [
self narrowString: aString.
self resetEntries.
self addSelectors
]
{ #category : #testing }
NECModel >> notEmpty [
^self isEmpty not
]
{ #category : #printing }
NECModel >> printOn: aStream [
super printOn: aStream.
clazz ifNotNil:
[ aStream nextPutAll: ' ('.
clazz printOn: aStream.
aStream nextPutAll: ')' ]
]
{ #category : #private }
NECModel >> reset [
self resetSelectors.
self resetEntries.
narrowString := String new
]
{ #category : #private }
NECModel >> resetEntries [
entries := OrderedCollection new
]
{ #category : #private }
NECModel >> resetSelectors [
selectors := OrderedCollection new: 500.
]
{ #category : #initialization }
NECModel >> setClass: aClass [
clazz := aClass.
self initializeSelectors.
self narrowWith: String new
]
{ #category : #private }
NECModel >> sortSelectors [
selectors sort:
[ :a :b |
| scoreA scoreB |
scoreA := scoreB := 0.
(a contents
beginsWithEmpty: narrowString
caseSensitive: self isCaseSensitive) ifFalse: [ scoreA := 2 ].
(b contents
beginsWithEmpty: narrowString
caseSensitive: self isCaseSensitive) ifFalse: [ scoreB := 2 ].
a contents < b contents
ifTrue: [ scoreB := scoreB + 1 ]
ifFalse: [ scoreA := scoreA + 1 ].
scoreA < scoreB ].
]
{ #category : #accessing }
NECModel >> theClass [
^nil
]
{ #category : #accessing }
NECModel >> title [
^nil
]