-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathViCompletionController.m
717 lines (587 loc) · 20.3 KB
/
ViCompletionController.m
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
/*
* Copyright (c) 2008-2012 Martin Hedenfalk <martin@vicoapp.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#import "NSString-additions.h"
#import "ViCompletionController.h"
#import "ViCommand.h"
#import "ViKeyManager.h"
#import "ViRegexp.h"
#import "ViThemeStore.h"
#include "logging.h"
@implementation ViCompletionController {
BOOL _positionCompletionsBelowPrefix;
}
@synthesize delegate = _delegate;
@synthesize window;
@synthesize completions = _completions;
@synthesize terminatingKey = _terminatingKey;
@synthesize range = _range;
@synthesize filter = _filter;
@synthesize completionView = tableView;
+ (ViCompletionController *)sharedController
{
static ViCompletionController *__sharedController = nil;
if (__sharedController == nil)
__sharedController = [[ViCompletionController alloc] init];
return __sharedController;
}
- (id)init
{
if ((self = [super init])) {
if (![[NSBundle mainBundle] loadNibNamed:@"CompletionWindow" owner:self topLevelObjects:nil]) {
return nil;
}
tableView.keyManager = [ViKeyManager keyManagerWithTarget:self
defaultMap:[ViMap completionMap]];
// ViTheme *theme = [ViThemeStore defaultTheme];
// [tableView setBackgroundColor:[_theme backgroundColor]];
// _matchParagraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
// [_matchParagraphStyle setLineBreakMode:NSLineBreakByTruncatingHead];
}
return self;
}
- (void)updateBoundsWithPrefixWindow:(NSWindow *)prefixWindow
{
NSSize winsz = NSMakeSize(0, 0);
for (ViCompletion *c in _filteredCompletions) {
NSSize sz = [c.title size];
if (sz.width + 50 > winsz.width)
winsz.width = sz.width + 50;
}
DEBUG(@"got %lu completions, row height is %f",
[_filteredCompletions count], [tableView rowHeight]);
NSScreen *screen = [NSScreen mainScreen];
NSSize screenSize = [window convertRectFromScreen:[screen visibleFrame]].size;
/*
We want to be able to show the list (either above or below the current position),
and still have the current line visible. We can do it easily if the constrain the
size of the completion window to half the screen. This means the following must
fit on half the screen:
- The label with the filter.
- The displayed completions.
- The current text line.
Note that all completions will still be available; users will simply need to
scroll.
*/
NSUInteger maxNumberOfRows = (NSUInteger)((screenSize.height / 2)
- label.bounds.size.height
- _prefixWindowRect.size.height)
/ tableView.rowHeight;
NSUInteger numberOfRows = MIN([_filteredCompletions count], maxNumberOfRows);
winsz.height = numberOfRows * ([tableView rowHeight] + 2) + [label bounds].size.height;
/* Set the window size, which is independent of origin. */
NSRect windowFrame = [window frame];
windowFrame.size = winsz;
windowFrame.origin = [self computeWindowOriginForSize:windowFrame.size fromPrefixWindow:prefixWindow];
if (!NSEqualRects(windowFrame, [window frame])) {
DEBUG(@"setting frame %@", NSStringFromRect(frame));
[window setFrame:windowFrame display:[window isVisible]];
}
}
- (void)completionResponse:(NSArray *)array error:(NSError *)error
{
DEBUG(@"got completions: %@, error %@", array, error);
_completions = [[NSMutableArray alloc] initWithArray:array];
NSUInteger ndropped = 0;
for (NSUInteger i = 0; i < [_completions count];) {
id c = [_completions objectAtIndex:i];
if ([c isKindOfClass:[NSString class]]) {
/*
* For strings, do initial prefix filtering here.
*/
if ([_prefix length] > 0 && [c rangeOfString:_prefix options:NSCaseInsensitiveSearch|NSAnchoredSearch].location == NSNotFound)
[_completions removeObjectAtIndex:i];
else {
c = [ViCompletion completionWithContent:c];
[_completions replaceObjectAtIndex:i++ withObject:c];
}
} else if ([c isKindOfClass:[ViCompletion class]]) {
/*
* If prefix length not set (correctly), do initial prefix filtering here.
* Prefix length is updated for all items below.
*/
ViCompletion *cp = c;
if (_prefix && cp.prefixLength != _prefixLength &&
[cp.content rangeOfString:_prefix options:NSCaseInsensitiveSearch|NSAnchoredSearch].location == NSNotFound)
[_completions removeObjectAtIndex:i];
else
++i;
} else {
[_completions removeObjectAtIndex:i];
++ndropped;
}
}
if (ndropped > 0)
INFO(@"dropped %lu invalid completions (expected NSString or ViCompletion objects)", ndropped);
if ([_completions count] == 0) {
if ([window isVisible]) {
[self cancel:nil];
return;
}
} else {
[self updateCompletions];
[self filterCompletions];
if ([_filteredCompletions count] == 0) {
if ([window isVisible]) {
[self cancel:nil];
return;
}
} else if ([_filteredCompletions count] == 1) {
_onlyCompletion = [_filteredCompletions objectAtIndex:0];
}
/* Automatically insert common prefix among all possible completions.
*/
if ([_options rangeOfString:@"p"].location != NSNotFound)
[self complete_partially:nil];
}
}
- (BOOL)chooseFrom:(id<ViCompletionProvider>)aProvider
range:(NSRange)aRange
prefix:(NSString *)aPrefix
prefixWindowRect:(NSRect)prefixRect
forWindow:(NSWindow *)parentWindow
delegate:(id<ViCompletionDelegate>)aDelegate
options:(NSString *)optionString
initialFilter:(NSString *)initialFilter {
_terminatingKey = 0;
[self reset];
_delegate = aDelegate;
_onlyCompletion = nil;
if (initialFilter)
_filter = [initialFilter mutableCopy];
else
_filter = [[NSMutableString alloc] init];
_provider = aProvider;
_range = aRange;
_prefix = aPrefix;
_prefixLength = [aPrefix length];
_options = optionString;
_fuzzySearch = ([_options rangeOfString:@"f"].location != NSNotFound);
// Aggressive means we auto-select a unique suggestion.
_aggressive = [_options rangeOfString:@"?"].location == NSNotFound;
_autocompleting = [_options rangeOfString:@"C"].location != NSNotFound;
_prefixWindowRect = prefixRect;
DEBUG(@"range is %@, with prefix [%@] and [%@] as initial filter, w/options %@",
NSStringFromRange(_range), _prefix, initialFilter, _options);
DEBUG(@"fetching completions for %@ w/options %@", _prefix, _options);
NSError *error = nil;
NSArray *result = nil;
if ([_provider respondsToSelector:@selector(completionsForString:options:error:)])
result = [_provider completionsForString:_prefix options:_options error:&error];
else
result = [_provider completionsForString:_prefix options:_options];
if (error) {
INFO(@"Completion provider %@ returned error %@", _provider, error);
[self reset];
return NO;
}
[self completionResponse:result error:nil];
if (_onlyCompletion && (_aggressive || [_onlyCompletion.content isEqualToString:_prefix])) {
DEBUG(@"returning %@ as only completion", _onlyCompletion);
_range = NSMakeRange(_range.location, _prefixLength + [_filter length]);
[self terminateWithKey:0 completion:_onlyCompletion];
return NO;
}
if ([_completions count] == 0 || [_filteredCompletions count] == 0) {
DEBUG(@"%s", "returning without completions");
[self reset];
return NO;
}
NSInteger initialSelectionIndex = 0;
if (!_positionCompletionsBelowPrefix) {
initialSelectionIndex = [self numberOfRowsInTableView:tableView] - 1;
}
[self updateUIForPrefixWindow:parentWindow];
[parentWindow addChildWindow:window ordered:NSWindowAbove];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(containingWindowDidResignMain:)
name:NSWindowDidResignMainNotification
object:nil];
return YES;
}
- (void)containingWindowDidResignMain:(NSNotification *)aNotification
{
[self terminateWithKey:0 completion:nil];
}
+ (NSString *)commonPrefixInCompletions:(NSArray *)completions
{
if ([completions count] == 0)
return nil;
int opts = NSCaseInsensitiveSearch;
NSString *longestMatch = nil;
ViCompletion *c = [completions objectAtIndex:0];
NSString *firstMatch = c.content;
for (c in completions) {
NSString *s = c.content;
NSString *commonPrefix = [firstMatch commonPrefixWithString:s options:opts];
if (longestMatch == nil || [commonPrefix length] < [longestMatch length])
longestMatch = commonPrefix;
}
return longestMatch;
}
- (void)setCompletions:(NSMutableArray *)newCompletions
{
_filter = [[NSMutableString alloc] init];
_completions = newCompletions;
_filteredCompletions = nil;
[self filterCompletions];
}
- (BOOL)keyManager:(ViKeyManager *)keyManager
evaluateCommand:(ViCommand *)command
{
if (![self respondsToSelector:command.action] ||
(command.motion && ![self respondsToSelector:command.motion.action])) {
return NO;
}
return [command performWithTarget:self];
}
+ (void)appendFilter:(NSString *)string
toPattern:(NSMutableString *)pattern
fuzzyClass:(NSString *)fuzzyClass
{
NSUInteger i;
for (i = 0; i < [string length]; i++) {
unichar c = [string characterAtIndex:i];
if (i != 0)
[pattern appendFormat:@"%@*?", fuzzyClass];
[pattern appendFormat:@"(%s%C)", [ViRegexp needEscape:c] ? "\\" : "", c];
}
}
- (void)filterCompletions
{
if (_fuzzySearch)
[label setStringValue:[NSString stringWithFormat:@"fuzzy filter: %@", _filter]];
else
[label setStringValue:[NSString stringWithFormat:@"prefix filter: %@%@", _prefix, _filter]];
ViRegexp *rx = nil;
if ([_filter length] > 0) {
NSMutableString *pattern = [NSMutableString string];
if (_fuzzySearch) {
[pattern appendFormat:@"^.{%lu}.*", _prefixLength];
[ViCompletionController appendFilter:_filter toPattern:pattern fuzzyClass:@"."];
[pattern appendString:@".*$"];
} else {
[pattern appendFormat:@"^.{%lu}%@", _prefixLength, [ViRegexp escape:_filter]];
}
rx = [ViRegexp regexpWithString:pattern
options:ONIG_OPTION_IGNORECASE];
}
_filteredCompletions = [[NSMutableArray alloc] init];
for (ViCompletion *c in _completions) {
// DEBUG(@"filtering completion %@ on %@", c, rx);
NSString *s = c.content;
if ([s length] < _prefixLength)
continue;
ViRegexpMatch *m = nil;
if (rx == nil || (m = [rx matchInString:s]) != nil) {
c.filterMatch = m;
[_filteredCompletions addObject:c];
}
}
if (_fuzzySearch)
[_filteredCompletions sortUsingComparator:^(id a, id b) {
ViCompletion *ca = a, *cb = b;
if (ca.score > cb.score)
return (NSComparisonResult)NSOrderedAscending;
else if (cb.score > ca.score)
return (NSComparisonResult)NSOrderedDescending;
return (NSComparisonResult)NSOrderedSame;
}];
[self updateUI];
}
- (void)updateUI
{
[self updateUIForPrefixWindow:nil];
}
- (void)updateUIForPrefixWindow:(NSWindow *)prefixWindow
{
[tableView reloadData];
NSInteger selectionRow = _positionCompletionsBelowPrefix ? 0 : _filteredCompletions.count - 1;
[self selectCompletionRowWithDelegateCalls:selectionRow];
[tableView scrollRowToVisible:selectionRow];
if (! prefixWindow)
prefixWindow = [window parentWindow];
[self updateBoundsWithPrefixWindow:prefixWindow];
}
- (void)setFilter:(NSString *)aString
{
_filter = [aString mutableCopy];
[self filterCompletions];
}
- (void)reset
{
_completions = nil;
_filteredCompletions = nil;
_filter = nil;
_provider = nil;
_prefix = nil;
_options = nil;
_delegate = nil; // delegate must be set for each completion, we don't want a lingering deallocated delegate to be called
}
- (void)acceptByKey:(NSInteger)termKey
{
NSInteger row = [tableView selectedRow];
if (row >= 0 && row < [_filteredCompletions count]) {
_selection = [_filteredCompletions objectAtIndex:row];
_selection = [self completionForRow:row];
_range = NSMakeRange(_range.location, _prefixLength + [_filter length]);
}
[self terminateWithKey:termKey completion:_selection];
}
- (BOOL)cancel:(ViCommand *)command
{
if (! _delegate)
return NO;
[self terminateWithKey:[[command.mapping.keySequence lastObject] integerValue] completion:nil];
return YES;
}
- (void)terminateWithKey:(NSInteger)terminatingKey completion:(ViCompletion *)completion
{
_terminatingKey = terminatingKey;
[_delegate completionController:self didTerminateWithKey:_terminatingKey selectedCompletion:completion];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:NSWindowDidResignMainNotification
object:nil];
[window orderOut:nil];
[self reset];
}
- (BOOL)accept_if_not_autocompleting:(ViCommand *)command
{
if (! _autocompleting) {
[self accept:command];
} else {
[self cancel:command];
}
return YES;
}
- (BOOL)accept:(ViCommand *)command
{
[self acceptByKey:[[command.mapping.keySequence lastObject] integerValue]];
return YES;
}
- (BOOL)input_character:(ViCommand *)command
{
NSInteger keyCode = [[command.mapping.keySequence lastObject] integerValue];
SEL sel = @selector(completionController:shouldTerminateForKey:);
if ([_delegate respondsToSelector:sel]) {
BOOL shouldTerminate = [_delegate completionController:self shouldTerminateForKey:keyCode];
if (shouldTerminate) {
[self acceptByKey:keyCode];
return YES;
}
}
if (keyCode < 0x20) /* ignore control characters? */
return NO;
if (keyCode > 0xFFFF) /* ignore key equivalents? */
return NO;
NSString *string = [NSString stringWithFormat:@"%C", (unichar)keyCode];
[_filter appendString:string];
[self filterCompletions];
if ([_filteredCompletions count] == 0) {
[self terminateWithKey:keyCode completion:nil];
}
return YES;
}
- (BOOL)input_backspace:(ViCommand *)command {
NSInteger keyCode = [[command.mapping.keySequence lastObject] integerValue];
if (_filter.length > 0) {
[_filter deleteCharactersInRange:NSMakeRange(_filter.length - 1, 1)];
[self filterCompletions];
} else {
/* This backspace goes beyond the filter into the prefix. Dismiss the window. */
[self terminateWithKey:keyCode completion:nil];
}
if ([_filteredCompletions count] == 0) {
[self terminateWithKey:keyCode completion:nil];
}
return YES;
}
- (void)updateCompletions
{
for (ViCompletion *c in _completions) {
c.prefixLength = _prefixLength;
c.filterIsFuzzy = _fuzzySearch;
}
}
- (BOOL)complete_partially:(ViCommand *)command
{
SEL sel = @selector(completionController:insertPartialCompletion:inRange:);
if (![_delegate respondsToSelector:sel])
return NO;
NSString *partialCompletion =
[ViCompletionController commonPrefixInCompletions:_filteredCompletions];
if ([partialCompletion length] == 0)
return YES;
DEBUG(@"common prefix is [%@], range is %@", partialCompletion, NSStringFromRange(_range));
_range = NSMakeRange(_range.location, _prefixLength + [_filter length]);
BOOL ret =
[_delegate completionController:self insertPartialCompletion:partialCompletion inRange:_range];
if (!ret)
return NO;
_range = NSMakeRange(_range.location, [partialCompletion length]);
DEBUG(@"_range => %@", NSStringFromRange(_range));
_prefixLength = _range.length;
[self setCompletions:_filteredCompletions];
[self updateCompletions];
return YES;
}
- (BOOL)accept_or_complete_partially:(ViCommand *)command
{
SEL sel = @selector(completionController:insertPartialCompletion:inRange:);
if (_fuzzySearch || _autocompleting || ![_delegate respondsToSelector:sel])
return [self accept:command];
if (![self complete_partially:command])
return [self cancel:command];
if ([_filteredCompletions count] == 1)
return [self accept:command];
return YES;
}
- (BOOL)select_previous_completion:(ViCommand *)command
{
NSUInteger row = [tableView selectedRow];
if (row == -1) {
row = 0;
} else if (row == 0) {
return NO;
}
BOOL selectionSuccessful = [self selectCompletionRowWithDelegateCalls:--row];
if (selectionSuccessful) {
[tableView scrollRowToVisible:row];
}
return selectionSuccessful;
}
- (BOOL)select_next_completion:(ViCommand *)command
{
NSUInteger row = [tableView selectedRow];
if (row == -1) {
row = 0;
} else if (row + 1 >= [tableView numberOfRows]) {
return NO;
}
BOOL selectionSuccessful = [self selectCompletionRowWithDelegateCalls:++row];
if (selectionSuccessful) {
[tableView scrollRowToVisible:row];
}
return selectionSuccessful;
}
- (BOOL)toggle_fuzzy:(ViCommand *)command
{
_fuzzySearch = !_fuzzySearch;
for (ViCompletion *c in _completions)
c.filterIsFuzzy = _fuzzySearch;
[self filterCompletions];
if ([_filteredCompletions count] == 1)
return [self accept:command];
return YES;
}
#pragma mark -
#pragma mark NSTableViewDataSource Protocol
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [_filteredCompletions count];
}
- (id)tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn
row:(NSInteger)rowIndex
{
return [self completionForRow:rowIndex].title;
}
#pragma mark - NSTableViewDelegate
/* ------------------IMPORTANT NOTE---------------------
THESE DELEGATE METHODS ARE NOT CALLED BY THE SYSTEM
DURING MOVE-UP & MOVE-DOWN KEY PRESSES.
The calls are manually inserted into the move_up:
and move_down: method via a call to
-selectCompletionRowWithDelegateCalls:
If you need more delegate calls to be processed, make
sure you add them there.
-----------------------------------------------------*/
- (void)tableViewSelectionDidChange:(NSNotification *)notification {
NSInteger newSelection = [tableView selectedRow];
[self completionForRow:newSelection].isCurrentChoice = YES;
}
- (BOOL)selectionShouldChangeInTableView:(NSTableView *)tv {
NSInteger oldSelection = [tableView selectedRow];
[self completionForRow:oldSelection].isCurrentChoice = NO;
return YES;
}
- (ViCompletion *)completionForRow:(NSInteger)row {
if (row < 0 || row >= _filteredCompletions.count) {
return nil;
}
NSInteger equivalentIndex = _positionCompletionsBelowPrefix ? row : _filteredCompletions.count - 1 - row;
return [_filteredCompletions objectAtIndex:equivalentIndex];
}
#pragma mark - Helpers
- (BOOL)selectCompletionRowWithDelegateCalls:(NSInteger)completionRow {
if (![tableView.delegate selectionShouldChangeInTableView:tableView]) {
return NO;
}
[tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:completionRow]
byExtendingSelection:NO];
/* Calling the delegate directly, but that's a weird delegate call anyway. */
[tableView.delegate tableViewSelectionDidChange:nil];
return YES;
}
- (NSPoint)computeWindowOriginForSize:(NSSize)winsz fromPrefixWindow:(NSWindow *)prefixWindow
{
NSRect screenRect = [prefixWindow convertRectToScreen:_prefixWindowRect];
NSPoint origin = screenRect.origin;
NSScreen *screen = [NSScreen mainScreen];
NSSize screenSize = [window convertRectFromScreen:[screen visibleFrame]].size;
/*
If this is the first time the window appears, determine if we need to show
the completions above or below. Default is below.
*/
if ([NSApp modalWindow] != window) {
if (winsz.height > screenRect.origin.y) {
_positionCompletionsBelowPrefix = NO;
} else {
_positionCompletionsBelowPrefix = YES;
}
}
/* Now we compute the origin. */
if (_positionCompletionsBelowPrefix) {
if (origin.y < winsz.height) {
origin.y = winsz.height + 5;
}
} else {
if (origin.y + winsz.height > screenSize.height) {
origin.y = screenSize.height - winsz.height - 5;
}
}
origin.x -= 3; // Align with the character. Hack, but computing the value is hard. :-/
if (origin.x + winsz.width > screenSize.width) {
origin.x = screenSize.width - winsz.width;
}
if (_positionCompletionsBelowPrefix) {
origin.y -= winsz.height;
} else {
origin.y += screenRect.size.height;
}
return origin;
}
@end