Skip to content

Commit

Permalink
fix method extraction refactoring
Browse files Browse the repository at this point in the history
SQUASHED: AUTO-COMMIT-demos-javascript-a.js,AUTO-COMMIT-src-components-widgets-lively-code-mirror-context-menu.js,AUTO-COMMIT-src-components-widgets-lively-code-mirror-context-menu.js.l4a,AUTO-COMMIT-test.html,
  • Loading branch information
JensLincke committed Jun 18, 2024
1 parent 5f78155 commit 38d73ac
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 56 deletions.
20 changes: 19 additions & 1 deletion demos/javascript/a.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
// a.js
"disable deepeval"
export const value = 44;
export const value = 44;


function hello2() {
var a = 3 + 4
}

hello2()



class Foo {


hello2() {
var a = 3 + 4
}

}
126 changes: 72 additions & 54 deletions src/components/widgets/lively-code-mirror-context-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,31 @@ export default async function openMenu(astCapabilities, codeMirror, livelyCodeMi
function fa(name, ...modifiers) {
return `<i class="fa fa-${name} ${modifiers.map(m => 'fa-' + m).join(' ')}"></i>`;
}

/*MD ### Generate Submenus MD*/

async function generateGenerationSubmenu() {

// for now, classes can be generated everywhere
// if this isn't wanted anymore, scope checks can be done as can be seen below
let submenu = [['Class', () => {
let submenu = [
['Class', () => {
menu.remove();
astCapabilities.generateClass();
}, '→', fa('suitcase')]];
}, '→', fa('suitcase')]
];

const selectedPath = astCapabilities.getInnermostPathContainingSelection(astCapabilities.programPath,
astCapabilities.firstSelection);

const selectedPath = astCapabilities.getInnermostPathContainingSelection(astCapabilities.programPath, astCapabilities.firstSelection);

//add testcase if in describe
if (astCapabilities.isInDescribe(selectedPath)) {
submenu.unshift(['Testcase', () => {
menu.remove();
astCapabilities.generateTestCase();
}, '→', fa('suitcase')]);
}

//add getter / setter if directly in ClassBody or ObjectExpression
if (astCapabilities.isDirectlyIn(["ClassBody", "ObjectExpression"], selectedPath)) {
submenu.push(['Getter', () => {
Expand Down Expand Up @@ -63,60 +66,75 @@ export default async function openMenu(astCapabilities, codeMirror, livelyCodeMi

/*MD ### Generate Factoring Menu MD*/

const menuItems = [['selection to local variable', () => {
menu.remove();
astCapabilities.extractExpressionIntoLocalVariable();
}, '→', fa('share-square-o', 'flip-horizontal')], ['inline variable', () => {
menu.remove();
astCapabilities.inlineLocalVariable();
}, '→', fa('external-link', 'flip-vertical')], ['wrap into active expression', () => {
menu.remove();
astCapabilities.wrapExpressionIntoActiveExpression();
}, '→', fa('suitcase')], ['Rename', () => {
menu.remove();
astCapabilities.rename();
}, '→', fa('suitcase')], ['Swap then and else of conditional', () => {
menu.remove();
astCapabilities.rename();
}, 'swap', fa('suitcase')], ['Extract Method', () => {
menu.remove();
astCapabilities.swapConditional()
}, 'Alt+M', fa('suitcase'), {
onSelect: () => {
const selection = astCapabilities.selectMethodExtraction(astCapabilities.programPath, true);
if (selection) {
openMenu.changedSelectionInMenu = true;
astCapabilities.selectPaths(selection.selectedPaths);
} else {
openMenu.changedSelectionInMenu = false;
}
},
onDeselect: () => {
if (openMenu.changedSelectionInMenu) {
codeMirror.undoSelection();
const menuItems = [
['selection to local variable', () => {
menu.remove();
astCapabilities.extractExpressionIntoLocalVariable();
}, '→', fa('share-square-o', 'flip-horizontal')],
['inline variable', () => {
menu.remove();
astCapabilities.inlineLocalVariable();
}, '→', fa('external-link', 'flip-vertical')],
['wrap into active expression', () => {
menu.remove();
astCapabilities.wrapExpressionIntoActiveExpression();
}, '→', fa('suitcase')],
['Rename', () => {
menu.remove();
astCapabilities.rename();
}, '→', fa('suitcase')],
['Swap then and else of conditional', () => {
menu.remove();
astCapabilities.swapConditional();
}, 'swap', fa('suitcase')],
['Extract Method', () => {
menu.remove();
astCapabilities.extractMethod()
}, 'Alt+M', fa('suitcase'), {
onSelect: () => {
debugger
const selection = astCapabilities.selectMethodExtraction(astCapabilities.programPath, true);
if (selection) {
openMenu.changedSelectionInMenu = true;
astCapabilities.selectPaths(selection.selectedPaths);
} else {
openMenu.changedSelectionInMenu = false;
}
},
onDeselect: () => {
debugger
if (openMenu.changedSelectionInMenu) {
codeMirror.undoSelection();
}
}
}}], ['Generate HTML Accessors', () => {
}],
['Generate HTML Accessors', () => {
menu.remove();
astCapabilities.generateHTMLAccessors();
}, 'Alt+H', fa('suitcase')],
['Print References', () => {
astCapabilities.printAllBindings();
menu.remove();
}, 'Alt+I', fa('suitcase')],
[
'lively', [['lively.notify', () => {
menu.remove();
astCapabilities.livelyNotify();
}, '+', fa('plus')],
['lively4url', () => {
menu.remove();
astCapabilities.lively4url();
}, '+', fa('plus')],
]],
['Generate', generateGenerationSubmenu()], ['Import', generateImportSubmenu()]];
['Print References', () => {
astCapabilities.printAllBindings();
menu.remove();
}, 'Alt+I', fa('suitcase')],
[
'lively', [
['lively.notify', () => {
menu.remove();
astCapabilities.livelyNotify();
}, '+', fa('plus')],
['lively4url', () => {
menu.remove();
astCapabilities.lively4url();
}, '+', fa('plus')],
]
],
['Generate', generateGenerationSubmenu()],
['Import', generateImportSubmenu()]
];
var menuPosition = codeMirror.cursorCoords(false, "window");

const menu = await ContextMenu.openIn(document.body, { clientX: menuPosition.left, clientY: menuPosition.bottom }, undefined, document.body, menuItems);
const menu = await ContextMenu.openIn(document.body, { clientX: menuPosition.left, clientY: menuPosition.bottom },
undefined, document.body, menuItems);
menu.addEventListener("DOMNodeRemoved", () => {
livelyCodeMirror.focus();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"type":"Reference","version":"96c12edb6cf8db2fa65783aac87327d17261d16c","content":"import ContextMenu from 'src/client/contextmenu.js';\n\nexport default async function openMenu(astCapabilities, codeMirror, livelyCodeMirror) {\n\n function fa(name, ...modifiers) {\n return `<i class=\"fa fa-${name} ${modifiers.map(m => 'fa-' + m).join(' ')}\"></i>`;\n }\n \n /*MD ### Generate Submenus MD*/\n\n async function generateGenerationSubmenu() {\n\n // for now, classes can be generated everywhere\n // if this isn't wanted anymore, scope checks can be done as can be seen below\n let submenu = [['Class', () => {\n menu.remove();\n astCapabilities.generateClass();\n }, '→', fa('suitcase')]];\n\n const selectedPath = astCapabilities.getInnermostPathContainingSelection(astCapabilities.programPath, astCapabilities.firstSelection);\n \n //add testcase if in describe\n if (astCapabilities.isInDescribe(selectedPath)) {\n submenu.unshift(['Testcase', () => {\n menu.remove();\n astCapabilities.generateTestCase();\n }, '→', fa('suitcase')]);\n }\n \n //add getter / setter if directly in ClassBody or ObjectExpression\n if (astCapabilities.isDirectlyIn([\"ClassBody\", \"ObjectExpression\"], selectedPath)) {\n submenu.push(['Getter', () => {\n menu.remove();\n astCapabilities.generateGetter();\n }, '→', fa('suitcase')], ['Setter', () => {\n menu.remove();\n astCapabilities.generateSetter();\n }, '→', fa('suitcase')]);\n }\n\n return submenu;\n }\n\n async function generateImportSubmenu() {\n let { identName, functions, classes } = await astCapabilities.findImports();\n let submenu = [];\n if (!identName || functions.length == 0 && classes.length == 0) {\n submenu.push(['none', () => {\n menu.remove();\n }, '', '']);\n } else {\n functions.forEach(url => submenu.push([url.replace(lively4url, ''), () => {\n menu.remove();\n astCapabilities.addImport(url, identName, true);\n }, '-', fa('share-square-o')]));\n classes.forEach(cl => submenu.push([cl.name + \", \" + cl.url.replace(lively4url, ''), () => {\n menu.remove();\n astCapabilities.addImport(cl.url, cl.name, false);\n }, '-', fa('share-square-o')]));\n }\n return submenu;\n }\n\n /*MD ### Generate Factoring Menu MD*/\n\n const menuItems = [['selection to local variable', () => {\n menu.remove();\n astCapabilities.extractExpressionIntoLocalVariable();\n }, '→', fa('share-square-o', 'flip-horizontal')], ['inline variable', () => {\n menu.remove();\n astCapabilities.inlineLocalVariable();\n }, '→', fa('external-link', 'flip-vertical')], ['wrap into active expression', () => {\n menu.remove();\n astCapabilities.wrapExpressionIntoActiveExpression();\n }, '→', fa('suitcase')], ['Rename', () => {\n menu.remove();\n astCapabilities.rename();\n }, '→', fa('suitcase')], ['Swap then and else of conditional', () => {\n menu.remove();\n astCapabilities.rename();\n }, 'swap', fa('suitcase')], ['Extract Method', () => {\n menu.remove();\n astCapabilities.swapConditional()\n }, 'Alt+M', fa('suitcase'), {\n onSelect: () => {\n const selection = astCapabilities.selectMethodExtraction(astCapabilities.programPath, true);\n if (selection) {\n openMenu.changedSelectionInMenu = true;\n astCapabilities.selectPaths(selection.selectedPaths);\n } else {\n openMenu.changedSelectionInMenu = false;\n }\n },\n onDeselect: () => {\n if (openMenu.changedSelectionInMenu) {\n codeMirror.undoSelection();\n }\n }}], ['Generate HTML Accessors', () => {\n menu.remove();\n astCapabilities.generateHTMLAccessors();\n }, 'Alt+H', fa('suitcase')],\n ['Print References', () => {\n astCapabilities.printAllBindings();\n menu.remove();\n }, 'Alt+I', fa('suitcase')],\n [\n 'lively', [['lively.notify', () => {\n menu.remove();\n astCapabilities.livelyNotify();\n }, '+', fa('plus')],\n ['lively4url', () => {\n menu.remove();\n astCapabilities.lively4url();\n }, '+', fa('plus')],\n ]],\n ['Generate', generateGenerationSubmenu()], ['Import', generateImportSubmenu()]];\n var menuPosition = codeMirror.cursorCoords(false, \"window\");\n\n const menu = await ContextMenu.openIn(document.body, { clientX: menuPosition.left, clientY: menuPosition.bottom }, undefined, document.body, menuItems);\n menu.addEventListener(\"DOMNodeRemoved\", () => {\n livelyCodeMirror.focus();\n });\n}\n"}
{"type":"Reference","version":"31c8137e1c3b60538c7788c7ec5a23760d40b0c7","content":"import ContextMenu from 'src/client/contextmenu.js';\n\nexport default async function openMenu(astCapabilities, codeMirror, livelyCodeMirror) {\n\n function fa(name, ...modifiers) {\n return `<i class=\"fa fa-${name} ${modifiers.map(m => 'fa-' + m).join(' ')}\"></i>`;\n }\n\n /*MD ### Generate Submenus MD*/\n\n async function generateGenerationSubmenu() {\n\n // for now, classes can be generated everywhere\n // if this isn't wanted anymore, scope checks can be done as can be seen below\n let submenu = [\n ['Class', () => {\n menu.remove();\n astCapabilities.generateClass();\n }, '→', fa('suitcase')]\n ];\n\n const selectedPath = astCapabilities.getInnermostPathContainingSelection(astCapabilities.programPath,\n astCapabilities.firstSelection);\n\n //add testcase if in describe\n if (astCapabilities.isInDescribe(selectedPath)) {\n submenu.unshift(['Testcase', () => {\n menu.remove();\n astCapabilities.generateTestCase();\n }, '→', fa('suitcase')]);\n }\n\n //add getter / setter if directly in ClassBody or ObjectExpression\n if (astCapabilities.isDirectlyIn([\"ClassBody\", \"ObjectExpression\"], selectedPath)) {\n submenu.push(['Getter', () => {\n menu.remove();\n astCapabilities.generateGetter();\n }, '→', fa('suitcase')], ['Setter', () => {\n menu.remove();\n astCapabilities.generateSetter();\n }, '→', fa('suitcase')]);\n }\n\n return submenu;\n }\n\n async function generateImportSubmenu() {\n let { identName, functions, classes } = await astCapabilities.findImports();\n let submenu = [];\n if (!identName || functions.length == 0 && classes.length == 0) {\n submenu.push(['none', () => {\n menu.remove();\n }, '', '']);\n } else {\n functions.forEach(url => submenu.push([url.replace(lively4url, ''), () => {\n menu.remove();\n astCapabilities.addImport(url, identName, true);\n }, '-', fa('share-square-o')]));\n classes.forEach(cl => submenu.push([cl.name + \", \" + cl.url.replace(lively4url, ''), () => {\n menu.remove();\n astCapabilities.addImport(cl.url, cl.name, false);\n }, '-', fa('share-square-o')]));\n }\n return submenu;\n }\n\n /*MD ### Generate Factoring Menu MD*/\n\n const menuItems = [\n ['selection to local variable', () => {\n menu.remove();\n astCapabilities.extractExpressionIntoLocalVariable();\n }, '→', fa('share-square-o', 'flip-horizontal')],\n ['inline variable', () => {\n menu.remove();\n astCapabilities.inlineLocalVariable();\n }, '→', fa('external-link', 'flip-vertical')],\n ['wrap into active expression', () => {\n menu.remove();\n astCapabilities.wrapExpressionIntoActiveExpression();\n }, '→', fa('suitcase')],\n ['Rename', () => {\n menu.remove();\n astCapabilities.rename();\n }, '→', fa('suitcase')],\n ['Swap then and else of conditional', () => {\n menu.remove();\n astCapabilities.swapConditional();\n }, 'swap', fa('suitcase')],\n ['Extract Method', () => {\n menu.remove();\n astCapabilities.extractMethod()\n }, 'Alt+M', fa('suitcase'), {\n onSelect: () => {\n debugger\n const selection = astCapabilities.selectMethodExtraction(astCapabilities.programPath, true);\n if (selection) {\n openMenu.changedSelectionInMenu = true;\n astCapabilities.selectPaths(selection.selectedPaths);\n } else {\n openMenu.changedSelectionInMenu = false;\n }\n },\n onDeselect: () => {\n debugger\n if (openMenu.changedSelectionInMenu) {\n codeMirror.undoSelection();\n }\n }\n }],\n ['Generate HTML Accessors', () => {\n menu.remove();\n astCapabilities.generateHTMLAccessors();\n }, 'Alt+H', fa('suitcase')],\n ['Print References', () => {\n astCapabilities.printAllBindings();\n menu.remove();\n }, 'Alt+I', fa('suitcase')],\n [\n 'lively', [\n ['lively.notify', () => {\n menu.remove();\n astCapabilities.livelyNotify();\n }, '+', fa('plus')],\n ['lively4url', () => {\n menu.remove();\n astCapabilities.lively4url();\n }, '+', fa('plus')],\n ]\n ],\n ['Generate', generateGenerationSubmenu()],\n ['Import', generateImportSubmenu()]\n ];\n var menuPosition = codeMirror.cursorCoords(false, \"window\");\n\n const menu = await ContextMenu.openIn(document.body, { clientX: menuPosition.left, clientY: menuPosition.bottom },\n undefined, document.body, menuItems);\n menu.addEventListener(\"DOMNodeRemoved\", () => {\n livelyCodeMirror.focus();\n });\n}\n"}

0 comments on commit 38d73ac

Please sign in to comment.