Skip to content

Commit 9b3adaa

Browse files
committed
Improved selection
1 parent 442ca2b commit 9b3adaa

6 files changed

Lines changed: 85 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
## 0.4.21
44

5-
- Update description for line selection (*selection ...* changes to *lines ...*)
5+
- Add **Bookmarks: Selection Style** to control bookmark selection behavior, and default to `Full-line` as it provides better visibility.
6+
- Update description for line selection (*selection ...* changes to *lines ...*).
67

78
## 0.4.20
89

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ In the **Notes** context menu for a given bookmark, use:
113113

114114
## Navigation
115115

116+
Clicking a bookmark in the **Bookmarks** view opens its target editor and selects the target range. For multi-line bookmarks the default *start-to-start* selection made ranges look like they were missing the last line and made single-line bookmarks harder to spot, so the default selection mode is now *full-line*. You can change this behavior in the **Bookmarks: Selection Mode** setting.
117+
118+
116119
### Commands
117120

118121
There are two commands, **Bookmarks: Go to Next in Current Editor** and **Bookmarks: Go to Previous in Current Editor**, that can be used to jump between bookmarks set in the current editor.
@@ -123,8 +126,7 @@ The following commands are available for other extensions to use: `bookmarks.nav
123126

124127
## Visualization
125128

126-
Bookmarks are expected to be rendered as markers on the corresponding editor gutter. This is supported via the **Bookmarks: Toggle In-Editor Markers** command but it is disabled by default due to [VSCode #5923](https://github.com/Microsoft/vscode/issues/5923) which causes these markers to affect the breakpoint markers in some cases (unacceptable experience).
127-
You can also use the **Hide In-Editor Markers** and **Show In-Editor Markers** actions in the **Bookmarks** view menu.
129+
Clicking a bookmark in the **Bookmarks** view selects its target range in the appropriate editor. Bookmarks can also be rendered as markers on the corresponding editor gutter. This is supported via the **Bookmarks: Toggle In-Editor Markers** command but it is disabled by default due to [VSCode #5923](https://github.com/Microsoft/vscode/issues/5923) which causes these markers to affect the breakpoint markers in some cases (unacceptable experience). You can also use the **Hide In-Editor Markers** and **Show In-Editor Markers** actions in the **Bookmarks** view menu.
128130

129131
<p align="center">
130132
<img width="448" alt="image" src="https://user-images.githubusercontent.com/38414719/185772569-eebf133d-adfc-4ff2-9c20-9066508a3345.png">

package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,24 @@
234234
"default": false,
235235
"description": "Render bookmark markers are displayed in editors.",
236236
"scope": "application"
237+
},
238+
"bookmarks.selectionStyle": {
239+
"type": "string",
240+
"default": "fullLine",
241+
"description": "Controls how bookmarks select lines in the editor.",
242+
"enum": [
243+
"default",
244+
"fullLine"
245+
],
246+
"enumItemLabels": [
247+
"Default",
248+
"Full-line"
249+
],
250+
"enumDescriptions": [
251+
"Selects from the start of the first line to the start of the last line (VS Code default behavior).",
252+
"Selects entire lines from start to end, including the last line fully."
253+
],
254+
"scope": "application"
237255
}
238256
}
239257
}

src/commands/openBookmark.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { commands, Range, workspace } from 'vscode';
2+
import { Bookmark } from '../bookmark';
3+
4+
export enum BookmarkSelectionStyle {
5+
/**
6+
* Uses VS Code's default behavior: selects from the start of the first line
7+
* to the start of the last line (exclusive).
8+
*/
9+
Default = 'default',
10+
11+
/**
12+
* Selects entire lines from start to end, including the last line fully.
13+
*/
14+
FullLine = 'fullLine',
15+
}
16+
17+
/**
18+
* Open bookmark.
19+
*/
20+
export async function openBookmark(
21+
bookmark: Bookmark,
22+
selectionStyle?: BookmarkSelectionStyle,
23+
): Promise<void> {
24+
let args: any = undefined;
25+
26+
const targetSelectionStyle =
27+
selectionStyle ??
28+
workspace.getConfiguration().get('bookmarks.selectionStyle', BookmarkSelectionStyle.Default);
29+
if (targetSelectionStyle === BookmarkSelectionStyle.FullLine) {
30+
const selection = await getSelection();
31+
32+
args = { selection };
33+
}
34+
35+
await commands.executeCommand('vscode.open', bookmark.uri, args);
36+
37+
async function getSelection() {
38+
let selection: Range | undefined;
39+
const document = await workspace
40+
.openTextDocument(bookmark.uri)
41+
.then(undefined, () => undefined);
42+
if (document) {
43+
const zeroStart = bookmark.start - 1;
44+
if (bookmark.end === undefined) {
45+
selection = document.lineAt(zeroStart).range;
46+
} else {
47+
const zeroEnd = bookmark.end - 1;
48+
selection = new Range(zeroStart, 0, zeroEnd, document.lineAt(zeroEnd).range.end.character);
49+
}
50+
}
51+
52+
return selection;
53+
}
54+
}

src/extension.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { addBookmarkAsync } from './commands/addBookmark';
99
import { addBookmarkFolderAsync } from './commands/addBookmarkFolder';
1010
import { exportBookmarks } from './commands/exportBookmarks';
1111
import { importBookmarks } from './commands/importBookmarks';
12+
import { openBookmark } from './commands/openBookmark';
1213
import { openFolderBookmarks } from './commands/openFolderBookmarks';
1314
import { removeBookmarkOrFolderAsync } from './commands/removeBookmarkOrFolder';
1415
import { renameBookmarkFolderAsync } from './commands/renameBookmarkFolder';
@@ -130,6 +131,10 @@ export async function activate(context: vscode.ExtensionContext) {
130131
'bookmarks.navigate.previous.editor',
131132
(pathOrUri?: string | vscode.Uri): Thenable<void> => navigateAsync(manager, false, pathOrUri),
132133
),
134+
vscode.commands.registerCommand(
135+
'_bookmarks.open',
136+
(bookmark: Bookmark): Thenable<void> => openBookmark(bookmark),
137+
),
133138
vscode.commands.registerCommand(
134139
'bookmarks.openFolder.tree',
135140
(container: BookmarkContainer): Thenable<void> => openFolderBookmarks(container),

src/tree/treeItemProvider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ export abstract class TreeItemProvider {
3333

3434
treeItem.command = {
3535
title: 'Open',
36-
command: 'vscode.open',
37-
arguments: [bookmark.uri],
36+
command: '_bookmarks.open',
37+
arguments: [bookmark],
3838
};
3939

4040
treeItem.contextValue = 'bookmark';

0 commit comments

Comments
 (0)