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
SQL formatting #4
Comments
I did look into SQL formatting briefly. However I could not find any good tool/library/extension for it. I think for this library the way to do it, would be to tell VS Code about the language in the SQL template literals. It's quite easy to achieve this for syntax highlighting. And it's quite a bit harder for IntelliSense and formatting support. The typescript-lit-html-plugin supports this for I don't have time to work on this at the moment. But it would be amazing if you or anyone else would like to work on this. |
Maybe https://www.npmjs.com/package/pg-formatter would help, if you're ok with requiring Perl (because the library wraps This would be amazing to have this! |
I don't think I mind requiring Perl, at least if it's optional. I'm not entirely sure how formatting in general should be handled, though. My feeling is that it would be nice for formatting to be a separate extension. If this extension could mark the SQL text properly as SQL, it might be formatted automatically by any extension that supports formatting of SQL. I think I read somewhere that something like this is possible. But I can't remember where. But I could also be wrong, in which case this might be the correct place. |
Yeah, what I would really like would be for it to be done by Prettier, which is not a new idea, but also probably won't be in core:
There is a first fledgling attempt at a plugin (https://github.com/benjie/prettier-plugin-pg), but it's probably going to be a while. As for non-Prettier options, there is an ESLint plugin, which currently gets the closest (using the |
Hm okay. After reading a bit more, I think formatting SQL in tagged template literals may have to be this plugin's responsibility. We have to implement: There is a VS Code extension, which uses pgFormatter: https://github.com/bradymholt/vscode-pgFormatter. It might be useful here. |
Nice! That would make this plugin the single solution for everything - that would be awesome. From my research into how a VS Code extension could implement something similar, I ran across the
Ah super interesting, I somehow missed this one! I would much prefer it being in your extension, but I have also opened an issue over there to see how willing the author is to support formatting of SQL in template strings: bradymholt/vscode-pgFormatter#40 Edit: Oh, maybe the author's not open to this after all: bradymholt/vscode-pgFormatter#40 (comment) |
I thought it would work like this, but I can't figure out when Need to play with it a bit more. |
Hm... yeah looking around a bit more, I couldn't find much on that method. But I did find something on Eg. something like this: https://github.com/mfeckie/handlebars-formatter/blob/345f08f5d33f0347dc57dbf47698f3dcae02e517/src/extension.ts#L37-L78 |
Yeah, I think those are VS Code APIs, which we could use in vscode-sql-tagged-template-literals. It would be nice if we could find a way to implement this in the TypeScript language server plugin, though. Because then it would be independent of the editor and could work everywhere. You can test this with this repo and VS Code without any extensions:
All this works without an extension. Ideally formatting would work, too. |
I'm still trying to understand why Started to dig around a bit. The TypeScript extension calls "format" here (which is registered using I think this calls the TypeScript server and I can see a command with the same name: And I think that's mapped to |
Oh wow, low level magic. Yeah, I don't have much experience with extension development alone, let alone the TS language server :) Although, looking about I did see these two resources which may shed some light on what's going on: |
Right, just tried this out for a few hours, and came to the same conclusions as in your last comment. It seems like it should be called, but it is not being called... I set it up with a logfile using: export TSS_LOG="-level verbose -file /Users/karl/projects/typescript-sql-tagged-template-plugin/tss.log" And it actually logs out the command:
But no logging of the |
Putting a Session.prototype.getFormattingEditsForRange = function (args) {
var _this = this;
this.logger.info('getFormattingEditsforRange in session'); |
Ok, I think I may have narrowed it down a bit. It seems that the proxy for It is wrapped here: cc @mjbvz is this a bug in |
The lit-html TS plugin's formatting still works for me with TS 4.1. I believe it's using the same function |
Do you mean
Doesn't it work with the built-in formatting in VS Code using the embedded It's different than this plugin, which actually contributes formatting edits from a third-party formatting engine. |
The lit-html extension is split in VS Code extension and TypeScript server plugin, too. You can see I'm going to play a bit with the lit-html plugin and figure out what the difference is. |
Okay, formatting with the typescript-lit-html-plugin works for me in Vim (using coc.nvim and the coc-tsserver extension). However it doesn't work in VS Code. I think VS Code my not invoke the format command of the language server. That would be really sad. Edit: This comment (not sure how reliable that information is) says VS Code doesn't use the language server protocol for TypeScript. This could explain why it works in Vim, but not VS Code. At least that's another thing to look at. Thanks, Matt. |
Ha, SQL formatting works in Vim with the current formatting branch. Requires Perl, obviously. Now, how do we make it work in VS Code? I hope this doesn't require a special extension. |
So, after thinking about it for a day, my gut feel hypothesis was this:
I think this may be true because when I added a
I think this because the This line of reasoning (although just a gut feel) leads me to the hypothesis that the But maybe it's way off base, not sure. It's the only way I could think of why there would be a discrepancy here (this as a beginner with the TypeScript codebase and language servers, of course). |
I found it 🎉 Apparently VS Code starts 2 different language servers: one for syntax and one for semantics. The semantic one seems to be the one that loads plugins. But here the "format" command is configured to always run on the syntax server: If I cheekily change that in my VS Code installation (under |
Nice, really great investigative work @frigus02 !! Diving really deep :) 💯 |
Oh, there is even a setting for this:
Now I also understand the log files.
The syntax server logs do load global plugins, though. This makes me think SQL formatting should work if the plugin is activated by the VS Code extension, rather than by the tsconfig.json file. I need to give this a go. |
The extension indeed causes the plugin to be loaded in both the semantic and syntax server. Unfortunately the syntax server does not receive the This means formatting has to be turned on by default. I guess we need to build do some feature detection (maybe check if Perl is install and in the |
I think all major issues are solved. I'm going to track the remaining work on the PR 🙂 |
I pushed version 0.1.0 of this plugin, which supports formatting. Thanks again @karlhorky for all your help. I also uploaded version 0.0.16 of the VS Code, which now supports formatting. See changelog for more details: |
So amazing, really impressive work @frigus02 !! 🔥 Just giving it a spin, seems to be working!! I'm going to need to probably pass some additional configuration options to
|
Have you ever looked into adding sql formatting to this library, prettier has spoiled me and there seem to be sql formatting extensions for vscode, but I have no idea, what would be needed to build a formatter for tagged template literals.
The text was updated successfully, but these errors were encountered: