Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REPL Smart Shift+Enter and Dynamic Smart Cursor Cherry Pick #21976

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion pythonFiles/normalizeSelection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@

import ast
import json
import os
import pathlib
import re
import sys
import textwrap

script_dir = pathlib.Path('User/anthonykim/Desktop/vscode-python/pythonFiles/lib/python')
sys.path.append(os.fspath(script_dir))
import debugpy

debugpy.connect(5678)
debugpy.breakpoint()
def split_lines(source):
"""
Split selection lines in a version-agnostic way.
Expand Down Expand Up @@ -125,6 +132,24 @@ def normalize_lines(selection):

return source

top_level_nodes = [] # collection of top level nodes

class file_node_visitor(ast.NodeVisitor):
def visit_nodes(self, node):
top_level_nodes.append(node)
self.generic_visit(node)

def traverse_file(wholeFileContent):
# use ast module to parse content of the file
parsed_file_content = ast.parse(wholeFileContent)
file_node_visitor().visit(parsed_file_content)

for node in ast.iter_child_nodes(parsed_file_content):
top_level_nodes.append(node)
line_start = node.lineno
line_end = node.end_lineno
code_of_node = ast.get_source
# ast.get_source_segment(wholeFileContent, node) This is way to get original code of the selected node

if __name__ == "__main__":
# Content is being sent from the extension as a JSON object.
Expand All @@ -134,7 +159,9 @@ def normalize_lines(selection):
contents = json.loads(raw.decode("utf-8"))

normalized = normalize_lines(contents["code"])

normalized_whole_file = normalize_lines(contents["wholeFileContent"])
traverse_file(contents["wholeFileContent"]) # traverse file
file_node_visitor().visit(ast.parse(contents["wholeFileContent"]))
# Send the normalized code back to the extension in a JSON object.
data = json.dumps({"normalized": normalized})

Expand Down
3 changes: 2 additions & 1 deletion src/client/terminals/codeExecution/codeExecutionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ export class CodeExecutionManager implements ICodeExecutionManager {
}
const codeExecutionHelper = this.serviceContainer.get<ICodeExecutionHelper>(ICodeExecutionHelper);
const codeToExecute = await codeExecutionHelper.getSelectedTextToExecute(activeEditor!);
const normalizedCode = await codeExecutionHelper.normalizeLines(codeToExecute!);
const wholeFileContent = activeEditor!.document.getText();
const normalizedCode = await codeExecutionHelper.normalizeLines(codeToExecute!, wholeFileContent);
if (!normalizedCode || normalizedCode.trim().length === 0) {
return;
}
Expand Down
5 changes: 3 additions & 2 deletions src/client/terminals/codeExecution/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class CodeExecutionHelper implements ICodeExecutionHelper {
this.interpreterService = serviceContainer.get<IInterpreterService>(IInterpreterService);
}

public async normalizeLines(code: string, resource?: Uri): Promise<string> {
public async normalizeLines(code: string, wholeFileContent: string, resource?: Uri): Promise<string> {
try {
if (code.trim().length === 0) {
return '';
Expand Down Expand Up @@ -66,7 +66,7 @@ export class CodeExecutionHelper implements ICodeExecutionHelper {

// The normalization script expects a serialized JSON object, with the selection under the "code" key.
// We're using a JSON object so that we don't have to worry about encoding, or escaping non-ASCII characters.
const input = JSON.stringify({ code });
const input = JSON.stringify({ code, wholeFileContent });
observable.proc?.stdin?.write(input);
observable.proc?.stdin?.end();

Expand Down Expand Up @@ -110,6 +110,7 @@ export class CodeExecutionHelper implements ICodeExecutionHelper {

const { selection } = textEditor;
let code: string;
const wholeFileContent = textEditor.document.getText(); // This is a way to get the whole text content from the user
if (selection.isEmpty) {
code = textEditor.document.lineAt(selection.start.line).text;
} else if (selection.isSingleLine) {
Expand Down
2 changes: 1 addition & 1 deletion src/client/terminals/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface ICodeExecutionService {
export const ICodeExecutionHelper = Symbol('ICodeExecutionHelper');

export interface ICodeExecutionHelper {
normalizeLines(code: string): Promise<string>;
normalizeLines(code: string, wholeFileContent: string): Promise<string>;
getFileToExecute(): Promise<Uri | undefined>;
saveFileIfDirty(file: Uri): Promise<Resource>;
getSelectedTextToExecute(textEditor: TextEditor): Promise<string | undefined>;
Expand Down