Skip to content

Commit

Permalink
Refactor: Extract into functions
Browse files Browse the repository at this point in the history
  • Loading branch information
micoloth committed Feb 28, 2024
1 parent 7fa019b commit bd6e1c5
Showing 1 changed file with 59 additions and 56 deletions.
115 changes: 59 additions & 56 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ async function getNotebookAndKernel(globalState: Map<string, string>, editor: Te
const editorToIWKey = (editorUri: string) => 'editorToIWKey' + editorUri;
const editorToKernelKey = (editorUri: string) => 'editorToIWKey' + editorUri;
const editorConnectionStateKey = (editorUri: string) => 'state' + editorUri;
const editorRebuildPendingKey = (editorUri: string) => 'rebuildPending' + editorUri;


type CachedNotebookDocument = { cellCount: number, uri: Uri };
Expand Down Expand Up @@ -1035,6 +1036,60 @@ function createPrepareEnvironementAndComputeAction(config: {



function getOnDidChangeTextEditorSelectionAction(globalState: Map<string, string>, output: OutputChannel, codelensProvider: CellCodelensProvider) {
return async (event: vscode.TextEditorSelectionChangeEvent): Promise<void> => {
let editor = window.activeTextEditor;
if (event.textEditor &&
editor &&
event.textEditor.document === editor.document &&
editor.selection.isEmpty &&
getState(globalState, editor) == State.extension_available) {
const current_ranges = await getCurrentRangesFromPython(editor, output, globalState, {
rebuild: false
});
if (current_ranges) {
updateDecorations(editor, current_ranges);
}
else {
updateDecorations(event.textEditor, []);
}
let codelense_range = current_ranges ? current_ranges.filter((r) => (r.current && r.state != 'syntaxerror')).map((r) => r.range) : [];
codelensProvider.change_range(codelense_range.length > 0 ? codelense_range[0] : undefined);
} else {
updateDecorations(event.textEditor, []);
codelensProvider.change_range(undefined);
}
};
}

function getOnDidChangeTextDocumentAction(globalState: Map<string, string>, output: OutputChannel): (e: vscode.TextDocumentChangeEvent) => any {
return async (event) => {
let editor = window.activeTextEditor;
if (editor && event.document === editor.document) {
if (getState(globalState, editor) !== State.extension_available) { return; }
const current_ranges = await getCurrentRangesFromPython(editor, output, globalState, {
rebuild: true
});
if (!current_ranges) return;
await updateDecorations(editor, current_ranges);
}
};
}

function getOnDidChangeActiveTextEditorAction(globalState: Map<string, string>, output: OutputChannel): (e: TextEditor | undefined) => any {
return async (editor: TextEditor | undefined) => {
if (editor) {
if (getState(globalState, editor) !== State.extension_available) { return; }
const current_ranges = await getCurrentRangesFromPython(editor, output, globalState, { rebuild: true });
if (!current_ranges) return;
await updateDecorations(editor, current_ranges);
}
};
}




////////////////////////////////////////////////////////////////////////////////////////////////////
// ACTIVATION
////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1110,66 +1165,14 @@ async function defineAllCommands(context: ExtensionContext, output: OutputChanne

///////// Document Highlights: ///////////////////////

window.onDidChangeActiveTextEditor(
async (editor) => {
if (editor) {
if (getState(globalState, editor) !== State.extension_available) { return; }
const current_ranges = await getCurrentRangesFromPython(editor, output, globalState, { rebuild: true });
if (!current_ranges) return;
await updateDecorations(editor, current_ranges);
}
},
null,
Context.subscriptions
);
workspace.onDidChangeTextDocument(
async (event) => {
let editor = window.activeTextEditor;
if (editor && event.document === editor.document) {
if (getState(globalState, editor) !== State.extension_available) { return; }
const current_ranges = await getCurrentRangesFromPython(editor, output, globalState, {
rebuild: true
});
if (!current_ranges) return;
await updateDecorations(editor, current_ranges);
}
},
null,
Context.subscriptions
);
window.onDidChangeTextEditorSelection(
async (event) => {
let editor = window.activeTextEditor;
if (
event.textEditor &&
editor &&
event.textEditor.document === editor.document &&
editor.selection.isEmpty &&
getState(globalState, editor) == State.extension_available
) {
const current_ranges = await getCurrentRangesFromPython(editor, output, globalState, {
rebuild: false
});
if (current_ranges) {
updateDecorations(editor, current_ranges);
}
else{
updateDecorations(event.textEditor, []);
}
let codelense_range = current_ranges ? current_ranges.filter((r) => (r.current && r.state != 'syntaxerror')).map((r) => r.range) : [];
codelensProvider.change_range(codelense_range.length > 0 ? codelense_range[0] : undefined);
} else {
updateDecorations(event.textEditor, []);
codelensProvider.change_range(undefined);
}
},
null,
Context.subscriptions
);
workspace.onDidChangeTextDocument( getOnDidChangeTextDocumentAction(globalState, output), null, Context.subscriptions );
window.onDidChangeActiveTextEditor( getOnDidChangeActiveTextEditorAction(globalState, output), null, Context.subscriptions );
window.onDidChangeTextEditorSelection( getOnDidChangeTextEditorSelectionAction(globalState, output, codelensProvider), null, Context.subscriptions );
}




////////////////////////////////////////////////////////////////////////////////////////////////////
// NOTES
////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit bd6e1c5

Please sign in to comment.