Controlling completion in a text editor

Completion can be added to any editor through a GtCompletionController Object subclass: #GtCompletionController instanceVariableNames: 'completionDelay textElement attribute overlay listElement previewAction drillDownActions announcer lostFocusDelay closeOnEmptyCompletion textEditorHandlers textElementFilters popupHandlers textElementRemovedDelay showOnTextModification completer completionResult showPreviewAttributes' classVariableNames: 'CompleteOnReturn PopupDelay' package: 'GToolkit-Completer-UI' that contains a so called GtCompletionStrategy Object subclass: #GtCompletionStrategy instanceVariableNames: '' classVariableNames: '' package: 'GToolkit-Completer-Strategies' .

The simplest strategy is based on a collection of strings. For example, the following editor offers a completion anywhere for the class names from the image:

editor := BrEditor new
		aptitude: BrGlamorousCodeEditorAptitude new;
		text: 'Type a capital letter to get completion for class names.'.
tree := GtPrefixTree new.
Object withAllSubclassesDo: [ :c | tree add: c name ].
completionStrategy := GtStringsCompletionStrategy new completions: tree.
completionController := GtCompletionController
		on: editor
		strategy: completionStrategy.
completionController install.
editor 
  

But sometimes, we might want to have a special completion just on a part of the text. That can be accomplished through a dedicated GtCompletionStrategyAttribute BlTextAttribute subclass: #GtCompletionStrategyAttribute instanceVariableNames: 'strategy' classVariableNames: '' package: 'GToolkit-Completer-Attributes' that can be installed on only a part of the text:

text := 'Type only in the blue part a capital letter to get completion for class names.'
		asRopedText.
editor := BrEditor new
		aptitude: BrGlamorousCodeEditorAptitude new;
		text: text.
tree := GtPrefixTree new.
Object withAllSubclassesDo: [ :c | tree add: c name ].
completionStrategy := GtStringsCompletionStrategy new completions: tree.
(GtCompletionController on: editor) install.
(text from: 1 to: 26)
	foreground: Color blue;
	attribute: (GtCompletionStrategyAttribute new strategy: completionStrategy).
editor
  

Combining this the completion strategy attribute with a styler allows you to build highly contextual completion behaviors. See more details about styling at Constructing a text editor with styling.