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

create a new line after execute the last line (#6) #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
65 changes: 52 additions & 13 deletions src/run-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,58 @@ export function runInferredCodeBlock(): void {
}

export function runInferredCodeBlockAndMoveDown(): void {
const textEditor = vscode.window.activeTextEditor;
if (!textEditor) {
return;
}

const expandedCodeRange = inferCodeBlock(textEditor);
const inferredBlockText = textEditor.document.getText(expandedCodeRange);
executeText(inferredBlockText);

const endPosition = new Position(expandedCodeRange.end.line + 1, 0);
const newSelection = new Selection(endPosition, endPosition);
setSelectionAndMoveDown(textEditor, newSelection);
const textEditor = vscode.window.activeTextEditor;
if (!textEditor) {
return;
}
const expandedCodeRange = inferCodeBlock(textEditor);
const inferredBlockText = textEditor.document.getText(expandedCodeRange);
console.log("inferredBlockText", inferredBlockText);
var empty_line_count = 0;
for(var i = expandedCodeRange.end.line; i >= expandedCodeRange.start.line; i--){
if(textEditor.document.lineAt(i).text.match(/^\s*$/)){
empty_line_count += 1;
}
else{
break;
}
}
console.log("empty_line_count", empty_line_count);
executeText(inferredBlockText);
//add a new line to the end of the code block
var endPosition;
var newSelection;
if(textEditor.document.lineCount <= expandedCodeRange.end.line + 1){
console.log("the last block")
if(expandedCodeRange.start.line == expandedCodeRange.end.line-empty_line_count){
console.log("the last block is a single line")
if(empty_line_count == 0){
console.log("need to add a new line")
vscode.commands.executeCommand("editor.action.insertLineAfter");
}
endPosition = new Position(expandedCodeRange.end.line + 1 - empty_line_count, 0);
newSelection = new Selection(endPosition, endPosition);
setSelectionAndMoveDown(textEditor, newSelection);
}
else{
console.log("the last block is a multi-line block")
endPosition = new Position(expandedCodeRange.end.line + 1 - empty_line_count, 0);
newSelection = new Selection(endPosition, endPosition);
setSelectionAndMoveDown(textEditor, newSelection);
if(empty_line_count == 0){
console.log("need to add a new line")
endPosition = new Position(expandedCodeRange.end.line + 1, 0);
newSelection = new Selection(endPosition, endPosition);
vscode.commands.executeCommand("editor.action.insertLineAfter");
setSelectionAndMoveDown(textEditor, newSelection);
}
}
}
else {
endPosition = new Position(expandedCodeRange.end.line + 1, 0);
newSelection = new Selection(endPosition, endPosition);
setSelectionAndMoveDown(textEditor, newSelection);
}
}

function inferCodeBlock(textEditor: TextEditor): Range {
Expand Down Expand Up @@ -131,4 +171,3 @@ function expandRangeDownward(
console.log("endRange", endRange);
return endRange;
}