Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
edublancas committed Sep 5, 2024
2 parents 4623791 + 07fac97 commit 33119fd
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 9 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# CHANGELOG

## 0.4.4

* Fixes error that caused the `Format SQL` bitton to appear even when disabled in the settings

## 0.4.3

* Trigger autocomplete only when the cell begins with `%sql` or `%%sql`

## 0.4.2

* Removed `Share notebook` button
Expand Down
18 changes: 18 additions & 0 deletions jupysql_plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,21 @@

def _jupyter_labextension_paths():
return [{"src": "labextension", "dest": "jupysql-plugin"}]


def _jupyter_server_extension_points():
return [{"module": "jupysql_plugin"}]


def _load_jupyter_server_extension(serverapp):
"""
This function is called when the extension is loaded.
Parameters
----------
server_app: jupyterlab.labapp.LabApp
JupyterLab application instance
"""
serverapp.log.info(f"Registered {_module_name} server extension")


load_jupyter_server_extension = _load_jupyter_server_extension
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jupysql-plugin",
"version": "0.4.2",
"version": "0.4.4",
"description": "Jupyterlab extension for JupySQL",
"private": true,
"keywords": [
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
jupyterlab>=4,<4.0.12
jupyterlab>=4
build
twine
hatch
Expand Down
17 changes: 16 additions & 1 deletion src/completer/customconnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import {

import { keywords } from './keywords.json';

const CELL_MAGIC = '%%sql';
const LINE_MAGIC = '%sql';

/**
* A custom connector for completion handlers.
*/
Expand All @@ -31,7 +34,19 @@ export class SQLCompleterProvider implements ICompletionProvider {
* @param context - additional information about context of completion request
*/
async isApplicable(context: ICompletionContext): Promise<boolean> {
return true;
const editor = context.editor;
if (editor === undefined)
return false;

// If this is a SQL magic cell, then we can complete
const firstLine = editor.getLine(0);
if (firstLine.slice(0, CELL_MAGIC.length) === CELL_MAGIC)
return true;

// Otherwise, if we're to the right of a line magic, we can complete
const currPos = editor.getCursorPosition();
const lineMagicPos = editor.getLine(currPos.line).indexOf(LINE_MAGIC);
return (lineMagicPos > -1 && lineMagicPos + LINE_MAGIC.length < currPos.column);
}

/**
Expand Down
10 changes: 9 additions & 1 deletion src/formatter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export class FormattingExtension
private notebookCodeFormatter: JupyterlabNotebookCodeFormatter;
private formatSQLButton: ToolbarButton;
private panel: NotebookPanel;
private extensionSettings: boolean;


constructor(
tracker: INotebookTracker
Expand All @@ -39,6 +41,7 @@ export class FormattingExtension
}

private _onSettingsChanged = (sender: any, settings: JupySQLSettings) => {
this.extensionSettings = settings.showFormatSQL;
if (!settings.showFormatSQL) {
this.formatSQLButton.parent = null;
} else {
Expand All @@ -65,6 +68,11 @@ export class FormattingExtension
this.formatSQLButton.node.setAttribute("data-testid", "format-btn");

panel.toolbar.insertItem(10, 'formatSQL', this.formatSQLButton);
if (!this.extensionSettings) {
this.formatSQLButton.parent = null;
} else {
this.panel.toolbar.insertItem(10, 'formatSQL', this.formatSQLButton);
}

return new DisposableDelegate(() => {
this.formatSQLButton.dispose();
Expand Down Expand Up @@ -97,4 +105,4 @@ const plugin_formatting: JupyterFrontEndPlugin<void> = {
};


export { plugin_formatting }
export { plugin_formatting }
63 changes: 59 additions & 4 deletions ui-tests/tests/completer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ async function createNotebook(page: IJupyterLabPageFixture) {

const samples = {
'upper case': {
input: 'SEL',
input: '%sql SEL',
expected: ['SELECT', 'SELECT DISTINCT', 'SELECT INTO', 'SELECT TOP'],
unexpected: ['INSERT']
},
'lower case': {
input: 'sel',
input: '%sql sel',
expected: ['SELECT', 'SELECT DISTINCT', 'SELECT INTO', 'SELECT TOP'],
unexpected: ['INSERT']
},
'in-word': {
input: 'se',
input: '%sql se',
expected: ['SELECT', 'SELECT DISTINCT', 'SELECT INTO', 'SELECT TOP', 'INSERT INTO'],
unexpected: []
}
Expand Down Expand Up @@ -56,7 +56,7 @@ test('test complete updates cell', async ({ page }) => {
await createNotebook(page);

await page.notebook.enterCellEditingMode(0);
await page.keyboard.type('SEL');
await page.keyboard.type('%sql SEL');

await page.keyboard.press('Tab');

Expand All @@ -70,3 +70,58 @@ test('test complete updates cell', async ({ page }) => {
});

});

const contexts = {
'valid cell magic': {
input: '%%sql\nSEL',
completion: true
},
'invalid cell magic': {
input: ' %%sql\nSEL',
completion: false
},
'line magic': {
input: '%sql SEL',
completion: true
},
'line magic with python': {
input: 'result = %sql SEL',
completion: true
},
'line magic new line': {
input: '%sql SEL\nSEL',
completion: false
},
'no magic': {
input: 'SEL',
completion: false
}
}

for (const [ name, { input, completion} ] of Object.entries(contexts))
test(`test ${name} ${completion ? 'does' : 'does not'} complete`, async ({ page }) => {
await createNotebook(page);

await page.notebook.enterCellEditingMode(0);
await page.keyboard.type(input);

await page.keyboard.press('Tab');
const suggestions = page.locator('.jp-Completer');
if (completion)
await expect(suggestions).toBeVisible();
else
await expect(suggestions).not.toBeVisible();
});

test('test no completion before line magic', async ({ page }) => {
await createNotebook(page);

await page.notebook.enterCellEditingMode(0);
await page.keyboard.type('SEL = %sql SEL');
for (let i=0; i<11; i++)
await page.keyboard.press('ArrowLeft');

await page.keyboard.press('Tab');
const suggestions = page.locator('.jp-Completer');
await expect(suggestions).not.toBeVisible();
});
2 changes: 1 addition & 1 deletion ui-tests/tests/format_sql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test('test format SQL', async ({ page }) => {
await page.notebook.openByPath("sample.ipynb");
await page.notebook.activate("sample.ipynb");
await page.notebook.addCell("code", "%%sql\nselect * from table")
await page.getByTestId('format-btn').locator('button').click();
await page.getByTestId('format-btn').locator('button').click({ force: true });
await page.waitForTimeout(2000);

await expect(page.locator('body')).toContainText('SELECT');
Expand Down

0 comments on commit 33119fd

Please sign in to comment.