Revision: 22219 http://sourceforge.net/p/bibdesk/svn/22219 Author: hofman Date: 2018-05-06 08:49:08 +0000 (Sun, 06 May 2018) Log Message: ----------- Different way to display expansion in macro completion, replace full description by macro on insert using delegate methods
Modified Paths: -------------- trunk/bibdesk/BDSKCompletionManager.h trunk/bibdesk/BDSKCompletionManager.m trunk/bibdesk/BDSKEditor.m trunk/bibdesk/BDSKFieldEditor.h trunk/bibdesk/BDSKFieldEditor.m trunk/bibdesk/BDSKTextImportController.m trunk/bibdesk/BDSKTextViewCompletionController.h trunk/bibdesk/BDSKTextViewCompletionController.m Modified: trunk/bibdesk/BDSKCompletionManager.h =================================================================== --- trunk/bibdesk/BDSKCompletionManager.h 2018-05-06 06:30:55 UTC (rev 22218) +++ trunk/bibdesk/BDSKCompletionManager.h 2018-05-06 08:49:08 UTC (rev 22219) @@ -52,6 +52,7 @@ - (NSArray *)entry:(NSString *)entry completions:(NSArray *)words forPartialWordRange:(NSRange)charRange ofString:(NSString *)fullString indexOfSelectedItem:(NSInteger *)index; - (NSRange)rangeForUserCompletion:(NSRange)charRange forBibTeXString:(NSString *)fullString; -- (NSArray *)possibleMatches:(NSDictionary *)definitions labels:(NSArray **)labels forBibTeXString:(NSString *)fullString partialWordRange:(NSRange)charRange indexOfBestMatch:(NSInteger *)index; +- (NSArray *)possibleMatches:(NSDictionary *)definitions forBibTeXString:(NSString *)fullString partialWordRange:(NSRange)charRange indexOfBestMatch:(NSInteger *)index; +- (NSString *)possibleMatch:(NSString *)match; @end Modified: trunk/bibdesk/BDSKCompletionManager.m =================================================================== --- trunk/bibdesk/BDSKCompletionManager.m 2018-05-06 06:30:55 UTC (rev 22218) +++ trunk/bibdesk/BDSKCompletionManager.m 2018-05-06 08:49:08 UTC (rev 22219) @@ -198,7 +198,7 @@ return charRange; } -- (NSArray *)possibleMatches:(NSDictionary *)definitions labels:(NSArray **)labels forBibTeXString:(NSString *)fullString partialWordRange:(NSRange)charRange indexOfBestMatch:(NSInteger *)idx{ +- (NSArray *)possibleMatches:(NSDictionary *)definitions forBibTeXString:(NSString *)fullString partialWordRange:(NSRange)charRange indexOfBestMatch:(NSInteger *)idx{ NSString *partialString = [fullString substringWithRange:charRange]; NSMutableArray *matches = [NSMutableArray array]; NSString *key = nil; @@ -210,12 +210,6 @@ [matches addObject:key]; } [matches sortUsingSelector:@selector(caseInsensitiveCompare:)]; - if (labels) { - NSMutableArray *array = [NSMutableArray array]; - for (key in matches) - [array addObject:[NSString stringWithFormat:@"%@ [%@]", key, [definitions valueForKey:key]]]; - *labels = array; - } NSInteger i, count = [matches count]; for (i = 0; i < count; i++) { @@ -227,7 +221,19 @@ } } - return matches; + // we show the expansion in the list, this is removed by possibleMatch: when actually inserting + NSMutableArray *displayMatches = [NSMutableArray array]; + for (key in matches) + [displayMatches addObject:[NSString stringWithFormat:@"%@ [%@]", key, [definitions valueForKey:key]]]; + + return displayMatches; } +- (NSString *)possibleMatch:(NSString *)match { + NSRange range = [match rangeOfString:@" ["]; + if (range.location != NSNotFound) + match = [match substringToIndex:range.location]; + return match; +} + @end Modified: trunk/bibdesk/BDSKEditor.m =================================================================== --- trunk/bibdesk/BDSKEditor.m 2018-05-06 06:30:55 UTC (rev 22218) +++ trunk/bibdesk/BDSKEditor.m 2018-05-06 08:49:08 UTC (rev 22219) @@ -2523,6 +2523,12 @@ } } +- (NSString *)control:(NSControl *)control textView:(NSTextView *)textView completion:(NSString *)word { + if (control == tableView && [complexStringEditor isAttached]) + return [[BDSKCompletionManager sharedManager] possibleMatch:word]; + return word; +} + - (BOOL)control:(NSControl *)control textViewShouldAutoComplete:(NSTextView *)textview { if (control == tableView) return [[NSUserDefaults standardUserDefaults] boolForKey:BDSKEditorFormShouldAutoCompleteKey]; @@ -2529,12 +2535,11 @@ return NO; } -- (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)charRange labels:(NSArray **)labels indexOfSelectedItem:(NSInteger *)idx{ +- (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger *)idx{ if (control != tableView) { return words; } else if ([complexStringEditor isAttached]) { return [[BDSKCompletionManager sharedManager] possibleMatches:[[publication macroResolver] allMacroDefinitions] - labels:labels forBibTeXString:[textView string] partialWordRange:charRange indexOfBestMatch:idx]; @@ -2548,10 +2553,6 @@ } } -- (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger *)idx{ - return [self control:control textView:textView completions:words forPartialWordRange:charRange labels:NULL indexOfSelectedItem:idx]; -} - - (BOOL)control:(NSControl *)control textViewShouldLinkKeys:(NSTextView *)textView { return [control isEqual:tableView] && [[fields objectAtIndex:[tableView editedRow]] isCitationField]; } Modified: trunk/bibdesk/BDSKFieldEditor.h =================================================================== --- trunk/bibdesk/BDSKFieldEditor.h 2018-05-06 06:30:55 UTC (rev 22218) +++ trunk/bibdesk/BDSKFieldEditor.h 2018-05-06 08:49:08 UTC (rev 22219) @@ -42,8 +42,8 @@ @protocol BDSKFieldEditorDelegate <NSTextViewDelegate> @optional -- (NSArray *)textView:(NSTextView *)textView completions:(NSArray *)completions forPartialWordRange:(NSRange)charRange labels:(NSArray **)labels indexOfSelectedItem:(NSInteger *)index; - (NSRange)textView:(NSTextView *)textView rangeForUserCompletion:(NSRange)charRange; +- (NSString *)textView:(NSTextView *)textView completion:(NSString *)word; - (BOOL)textViewShouldAutoComplete:(NSTextView *)textView; - (BOOL)textViewShouldLinkKeys:(NSTextView *)textView; - (BOOL)textView:(NSTextView *)textView isValidKey:(NSString *)key; @@ -63,8 +63,8 @@ // currently implemented for NSTextField and NSTableView @protocol BDSKControlFieldEditorDelegate <NSControlTextEditingDelegate> @optional -- (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)completions forPartialWordRange:(NSRange)charRange labels:(NSArray **)labels indexOfSelectedItem:(NSInteger *)index; - (NSRange)control:(NSControl *)control textView:(NSTextView *)textView rangeForUserCompletion:(NSRange)charRange; +- (NSString *)control:(NSControl *)control textView:(NSTextView *)textView completion:(NSString *)word; - (BOOL)control:(NSControl *)control textViewShouldAutoComplete:(NSTextView *)textView; - (BOOL)control:(NSControl *)control textViewShouldLinkKeys:(NSTextView *)textView; - (BOOL)control:(NSControl *)control textView:(NSTextView *)textView isValidKey:(NSString *)key; Modified: trunk/bibdesk/BDSKFieldEditor.m =================================================================== --- trunk/bibdesk/BDSKFieldEditor.m 2018-05-06 06:30:55 UTC (rev 22218) +++ trunk/bibdesk/BDSKFieldEditor.m 2018-05-06 08:49:08 UTC (rev 22219) @@ -282,20 +282,19 @@ return charRange; } +- (void)insertCompletion:(NSString *)word forPartialWordRange:(NSRange)charRange movement:(NSInteger)movement isFinal:(BOOL)flag { + if ([[self delegate] respondsToSelector:@selector(textView:completion:)]) + word = [[self delegate] textView:self completion:word]; + [super insertCompletion:word forPartialWordRange:charRange movement:movement isFinal:flag]; +} + #pragma mark Auto-completion methods - (NSArray *)completionsForPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger *)idx; { - return [self completionsForPartialWordRange:charRange labels:NULL indexOfSelectedItem:idx]; -} - -- (NSArray *)completionsForPartialWordRange:(NSRange)charRange labels:(NSArray **)labels indexOfSelectedItem:(NSInteger *)idx; -{ NSArray *completions = nil; - if([[self delegate] respondsToSelector:@selector(textView:completions:forPartialWordRange:labels:indexOfSelectedItem:)]) - completions = [[self delegate] textView:self completions:[NSArray array] forPartialWordRange:charRange labels:labels indexOfSelectedItem:idx]; - else if([[self delegate] respondsToSelector:@selector(textView:completions:forPartialWordRange:indexOfSelectedItem:)]) + if([[self delegate] respondsToSelector:@selector(textView:completions:forPartialWordRange:indexOfSelectedItem:)]) completions = [[self delegate] textView:self completions:[NSArray array] forPartialWordRange:charRange indexOfSelectedItem:idx]; // Default is to call -[NSSpellChecker completionsForPartialWordRange:inString:language:inSpellDocumentWithTag:], but this apparently sends a DO message to CocoAspell (in a separate process), and we block the main runloop until it returns a long time later. Lacking a way to determine whether the system speller (which works fine) or CocoAspell is in use, we'll just return our own completions. @@ -316,12 +315,12 @@ // make sure to initialize this NSInteger idx = 0; NSArray *labels = nil; - NSArray *completions = [self completionsForPartialWordRange:selRange labels:&labels indexOfSelectedItem:&idx]; + NSArray *completions = [self completionsForPartialWordRange:selRange indexOfSelectedItem:&idx]; if(sender == self) // auto-complete, don't select an item idx = -1; - [[BDSKTextViewCompletionController sharedController] displayCompletions:completions labels:labels indexOfSelectedItem:idx forPartialWordRange:selRange originalString:[string substringWithRange:selRange] forTextView:self]; + [[BDSKTextViewCompletionController sharedController] displayCompletions:completions indexOfSelectedItem:idx forPartialWordRange:selRange originalString:[string substringWithRange:selRange] forTextView:self]; } - (NSRange)selectionRangeForProposedRange:(NSRange)proposedSelRange granularity:(NSSelectionGranularity)granularity { @@ -411,16 +410,16 @@ @implementation NSTableView (BDSKFieldEditorDelegate) -- (NSArray *)textView:(NSTextView *)textView completions:(NSArray *)completions forPartialWordRange:(NSRange)charRange labels:(NSArray **)labels indexOfSelectedItem:(NSInteger *)index { - if (textView == [self currentEditor] && [[self delegate] respondsToSelector:@selector(control:textView:completions:forPartialWordRange:labels:indexOfSelectedItem:)]) - return [(id<BDSKControlFieldEditorDelegate>)[self delegate] control:self textView:textView completions:completions forPartialWordRange:charRange labels:labels indexOfSelectedItem:index]; - return [self textView:textView completions:completions forPartialWordRange:charRange indexOfSelectedItem:index]; +- (NSRange)textView:(NSTextView *)textView rangeForUserCompletion:(NSRange)charRange { + if (textView == [self currentEditor] && [[self delegate] respondsToSelector:@selector(control:textView:rangeForUserCompletion:)]) + return [(id<BDSKControlFieldEditorDelegate>)[self delegate] control:self textView:textView rangeForUserCompletion:charRange]; + return charRange; } -- (NSRange)textView:(NSTextView *)textView rangeForUserCompletion:(NSRange)charRange { - if (textView == [self currentEditor] && [[self delegate] respondsToSelector:@selector(control:textView:rangeForUserCompletion:)]) - return [(id<BDSKControlFieldEditorDelegate>)[self delegate] control:self textView:textView rangeForUserCompletion:charRange]; - return charRange; +- (NSString *)textView:(NSTextView *)textView completion:(NSString *)word { + if (textView == [self currentEditor] && [[self delegate] respondsToSelector:@selector(control:textView:completion:)]) + return [(id<BDSKControlFieldEditorDelegate>)[self delegate] control:self textView:textView completion:word]; + return word; } - (BOOL)textViewShouldAutoComplete:(NSTextView *)textView { Modified: trunk/bibdesk/BDSKTextImportController.m =================================================================== --- trunk/bibdesk/BDSKTextImportController.m 2018-05-06 06:30:55 UTC (rev 22218) +++ trunk/bibdesk/BDSKTextImportController.m 2018-05-06 08:49:08 UTC (rev 22219) @@ -1006,13 +1006,18 @@ } } -- (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)charRange labels:(NSArray **)labels indexOfSelectedItem:(NSInteger *)idx{ +- (NSString *)control:(NSControl *)control textView:(NSTextView *)textView completion:(NSString *)word { + if (control == itemTableView && [complexStringEditor isAttached]) + return [[BDSKCompletionManager sharedManager] possibleMatch:word]; + return word; +} + +- (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger *)idx{ if (control != itemTableView) { return words; } else if ([complexStringEditor isAttached]) { return [[BDSKCompletionManager sharedManager] possibleMatches:[[self macroResolver] allMacroDefinitions] - labels:labels - forBibTeXString:[textView string] + forBibTeXString:[textView string] partialWordRange:charRange indexOfBestMatch:idx]; } else { @@ -1023,11 +1028,6 @@ indexOfSelectedItem:idx]; } } - -- (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger *)idx{ - return [self control:control textView:textView completions:words forPartialWordRange:charRange labels:NULL indexOfSelectedItem:idx]; -} - - (BOOL)control:(NSControl *)control textViewShouldAutoComplete:(NSTextView *)textview { if (control == itemTableView) return [[NSUserDefaults standardUserDefaults] boolForKey:BDSKEditorFormShouldAutoCompleteKey]; Modified: trunk/bibdesk/BDSKTextViewCompletionController.h =================================================================== --- trunk/bibdesk/BDSKTextViewCompletionController.h 2018-05-06 06:30:55 UTC (rev 22218) +++ trunk/bibdesk/BDSKTextViewCompletionController.h 2018-05-06 08:49:08 UTC (rev 22219) @@ -42,7 +42,6 @@ @interface BDSKTextViewCompletionController : NSWindowController <NSTableViewDelegate, NSTableViewDataSource> { NSArray *completions; - NSArray *labels; NSString *originalString; NSInteger movement; NSTableView *tableView; @@ -54,7 +53,7 @@ + (id)sharedController; - (NSTextView *)currentTextView; -- (void)displayCompletions:(NSArray *)completions labels:(NSArray *)labels indexOfSelectedItem:(NSInteger)indexOfSelectedItem forPartialWordRange:(NSRange)partialWordRange originalString:(NSString *)originalString forTextView:(NSTextView *)textView; +- (void)displayCompletions:(NSArray *)completions indexOfSelectedItem:(NSInteger)indexOfSelectedItem forPartialWordRange:(NSRange)partialWordRange originalString:(NSString *)originalString forTextView:(NSTextView *)textView; - (void)endDisplay; - (void)endDisplayAndComplete:(BOOL)complete; - (void)endDisplayNoComplete; Modified: trunk/bibdesk/BDSKTextViewCompletionController.m =================================================================== --- trunk/bibdesk/BDSKTextViewCompletionController.m 2018-05-06 06:30:55 UTC (rev 22218) +++ trunk/bibdesk/BDSKTextViewCompletionController.m 2018-05-06 08:49:08 UTC (rev 22219) @@ -59,7 +59,6 @@ - (void)setOriginalString:(NSString *)string; - (void)setTextViewWindow:(NSWindow *)aWindow; - (void)setCompletions:(NSArray *)newCompletions; -- (void)setLabels:(NSArray *)newLabels; @end @@ -79,7 +78,6 @@ if (self) { tableView = [(NSScrollView *)[window contentView] documentView]; completions = nil; - labels = nil; originalString = nil; shouldInsert = YES; @@ -91,7 +89,6 @@ - (void)dealloc; { BDSKDESTROY(completions); - BDSKDESTROY(labels); BDSKDESTROY(originalString); BDSKDESTROY(textView); [super dealloc]; @@ -168,7 +165,7 @@ } } -- (void)displayCompletions:(NSArray *)array labels:(NSArray *)array2 indexOfSelectedItem:(NSInteger)indexOfSelectedItem forPartialWordRange:(NSRange)partialWordRange originalString:(NSString *)origString forTextView:(NSTextView *)tv; +- (void)displayCompletions:(NSArray *)array indexOfSelectedItem:(NSInteger)indexOfSelectedItem forPartialWordRange:(NSRange)partialWordRange originalString:(NSString *)origString forTextView:(NSTextView *)tv; { // do nothing; displaying an empty window can lead to oddities when typing, since we get keystrokes as well as the editor if([array count] == 0 || partialWordRange.length == 0 || partialWordRange.location == NSNotFound) @@ -187,7 +184,6 @@ [tableView deselectAll:nil]; [self setCompletions:array]; - [self setLabels:array2 && [array count] == [array2 count] ? array2 : array]; [tableView reloadData]; // requires screen coordinates; resize so our scroller stays onscreen (if possible) @@ -230,7 +226,6 @@ [self setCurrentTextView:nil]; [self setCompletions:nil]; - [self setLabels:nil]; [self setOriginalString:nil]; [tableView setDelegate:nil]; // in case it retains its delegate (shouldn't, though) @@ -249,9 +244,9 @@ #pragma mark table datasource -- (id)tableView:(NSTableView *)tv objectValueForTableColumn:(NSTableColumn *)column row:(NSInteger)row { return [labels objectAtIndex:row]; } +- (id)tableView:(NSTableView *)tv objectValueForTableColumn:(NSTableColumn *)column row:(NSInteger)row { return [completions objectAtIndex:row]; } -- (NSInteger)numberOfRowsInTableView:(NSTableView *)tv { return [labels count]; } +- (NSInteger)numberOfRowsInTableView:(NSTableView *)tv { return [completions count]; } @end @@ -307,15 +302,11 @@ NSInteger idx = -1; NSArray *newCompletions = nil; - NSArray *newLabels = nil; // may return { NSNotFound, 0 } NSRange charRange = [textView rangeForUserCompletion]; if(NSNotFound != charRange.location && [[textView string] isEqualToString:@""] == NO && [[textView string] length] >= NSMaxRange(charRange)){ - if ([textView respondsToSelector:@selector(completionsForPartialWordRange:labels:indexOfSelectedItem:)]) - newCompletions = [textView completionsForPartialWordRange:charRange labels:&newLabels indexOfSelectedItem:&idx]; - else - newCompletions = [textView completionsForPartialWordRange:charRange indexOfSelectedItem:&idx]; + newCompletions = [textView completionsForPartialWordRange:charRange indexOfSelectedItem:&idx]; } // if there are no completions, we should go away in order to avoid catching keystrokes when the completion window isn't visible; if the textview/delegate come up with a new list of completions, we'll be redisplayed anyway @@ -328,7 +319,6 @@ [tableView deselectAll:nil]; [self setCompletions:newCompletions]; - [self setLabels:newLabels && [newLabels count] == [newCompletions count] ? newLabels : newCompletions]; [tableView reloadData]; // reset the location in case it's changed; could keep charRange as an ivar as NSTextViewCompletionController does and compare against that? @@ -428,7 +418,7 @@ NSUInteger count = [tableView numberOfRows]; NSCell *cell = [[[tableView tableColumns] objectAtIndex:0] dataCell]; while(count--){ - [cell setStringValue:[labels objectAtIndex:count]]; + [cell setStringValue:[completions objectAtIndex:count]]; hSize = fmax(hSize, [cell cellSize].width); } hSize += [tableView intercellSpacing].width; @@ -495,14 +485,6 @@ } } -- (void)setLabels:(NSArray *)newLabels; -{ - if(labels != newLabels){ - [labels release]; - labels = [newLabels copy]; - } -} - @end #pragma mark NSWindow subclass This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot _______________________________________________ Bibdesk-commit mailing list Bibdesk-commit@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/bibdesk-commit