diff --git a/.gitignore b/.gitignore index 3c3629e..9daa824 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ +.DS_Store node_modules diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e0e112..04a1d5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,15 +3,6 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog] (http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). -### [1.1.0] - 2019-06-04 -- Added support for f-strings - -### [1.0.0] - 2019-03-29 -- Added snippet and keybinding - -### [0.1.0] - 2019-03-28 -- Published on VS Code marketplace - -## [0.0.1] - 2018-03-28 -### Added -- First working version +### [0.0.1] - 2021-12-13 +- Made it so adding `--(begin-)?sql` and `--end-sql` is not necessary! +- Forked from es6-string-html diff --git a/LICENSE b/LICENSE index 31f06ad..4980e91 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2018 ptweir +Copyright (c) 2018 ptweir, munro Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 293c432..2e85d38 100644 --- a/README.md +++ b/README.md @@ -1,58 +1,18 @@ -# SQL highlighting in Python multiline strings for VS Code +# SQL highlighting in Python multiline strings for VS Code -Adds syntax highlight support for python multiline SQL strings in VS Code. +Adds automatic syntax highlight support SQL strings embedded in Python strings. -## Installation +**This is still under development,** if you experience issues please try to help us fix them. :) Auto detecting SQL, and highlighting it properly insight Python is _hard_! -Install `python-string-sql` from extensions (`ctrl + shift + x` or `cmd + shift + x` on mac). -> Also available on [marketplace.visualstudio.com](https://marketplace.visualstudio.com/items?itemName=ptweir.python-string-sql) +View on extension on the [VS Code Marketplace](https://marketplace.visualstudio.com/items?itemName=Submersible.better-python-string-sql), or source code [Github](github.com/Submersible/better-python-string-sql). -## Example - -[![Example](docs/demo.png)](docs/demo.py) - -## Usage - -Insert `--sql`, `--beginsql`, or `--begin-sql` at the beginning of the part of the string you would like highlighted and a semicolon, `--endsql`, or `--end-sql` at the end of the highlighted section. - -### Snippets -begin typing `sql` and the autocomplete snippet will appear: - -![Snippet](docs/snippet.gif) - -### Keybindings - -cmd+s (or ctrl+s on mac) - Insert the following snippet: -``` -""" ---sql -SELECT -; -""" -``` - -## Requirements - -- Visual Studio Code v1.32.0 recommended -- Comments at beginning and end of highlighted section in the string (see Usage section). +![Image showing syntax highlighting working for SQL embedded inside a Python string](docs/demo.png) ## Community -- 2018-09-04 forked from [es6-string-css](https://github.com/bashmish/es6-string-css) +- 2021-12-13 forked from [python-string-sql](https://github.com/ptweir/python-string-sql) ## Release Notes -### [1.1.0] - 2019-06-04 -- Added support for f-strings - -### [1.0.0] - 2019-03-29 -- Added snippet and keybinding - -### [0.1.0] - 2019-03-28 -- Published on VS Code marketplace - -### [0.0.1] - 2019-03-28 -- Got it working based on [these instructions](https://code.visualstudio.com/api/language-extensions/syntax-highlight-guide) - -### [0.0.0] - 2018-09-04 +### [0.0.1] - 2021-12-13 +- Made it so adding `--(begin-)?sql` and `--end-sql` is not necessary! - Forked from es6-string-html - diff --git a/better-python-string-sql-0.0.1.vsix b/better-python-string-sql-0.0.1.vsix new file mode 100644 index 0000000..b1d5da5 Binary files /dev/null and b/better-python-string-sql-0.0.1.vsix differ diff --git a/better-python-string-sql-0.0.2.vsix b/better-python-string-sql-0.0.2.vsix new file mode 100644 index 0000000..9898a70 Binary files /dev/null and b/better-python-string-sql-0.0.2.vsix differ diff --git a/docs/demo.png b/docs/demo.png index 8b7036d..c85fafc 100644 Binary files a/docs/demo.png and b/docs/demo.png differ diff --git a/docs/demo.py b/docs/demo.py index 2d38ec1..eecf32d 100644 --- a/docs/demo.py +++ b/docs/demo.py @@ -1,19 +1,47 @@ -import psycopg2 - -conn = psycopg2.connect("dbname=test user=postgres") -cur = conn.cursor() - -query = """ ---begin-sql -SELECT last_name, - start_day, - COUNT(*) AS num_entries -FROM schema.table -- great name! -WHERE start_day >= '2019-01-01' -GROUP BY last_name, start_day -ORDER BY num_entries DESC -LIMIT 10; -""" - -cur.execute(query) -first_row = cur.fetchone() +import sqlite3 + +DATASET = [ + ("Tencho", "2018-12-03"), + ("Bessho", "2018-12-03"), + ("Emoto", "2020-12-03"), + ("Gamo", "2020-12-03"), + ("Funakoshi", "2020-12-03"), + ("Funakoshi", "2020-12-03"), + ("Doigaki", "2020-12-03"), + ("Doigaki", "2020-20-03"), + ("Chikura", "2020-12-03"), + ("Akabane", "2020-12-03"), +] + + +def main(): + conn = sqlite3.connect(":memory:") + + conn.executescript( + """ + DROP TABLE IF EXISTS foobar; + CREATE TABLE foobar ( + last_name TEXT TEXT NOT NULL, + start_day TEXT NOT NULL + ); + """ + ) + + conn.executemany("INSERT INTO foobar VALUES (?, ?)", DATASET) + + query = """ + SELECT last_name, + start_day, + COUNT(*) AS num_entries + FROM foobar + WHERE start_day >= '2019-01-01' + GROUP BY last_name, start_day + ORDER BY num_entries DESC + LIMIT 10; + """ + + print(conn.execute(query).fetchall()) + + +if __name__ == "__main__": + main() diff --git a/docs/python-string-sql-1.1.0.vsix b/docs/python-string-sql-1.1.0.vsix deleted file mode 100644 index 14a230d..0000000 Binary files a/docs/python-string-sql-1.1.0.vsix and /dev/null differ diff --git a/docs/snippet.gif b/docs/snippet.gif deleted file mode 100644 index 502ddda..0000000 Binary files a/docs/snippet.gif and /dev/null differ diff --git a/package-lock.json b/package-lock.json index 6b0cfca..437fede 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,15 @@ { - "name": "python-string-sql", - "version": "1.1.0", - "lockfileVersion": 1 + "name": "better-python-string-sql", + "version": "0.0.1", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "name": "better-python-string-sql", + "version": "0.0.1", + "engines": { + "vscode": "^1.63.0" + } + } + } } diff --git a/package.json b/package.json index 098c6c1..17155b1 100644 --- a/package.json +++ b/package.json @@ -1,60 +1,34 @@ { - "name": "python-string-sql", - "displayName": "python-string-sql", + "name": "better-python-string-sql", + "displayName": "better-python-string-sql", "description": "highlight python strings as sql", - "version": "1.1.0", - "publisher": "ptweir", + "version": "0.0.2", + "publisher": "Submersible", "icon": "docs/logo.png", "repository": { - "url": "https://github.com/ptweir/python-string-sql" + "url": "https://github.com/Submersible/better-python-string-sql" }, "engines": { - "vscode": "^1.32.0" + "vscode": "^1.0.0" }, "categories": [ - "Programming Languages", - "Snippets" + "Programming Languages" ], "contributes": { "grammars": [ { - "path": "./syntaxes/highlight-sql-string.json", + "path": "./syntaxes/highlight-sql-string.tmLanguage.json", "scopeName": "python-sql.injection", "injectTo": [ "source.python" ], "embeddedLanguages": { - "meta.embedded.sql": "sql" - } - }, - { - "path": "./syntaxes/highlight-html-string.json", - "scopeName": "python-html.injection", - "injectTo": [ - "source.python" - ], - "embeddedLanguages": { - "meta.embedded.block.html": "html" - } - } - ], - "snippets": [ - { - "language": "python", - "path": "./snippets/python.json" - } - ], - "keybindings": [ - { - "command": "editor.action.insertSnippet", - "key": "cmd+s", - "mac": "ctrl+s", - "when": "editorTextFocus", - "args": { - "langId": "python", - "name": "SQL_highlighted_string" + "meta.embedded.block.sql": "sql" } } ] + }, + "scripts": { + "package": "npx vsce package" } -} +} \ No newline at end of file diff --git a/snippets/python.json b/snippets/python.json deleted file mode 100644 index 8d3f04f..0000000 --- a/snippets/python.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "SQL_highlighted_string": { - "prefix": "sql", - "body": [ - "\"\"\"", - "--sql", - "${1| ,SELECT,WITH|}", - ";", - "\"\"\"" - ], - "description": "SQL highlighted string" - } -} \ No newline at end of file diff --git a/syntaxes/highlight-html-string.json b/syntaxes/highlight-html-string.json deleted file mode 100644 index 85af0c1..0000000 --- a/syntaxes/highlight-html-string.json +++ /dev/null @@ -1,57 +0,0 @@ -{ - "fileTypes": [ - "py" - ], - "injectionSelector": "L:string.quoted.multi.python, L:meta.fstring.python - (comment.line.number-sign.python, punctuation.definition.comment.python)", - "repository": { - "keep-variables": { - "patterns": [ - { - "begin": "\\{", - "end": "\\}", - "beginCaptures": { - "1": { - "name": "punctuation.section.embedded.begin.py" - }, - "2": { - "name": "punctuation.section.embedded" - } - }, - "endCaptures": { - "1": { - "name": "punctuation.section.embedded.end.py" - }, - "2": { - "name": "punctuation.section.embedded" - } - }, - "patterns": [ - { - "include": "text.html.derivative" - } - ] - } - ] - } - }, - "patterns": [ - { - "begin": "( *--html| *--beginhtml| *--begin-html)", - "end": "( *--\\!html| *--endhtml| *--end-html)", - "captures": { - "1": { - "name": "variable.parameter" - } - }, - "patterns": [ - { - "include": "text.html.derivative" - }, - { - "include": "#keep-variables" - } - ] - } - ], - "scopeName": "python-html.injection" -} \ No newline at end of file diff --git a/syntaxes/highlight-sql-string.json b/syntaxes/highlight-sql-string.json deleted file mode 100644 index 50ad41e..0000000 --- a/syntaxes/highlight-sql-string.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "fileTypes": [ - "py" - ], - "injectionSelector": "L:string.quoted.multi.python, L:meta.fstring.python - (comment.line.number-sign.python, punctuation.definition.comment.python)", - "patterns": [ - { - "begin": "( *--sql| *--beginsql| *--begin-sql)", - "end": "( *;| *--endsql| *--end-sql)", - "captures": { - "1": { - "name": "variable.parameter" - } - }, - "patterns": [ - { - "include": "source.sql" - } - ] - } - ], - "scopeName": "python-sql.injection" -} \ No newline at end of file diff --git a/syntaxes/highlight-sql-string.tmLanguage.json b/syntaxes/highlight-sql-string.tmLanguage.json new file mode 100644 index 0000000..1b989b8 --- /dev/null +++ b/syntaxes/highlight-sql-string.tmLanguage.json @@ -0,0 +1,46 @@ +{ + "fileTypes": [ + "py" + ], + "injectionSelector": "L:string.quoted.multi.python, L:string.quoted.single.python, L:meta.fstring.python - (comment.line.number-sign.python, punctuation.definition.comment.python)", + "patterns": [ + { + "name": "meta.embedded.block.sql", + "begin": "\\s*((?i)(explain|alter|analyze|attach|begin|commit|create|delete|detach|drop|insert|pragma|reindex|release|rollback|savepoint|select|update|vacuum|replace))", + "end": ";|(?=\"\"\"|\"|--|((?i)(explain|alter|analyze|attach|begin|commit|create|delete|detach|drop|insert|pragma|reindex|release|rollback|savepoint|select|update|vacuum|replace)))", + "beginCaptures": { + "1": { + "name": "keyword.other.DML.sql" + } + }, + "patterns": [ + { + "include": "source.sql" + }, + { + "include": "source.python#fstring-guts" + } + ] + }, + { + "name": "meta.embedded.block.sql", + "begin": "\\s*(--[^\"\\n\\r\\{\\}\\\\]*)", + "end": "(?=\"|\\n|\\r)", + "beginCaptures": { + "0": { + "name": "comment.line.double-dash.sql" + } + }, + "patterns": [ + { + "include": "source.python#fstring-guts" + }, + { + "match": "[^\"\\n\\r\\{\\}\\\\]*", + "name": "comment.line.double-dash.sql" + } + ] + } + ], + "scopeName": "python-sql.injection" +} \ No newline at end of file