Skip to content
Snippets Groups Projects
Commit ad0284d7 authored by David Bühler's avatar David Bühler Committed by Maxime Jacquemin
Browse files

[ivette] Source code: fixes the selection of location at a left click position.

- Uses an event handler on "mousedown" instead of completely replacing the
  default behavior on a mouse click.
- Avoids changing the cursor position because of the selection change due to
  a mouse click.
parent 86cf046f
No related branches found
No related tags found
No related merge requests found
...@@ -63,7 +63,7 @@ const D = new Dome.Debug('Source Code'); ...@@ -63,7 +63,7 @@ const D = new Dome.Debug('Source Code');
export default function SourceCode() { export default function SourceCode() {
// Hooks // Hooks
const [buffer] = React.useState(new RichTextBuffer()); const [buffer] = React.useState(() => new RichTextBuffer());
const [selection, updateSelection] = States.useSelection(); const [selection, updateSelection] = States.useSelection();
const theFunction = selection?.current?.fct; const theFunction = selection?.current?.fct;
const theMarker = selection?.current?.marker; const theMarker = selection?.current?.marker;
...@@ -96,28 +96,50 @@ export default function SourceCode() { ...@@ -96,28 +96,50 @@ export default function SourceCode() {
const text = React.useMemo(read, [file, onError]); const text = React.useMemo(read, [file, onError]);
const { result } = Dome.usePromise(text); const { result } = Dome.usePromise(text);
React.useEffect(() => buffer.setValue(result), [buffer, result]); React.useEffect(() => buffer.setValue(result), [buffer, result]);
React.useEffect(() => buffer.setCursorOnTop(line), [buffer, line, result]);
/* Last location selected by a click in the source code. */
const selected: React.MutableRefObject<undefined | States.Location> =
React.useRef();
React.useEffect(() => {
if (selected.current && selected?.current === selection?.current)
selected.current = undefined;
else
buffer.setCursorOnTop(line);
}, [buffer, selection, line, result]);
/* CodeMirror types used to bind callbacks to extraKeys. */ /* CodeMirror types used to bind callbacks to extraKeys. */
type position = CodeMirror.Position; type position = CodeMirror.Position;
type editor = CodeMirror.Editor; type editor = CodeMirror.Editor;
async function select(_: editor, pos?: position) { async function select(editor: editor, event: MouseEvent) {
const pos = editor.coordsChar({ left: event.x, top: event.y });
if (file === '' || !pos) return; if (file === '' || !pos) return;
const arg = [file, pos.line + 1, pos.ch + 1]; const arg = [file, pos.line + 1, pos.ch + 1];
try { Server
const [fct, marker] = await Server.send(getMarkerAt, arg); .send(getMarkerAt, arg)
if (fct || marker) .then(([fct, marker]) => {
updateSelection({ location: { fct, marker } }); if (fct || marker) {
} catch (err) { const location = { fct, marker } as States.Location;
D.error(`Failed to get marker from source file position: ${err}`); selected.current = location;
Status.setMessage({ updateSelection({ location });
text: 'Failed request to Frama-C server', }
kind: 'error', })
.catch((err) => {
D.error(`Failed to get marker from source file position: ${err}`);
Status.setMessage({
text: 'Failed request to Frama-C server',
kind: 'error',
});
}); });
}
} }
const selectCallback = React.useCallback(select, [file]);
React.useEffect(() => {
buffer.forEach((cm) => cm.on('mousedown', selectCallback));
return () => buffer.forEach((cm) => cm.off('mousedown', selectCallback));
}, [buffer, selectCallback]);
const [command] = Settings.useGlobalSettings(Preferences.EditorCommand); const [command] = Settings.useGlobalSettings(Preferences.EditorCommand);
async function launchEditor(_?: editor, pos?: position) { async function launchEditor(_?: editor, pos?: position) {
if (file !== '') { if (file !== '') {
...@@ -179,7 +201,6 @@ export default function SourceCode() { ...@@ -179,7 +201,6 @@ export default function SourceCode() {
lineNumbers={!!theFunction} lineNumbers={!!theFunction}
styleActiveLine={!!theFunction} styleActiveLine={!!theFunction}
extraKeys={{ extraKeys={{
LeftClick: select as (_: CodeMirror.Editor) => void,
'Alt-F': 'findPersistent', 'Alt-F': 'findPersistent',
'Ctrl-LeftClick': launchEditor as (_: CodeMirror.Editor) => void, 'Ctrl-LeftClick': launchEditor as (_: CodeMirror.Editor) => void,
RightClick: contextMenu as (_: CodeMirror.Editor) => void, RightClick: contextMenu as (_: CodeMirror.Editor) => void,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment