Skip to content

Commit 2077eb3

Browse files
committed
Record location of executed code
1 parent 97edcbb commit 2077eb3

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

src/vs/workbench/contrib/positronConsole/browser/positronConsoleActions.ts

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { URI } from '../../../../base/common/uri.js';
88
import { isString, assertType } from '../../../../base/common/types.js';
99
import { Codicon } from '../../../../base/common/codicons.js';
1010
import { ITextModel } from '../../../../editor/common/model.js';
11-
import { IRange } from '../../../../editor/common/core/range.js';
11+
import { IRange, Range } from '../../../../editor/common/core/range.js';
1212
import { IEditor } from '../../../../editor/common/editorCommon.js';
1313
import { ILogService } from '../../../../platform/log/common/log.js';
1414
import { IModelService } from '../../../../editor/common/services/model.js';
@@ -24,7 +24,7 @@ import { Action2, MenuId, registerAction2 } from '../../../../platform/actions/c
2424
import { IViewsService } from '../../../services/views/common/viewsService.js';
2525
import { ServicesAccessor } from '../../../../platform/instantiation/common/instantiation.js';
2626
import { IEditorService } from '../../../services/editor/common/editorService.js';
27-
import { IStatementRange, StatementRangeProvider } from '../../../../editor/common/languages.js';
27+
import { IStatementRange, StatementRangeProvider, Location } from '../../../../editor/common/languages.js';
2828
import { KeybindingWeight } from '../../../../platform/keybinding/common/keybindingsRegistry.js';
2929
import { ILanguageFeaturesService } from '../../../../editor/common/services/languageFeatures.js';
3030
import { INotificationService, Severity } from '../../../../platform/notification/common/notification.js';
@@ -79,8 +79,8 @@ const trimNewlines = (str: string) => str.replace(/^\n+|\n+$/g, '');
7979
*/
8080
async function executeCodeInConsole(
8181
code: string,
82-
position: Position,
83-
model: ITextModel,
82+
cursorLocation: Location,
83+
codeLocation: Location | undefined,
8484
services: {
8585
editorService: IEditorService;
8686
languageService: ILanguageService;
@@ -114,11 +114,8 @@ async function executeCodeInConsole(
114114
const attribution: IConsoleCodeAttribution = {
115115
source: CodeAttributionSource.Script,
116116
metadata: {
117-
file: model.uri.path,
118-
position: {
119-
line: position.lineNumber,
120-
column: position.column
121-
},
117+
cursorLocation,
118+
codeLocation,
122119
}
123120
};
124121

@@ -387,10 +384,14 @@ export function registerPositronConsoleActions() {
387384
// Get the code to execute.
388385
const selection = editor?.getSelection();
389386

387+
// Track the source and range of the executed code
388+
let codeLocation: Location | undefined = undefined;
389+
390390
// If we have a selection and it isn't empty, then we use its contents (even if it
391391
// only contains whitespace or comments) and also retain the user's selection location.
392392
if (selection && !selection.isEmpty()) {
393393
code = model.getValueInRange(selection);
394+
codeLocation = { uri: model.uri, range: selection };
394395
// HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK
395396
// This attempts to address https://github.com/posit-dev/positron/issues/1177
396397
// by tacking a newline onto multiline, indented Python code fragments. This allows
@@ -429,6 +430,7 @@ export function registerPositronConsoleActions() {
429430
// range provider returns, even if it is an empty string, as it should have
430431
// returned `undefined` if it didn't think it was important.
431432
code = isString(statementRange.code) ? statementRange.code : model.getValueInRange(statementRange.range);
433+
codeLocation = { uri: model.uri, range: statementRange.range };
432434

433435
if (advance) {
434436
nextPosition = await this.advanceStatement(model, editor, statementRange, statementRangeProviders[0], logService);
@@ -486,11 +488,16 @@ export function registerPositronConsoleActions() {
486488
}
487489
}
488490

491+
const cursorLocation: Location = {
492+
uri: model.uri,
493+
range: Range.fromPositions(position, position),
494+
};
495+
489496
// Use the helper function to execute the code
490497
await executeCodeInConsole(
491498
code,
492-
position,
493-
model,
499+
cursorLocation,
500+
codeLocation,
494501
{
495502
editorService,
496503
languageService,
@@ -770,11 +777,16 @@ export function registerPositronConsoleActions() {
770777
return;
771778
}
772779

780+
const cursorLocation: Location = {
781+
uri: model.uri,
782+
range: Range.fromPositions(position, position),
783+
};
784+
773785
// Use the helper function to execute the code
774786
await executeCodeInConsole(
775787
code,
776-
position,
777-
model,
788+
cursorLocation,
789+
undefined,
778790
{
779791
editorService,
780792
languageService,

0 commit comments

Comments
 (0)