Skip to content

Commit 4608099

Browse files
authored
Merge pull request #3064 from jramosg/fix-3063-evaluate-selection-comment-context-menu
Fix 3063 evaluate selection comment context menu Fixes #3063
2 parents b0d62dd + 11c78ef commit 4608099

File tree

4 files changed

+72
-6
lines changed

4 files changed

+72
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Changes to Calva.
44

55
## [Unreleased]
66

7+
- Fix: [calva.evaluateSelectionAsComment missing from context menu](https://github.com/BetterThanTomorrow/calva/issues/3063)
78
- Fix: [Error instrumenting function through command palette](https://github.com/BetterThanTomorrow/calva/issues/2966)
89
- Fix: [paredit.insertSemiColon requires two undo operations in structure-preserving case](https://github.com/BetterThanTomorrow/calva/issues/3059)
910
- Fix: [paredit.insertSemiColon can break structure before closing delimiter](https://github.com/BetterThanTomorrow/calva/issues/3061)

src/evaluate-utils.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export type EvaluateAsCommentOptions = {
2+
commentStyle: string;
3+
};
4+
5+
/**
6+
* Normalize the Calva Evaluate Selection as Comment arguments.
7+
*
8+
* The context menu handler passes the document/context object as the first
9+
* parameter, while palette/shortcut invocations pass the options bag first.
10+
* To handle both, we inspect the first argument for a `commentStyle` key.
11+
*/
12+
export function normalizeEvaluateAsCommentArgs(
13+
documentOrOptions,
14+
options: EvaluateAsCommentOptions = { commentStyle: 'line' }
15+
): {
16+
options: EvaluateAsCommentOptions;
17+
document: unknown;
18+
} {
19+
const opt1CommentStyle = documentOrOptions?.commentStyle;
20+
return opt1CommentStyle
21+
? {
22+
document: options,
23+
options: documentOrOptions as EvaluateAsCommentOptions,
24+
}
25+
: {
26+
document: documentOrOptions,
27+
options: options.commentStyle ? options : { ...options, commentStyle: 'line' },
28+
};
29+
}

src/evaluate.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import * as inspector from './providers/inspector';
2121
import { resultAsComment } from './util/string-result';
2222
import { highlight } from './highlight/src/extension';
2323
import * as flareHandler from './flare-handler';
24+
import { normalizeEvaluateAsCommentArgs } from './evaluate-utils';
2425

2526
let inspectorDataProvider: inspector.InspectorDataProvider;
2627

@@ -415,11 +416,12 @@ function validateCommentStyle(commentStyle: string) {
415416
}
416417

417418
function evaluateSelectionAsComment(options = { commentStyle: 'line' }, document = {}) {
418-
validateCommentStyle(options.commentStyle);
419+
const normalized = normalizeEvaluateAsCommentArgs(document, options);
420+
validateCommentStyle(normalized.options.commentStyle);
419421
if (util.getConnectedState()) {
420422
evaluateSelection(
421-
document,
422-
Object.assign({}, options, {
423+
normalized.document,
424+
Object.assign({}, normalized.options, {
423425
comment: true,
424426
pprintOptions: getConfig().prettyPrintingOptions,
425427
selectionFn: _currentSelectionElseCurrentForm,
@@ -431,11 +433,12 @@ function evaluateSelectionAsComment(options = { commentStyle: 'line' }, document
431433
}
432434

433435
function evaluateTopLevelFormAsComment(options = { commentStyle: 'line' }, document = {}) {
434-
validateCommentStyle(options.commentStyle);
436+
const normalized = normalizeEvaluateAsCommentArgs(document, options);
437+
validateCommentStyle(normalized.options.commentStyle);
435438
if (util.getConnectedState()) {
436439
evaluateSelection(
437-
document,
438-
Object.assign({}, options, {
440+
normalized.document,
441+
Object.assign({}, normalized.options, {
439442
comment: true,
440443
pprintOptions: getConfig().prettyPrintingOptions,
441444
selectionFn: _currentTopLevelFormText,
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import * as expect from 'expect';
2+
import { normalizeEvaluateAsCommentArgs } from '../../evaluate-utils';
3+
4+
describe('normalizeEvaluateAsCommentArgs', () => {
5+
it('normalizes command invocation (options first)', () => {
6+
const documentArg = { some: 'document-context' };
7+
const normalized = normalizeEvaluateAsCommentArgs(
8+
{ commentStyle: 'ignore' },
9+
documentArg as any
10+
);
11+
12+
expect(normalized.options.commentStyle).toBe('ignore');
13+
expect(normalized.document).toBe(documentArg);
14+
});
15+
16+
it('normalizes context menu invocation (document first)', () => {
17+
const documentArg = { some: 'document-context' };
18+
const normalized = normalizeEvaluateAsCommentArgs(documentArg, {
19+
commentStyle: 'rcf',
20+
});
21+
22+
expect(normalized.document).toBe(documentArg);
23+
expect(normalized.options.commentStyle).toBe('rcf');
24+
});
25+
26+
it('defaults comment style to line when context menu invocation omits options', () => {
27+
const documentArg = { some: 'document-context' };
28+
const normalized = normalizeEvaluateAsCommentArgs(documentArg);
29+
30+
expect(normalized.document).toBe(documentArg);
31+
expect(normalized.options.commentStyle).toBe('line');
32+
});
33+
});

0 commit comments

Comments
 (0)