diff --git a/src/PanelStyle.js b/src/PanelStyle.js index f89bdcb83..8ba08ceb2 100644 --- a/src/PanelStyle.js +++ b/src/PanelStyle.js @@ -3,12 +3,8 @@ import GObject from "gi://GObject"; import Gtk from "gi://Gtk"; import Pango from "gi://Pango"; -import { - connect_signals, - getLanguage, - settings, - getItersAtRange, -} from "./util.js"; +import { connect_signals, getLanguage, settings } from "./util.js"; +import { getItersAtRange } from "./editor_utils.js"; import WorkbenchHoverProvider from "./WorkbenchHoverProvider.js"; diff --git a/src/PanelUI.js b/src/PanelUI.js index f09461f24..beee1fce6 100644 --- a/src/PanelUI.js +++ b/src/PanelUI.js @@ -13,8 +13,8 @@ import { disconnect_signals, replaceBufferText, unstack, - getItersAtRange, } from "./util.js"; +import { getItersAtRange } from "./editor_utils.js"; import { getPid, once } from "../troll/src/util.js"; import WorkbenchHoverProvider from "./WorkbenchHoverProvider.js"; diff --git a/src/editor_utils.js b/src/editor_utils.js new file mode 100644 index 000000000..7855c44d8 --- /dev/null +++ b/src/editor_utils.js @@ -0,0 +1,70 @@ +import { rangeEquals } from "./lsp/LSP.js"; + +export function getItersAtRange(buffer, { start, end }) { + let start_iter; + let end_iter; + + // Apply the tag on the whole line + // if diagnostic start and end are equals such as + // Blueprint-Error 13:12 to 13:12 Could not determine what kind of syntax is meant here + if (rangeEquals(start, end)) { + [, start_iter] = buffer.get_iter_at_line(start.line); + [, end_iter] = buffer.get_iter_at_line(end.line); + end_iter.forward_to_line_end(); + start_iter.forward_find_char((char) => char !== "", end_iter); + } else { + [, start_iter] = buffer.get_iter_at_line_offset( + start.line, + start.character + ); + [, end_iter] = buffer.get_iter_at_line_offset(end.line, end.character); + } + + return [start_iter, end_iter]; +} + +export function remove_line_at_cursor(buffer) { + const start = buffer.get_iter_at_offset(buffer.cursor_position); + const end = start.copy(); + start.set_line_offset(0); + end.forward_lines(1); + + if (end.is_end()) { + if (start.backward_line() && !start.ends_line()) + start.forward_to_line_end(); + } + + buffer.delete(start, end); +} + +export function toggle_comment_line_at_cursor(buffer) { + const line_comment_start = buffer + .get_language() + .get_metadata("line-comment-start"); + if (!line_comment_start) return false; + + const start = buffer.get_iter_at_offset(buffer.cursor_position); + const end = start.copy(); + + start.set_line_offset(0); + if (!end.ends_line()) end.forward_to_line_end(); + + const line = buffer.get_slice(start, end, true); + const reg_exp = new RegExp(`^ *${line_comment_start}`); + + const match = line.match(reg_exp); + // console.log(match.index); + if (match) { + const idx = line.indexOf(line_comment_start); + buffer.delete( + buffer.get_iter_at_line_offset(start.get_line(), idx)[1], + buffer.get_iter_at_line_offset( + start.get_line(), + idx + line_comment_start.length + )[1] + ); + // line.replace(reg_exp, ""); + } else { + buffer.insert(start, `${line_comment_start} `, -1); + } +} diff --git a/src/util.js b/src/util.js index 8e2309c3e..b7bd31a96 100644 --- a/src/util.js +++ b/src/util.js @@ -1,7 +1,6 @@ import GLib from "gi://GLib"; import Gio from "gi://Gio"; import Xdp from "gi://Xdp"; -import { rangeEquals } from "./lsp/LSP.js"; export const portal = new Xdp.Portal(); @@ -171,26 +170,3 @@ export function unstack(fn) { }); }; } - -export function getItersAtRange(buffer, { start, end }) { - let start_iter; - let end_iter; - - // Apply the tag on the whole line - // if diagnostic start and end are equals such as - // Blueprint-Error 13:12 to 13:12 Could not determine what kind of syntax is meant here - if (rangeEquals(start, end)) { - [, start_iter] = buffer.get_iter_at_line(start.line); - [, end_iter] = buffer.get_iter_at_line(end.line); - end_iter.forward_to_line_end(); - start_iter.forward_find_char((char) => char !== "", end_iter); - } else { - [, start_iter] = buffer.get_iter_at_line_offset( - start.line, - start.character - ); - [, end_iter] = buffer.get_iter_at_line_offset(end.line, end.character); - } - - return [start_iter, end_iter]; -} diff --git a/src/window.blp b/src/window.blp index 7b3228ebd..3a88cf978 100644 --- a/src/window.blp +++ b/src/window.blp @@ -181,6 +181,14 @@ Gtk.ApplicationWindow window { show-line-numbers: true; smart-backspace: true; tab-width: 2; + + ShortcutController js_controller { + scope: managed; + Shortcut { + trigger: 'j'; + action: 'cool'; + } + } } }; } diff --git a/src/window.js b/src/window.js index dad446a9c..14707ac67 100644 --- a/src/window.js +++ b/src/window.js @@ -29,6 +29,10 @@ import Previewer from "./Previewer/Previewer.js"; import Compiler from "./Compiler.js"; import { promiseTask } from "../troll/src/util.js"; import ThemeSelector from "../troll/src/widgets/ThemeSelector.js"; +import { + remove_line_at_cursor, + toggle_comment_line_at_cursor, +} from "./editor_utils.js"; import resource from "./window.blp"; @@ -76,6 +80,46 @@ export default function Window({ application }) { data_dir, }); + const shortcuts = [ + [ + [ + // VSCode + // "/", + // nope + "B", + ], + () => { + log("cool"); + const buffer = langs.javascript.document.source_view.buffer; + toggle_comment_line_at_cursor(buffer); + }, + ], + // [ + // [ + // // VSCode + // "K", + // // GNOME Builder + // "D", + // ], + // () => { + // const buffer = langs.javascript.document.source_view.buffer; + // remove_line_at_cursor(buffer); + // }, + // ], + ]; + const shortcutController = new Gtk.ShortcutController(); + shortcuts.forEach(([accels, fn]) => { + const shortcut = new Gtk.Shortcut({ + trigger: Gtk.ShortcutTrigger.parse_string(accels.join("|")), + action: Gtk.CallbackAction.new(() => { + fn(); + return true; + }), + }); + shortcutController.add_shortcut(shortcut); + }); + langs.javascript.document.source_view.add_controller(shortcutController); + langs.vala.document = Document({ source_view: builder.get_object("source_view_vala"), lang: "vala",