initState method
Called when this object is inserted into the tree.
The framework will call this method exactly once for each State object it creates.
Override this method to perform initialization that depends on the location at which this object was inserted into the tree (i.e., context) or on the widget used to configure this object (i.e., widget).
If a State's build method depends on an object that can itself change state, for example a ChangeNotifier or Stream, or some other object to which one can subscribe to receive notifications, then be sure to subscribe and unsubscribe properly in initState, didUpdateWidget, and dispose:
- In initState, subscribe to the object.
- In didUpdateWidget unsubscribe from the old object and subscribe to the new one if the updated widget configuration requires replacing the object.
- In dispose, unsubscribe from the object.
You should not use BuildContext.dependOnInheritedWidgetOfExactType from this method. However, didChangeDependencies will be called immediately following this method, and BuildContext.dependOnInheritedWidgetOfExactType can be used there.
Implementations of this method should start with a call to the inherited
method, as in super.initState().
Implementation
@override
void initState() {
completionsController = CompletionsController(
dictionary: [],
refresh: setState,
onAcceptAndDismiss: acceptAndDismissCompletionsWidget,
);
getData();
scrollController.addListener(() {
if (scrollController.offset > 0 && !hasScrolled) {
setState(() {
hasScrolled = true;
});
} else if (scrollController.offset <= 0 && hasScrolled) {
setState(() {
hasScrolled = false;
});
}
});
// All actions needed to spin up a new editor instance
void loadUpInstance() {
codeTextStyle = TextStyle(
color: widget.api.workspace.workspaceConfigs.themeBundle.editorTheme
.editorForeground,
fontFamily: 'IBMPlexMono',
height: utils.AffogatoConstants.lineHeight,
fontSize:
widget.api.workspace.workspaceConfigs.stylingConfigs.editorFontSize,
);
// Create a single Text character using the editor's font style and paint it on an imaginary canvas
// to observe its width and height. This massively increases performance as font-dependent spacing operations
// such as generating indentation rulers or calculating cursor position no longer need to build and render multiple
// Text() objects but rather, can use a predefined SizedBox() and even the calculated cellWidth and cellHeight for computations.
final tp = TextPainter(
textDirection: TextDirection.ltr,
text: TextSpan(text: ' ', style: codeTextStyle),
)
..layout()
..paint(
Canvas(PictureRecorder()),
Offset.zero,
);
cellWidth = (tp.size.width);
cellHeight = (tp.size.height);
tp.dispose();
textController = AffogatoEditorFieldController(
languageBundle: data.languageBundle,
themeBundle: data.themeBundle,
workspaceConfigs: widget.api.workspace.workspaceConfigs,
)..addListener(() {
if (textController.text.isNotEmpty) {
for (int i = textController.selection.baseOffset; i >= 1; i--) {
if (textController.text[i - 1] == '\n') {
currentCharNum = textController.selection.baseOffset - i;
return;
} else if (i == 1) {
currentCharNum = textController.selection.baseOffset - i + 1;
return;
}
}
}
});
// Assume instant autosave
textController.text =
widget.api.vfs.accessEntity(data.documentId)!.document!.content;
// Set up completions and LSP (if applicable)
completionsController.dictionary.addAll(completionsController
.parseAllWordsInDocument(content: textController.text));
// Set off the event
AffogatoEvents.editorInstanceLoadedEventsController.add(
EditorInstanceLoadedEvent(
documentId: data.documentId,
instanceId: widget.instanceId,
paneId: widget.paneId,
),
);
// Link the text controller to the document
textController.addListener(() {
widget.api.vfs.accessEntity(
data.documentId,
isDir: false,
action: (entity) {
if (entity != null) {
entity.document!.addVersion(textController.text);
}
},
);
AffogatoEvents.vfsDocumentChangedEventsController.add(
VFSDocumentChangedEvent(
newContent: textController.text,
documentId: data.documentId,
selection: textController.selection,
originId: widget.instanceId,
),
);
setState(() {});
});
registerListener(
widget.api.vfs.documentChangedStream.where((event) =>
event.documentId == data.documentId &&
event.originId != widget.instanceId),
(event) {
setState(() {
textController.text = widget.api.vfs
.accessEntity(data.documentId, isDir: false)!
.document!
.content;
});
},
);
// the actual document parsing and interceptors
registerListener(
widget.api.editor.keyEventsStream
.where((e) => e.instanceId == widget.instanceId),
(event) {
if (event.keyEvent is KeyDownEvent) {
textFieldFocusNode.requestFocus();
if (event.keyEvent.logicalKey == LogicalKeyboardKey.tab) {
setState(() {
insertTextAtCursorPos(' ' *
widget.api.workspace.workspaceConfigs.stylingConfigs
.tabSizeInSpaces);
});
}
setState(() {
completionsController.registerTextChanged(
content: textController.text,
selection: textController.selection,
keyEvent: event.keyEvent,
);
});
}
},
);
registerListener(
widget.api.vfs.documentRequestChangeStream
.where((event) => event.entityId == data.documentId),
(event) {
textController.value = event.editorAction.editingValue;
setState(() {});
AffogatoEvents.vfsDocumentChangedEventsController.add(
VFSDocumentChangedEvent(
newContent: textController.text,
documentId: data.documentId,
selection: textController.selection,
originId: widget.instanceId,
),
);
},
);
registerListener(
widget.api.editor.instanceRequestToggleSearchOverlay
.where((event) => event.documentId == data.documentId),
(_) => setState(() {
searchAndReplaceController.toggle();
}),
);
}
// Call once during initState
loadUpInstance();
// And register a listener to call whenever the instance needs to be reloaded
registerListener(
widget.api.editor.instanceRequestReloadStream,
(event) {
setState(() {
// `saveInstance()`
loadUpInstance();
});
},
);
super.initState();
}