[GSoC 2019] Code Formatting Functionality for SourceKit-LSP

gsoc-2019

(Keita Nonaka) #1

@blangmuir

Hello Ben,

I am interested in participating in GSoC2019. Could you tell me more about Implement Code Formatting Functionality for SourceKit-LSP. ,Can I also propose my own project?

Thank you,
Keita


SourceKit-LSP Code formatting
Implement Code Formatting Functionality for SourceKit-LSP
Swift to participate in GSoC 2019!
[GSoC 2019] Implement Code Formatting Functionality for SourceKit-LSP
Implementing code formatting functionality for SourceKit-LSP
(Ben Langmuir) #2

Hi @Gumichocopengin8,

There's a description at https://swift.org/gsoc2019/#implement-code-formatting-functionality-for-sourcekit-lsp. Did you have a more specific question in mind?

Yes, you're welcome to propose your own project!


(Keita Nonaka) #3

Hi @blangmuir
Thank you for your reply.

The description says that I need to implement range-based code-formatting functionality. I slightly understand what it is, but not exactly. Could you tell me about range-based code-formatting functionality.

As I mentioned before, I also propose my own project for swift language. I'd like to implement basic collections, such as stack, queue, binary tree etc. because when I tried to use stack in swift, I couldn't use it. I know programmers can make own collections, but I think swift should have own collection library like Java or other languages.

Am I allowed to propose both projects? or should I propose collections in the same project as SourceKit-LSP?

Thank you,
Keita


(Argyrios Kyrtzidis) #4

Hi Keita,

I'd recommend that you post in the Standard Library forum, (with gsoc-2019 tag) and outline your proposal to see if

  • people think this is a good idea
  • you can gather interest for someone to mentor you for this

(Ben Langmuir) #5

Sure! The swift-format tool can reformat a whole file at once, but it's also useful to be able to only reformat a particular range within that file. For example, if you select lines 10-24 in an editor, you might want to reformat just those lines and not the rest. In LSP, this is the difference between the textDocument/formatting request and the textDocument/rangeFormatting request.

Another use case for range formatting would be to only reformat the parts of the code that you have changed. That's something you can do with e.g. git clang-format, which I've found very helpful.


(Keita Nonaka) #6

Thank you for your advice.
I've just create a new topic.


(Keita Nonaka) #7

Sure! The swift-format tool can reformat a whole file at once, but it's also useful to be able to only reformat a particular range within that file. For example, if you select lines 10-24 in an editor, you might want to reformat just those lines and not the rest. In LSP, this is the difference between the textDocument/formatting request and the textDocument/rangeFormatting request.

Thank you very much. That was helpful.
I just wanna make sure that I need to implement Code Formatting Functionality for SourceKit-LSP because SourceKit-LSP still doesn't have the functions. So I am supposed to implement it for Swift in Swift language. Is it correct?
All I have to implement is the textDocument/formatting request and the textDocument/rangeFormatting request?

Thank you.


(Argyrios Kyrtzidis) #8

Hey Keita,

There's the SourceKit-LSP but there's also the swift-format part. As Ben mentioned earlier that tool only supports reformatting the whole file, so part of the GSoC project would also be to add capability in the tool and its library API to reformat a particular range. SourceKit-LSP will use it as a library.

To get started look into getting accustomed with both SourceKit-LSP and swift-format, build and run them, and see how they work.


(Mr Bee) #9

I just downloaded swift-format from GitHub and try to build it on my Mac with swift build command. But it failed with an error message saying it couldn't find CCommonMark module. It seems that it requires a lot of external modules.

So, where can I find the binary file so I don't need to build it myself manually? I just want to use it, not to examine the code.

Thank you.


(Argyrios Kyrtzidis) #10

You need to do git submodule update --init before the swift build command.


(Keita Nonaka) #11

There's the SourceKit-LSP but there's also the swift-format part. As Ben mentioned earlier that tool only supports reformatting the whole file, so part of the GSoC project would also be to add capability in the tool and its library API to reformat a particular range. SourceKit-LSP will use it as a library.
To get started look into getting accustomed with both SourceKit-LSP and swift-format, build and run them, and see how they work.

Thank you for your advice.
I read some documents and understood how it works.
However, I got a problem.

I downloaded swift-format and built it, but I cannot use command swift-format -v on terminal.

Do I still need to do something to run it?

Thank you.


(Argyrios Kyrtzidis) #12

Could you clarify what is the exact output you are seeing ?


(Keita Nonaka) #13

I pulled format branch and run git submodule update --init and swift build.
Then I run the swift-format -v on the same directly.
The error message says -bash: swift-format: command not found


(Argyrios Kyrtzidis) #14

I'd recommend to familiarize yourself with how to use swiftpm, see this quick guide on swift.org.


(Keita Nonaka) #15

Thank you very much.
Ive only used swift on Xcode, so the website is really helpful.
I can run it now.

Can I have small issues or something for the swift format?


(Marcin Krzyzanowski) #16

On a side note, swift-format uses an old version of swift-syntax (Swift 4.2) so part of the job is to upgrade swift-format to the to the Swift snapshot used by the sourcekit-lsp) or wait until swift-format maintainers will do that.


(Nick) #17

Hi everyone,

I was going to start a new thread, but I noticed in a couple of posts that @akyrtzi was recommending people to post in this thread instead.

My name is Nick, and I'm an undergraduate senior at the University of Virginia's College at Wise where I'm majoring in Software Engineering.

I've always wanted to contribute to open source software, but have found that I get overwhelmed when trying to make sense of the larger projects that are out there (like Swift). I believe that working closely with an experienced mentor in a real-world project would be an amazing learning experience and would allow me to give back to a language that I fondly call my favorite.

I'm going to fork swift-format and begin looking through the code to get an idea of how it works. Does anyone have any tips or techniques that they find helpful when analyzing a project they're unfamiliar with?


(Ben Langmuir) #18
  • There is some good information in the Documentation directory of this repo :smile:
  • It's often useful to look at new code with a specific goal in mind. The classic example is to choose a bug that seems straightforward and try to fix it. It looks like there aren't very many open issues on this repo, so another idea would be to choose something specific about the formatting it does, and try to figure out where that happens in the code and understand it.
  • To get a high-level overview, you could start in the main function and try to follow along at a high level with what the flow of the program is - either by reading the code or in a debugger.

@allevato any other suggestions?


(Tony Allevato) #19

Hi Nick (a fellow southwest Virginian! I did my undergrad and grad school at Virginia Tech).

For integration with SourceKit-LSP, the main entry point you're going to be interested in is the SwiftFormat module, which provides a Swift API for invoking the formatter instead of spawning an external command-line process. The README discusses that in a little bit more detail (mainly just linking to the doc comments in the source).

So, you can add a dependency to swift-format in the Package.swift for SourceKit-LSP and then import those modules wherever you need them.

Right now the API is designed around either formatting code based on a file URL, or on a syntax tree that's already in memory. That should be enough (or almost enough) to do whole-file formatting. One area that we haven't jumped into is range-based (partial file) formatting, so there's room for improvement there.

@krzyzanowskim is right that swift-format is currently targeting Swift 4.2 compilers (since we wanted it to work for folks writing shipping code with the latest GM release available right now). We'll need to migrate it to support Swift 5 (some of the syntax nodes have changed); feel free to take a stab at it if you like. The changes aren't too significant, but we'll need to host them in a different branch for the time being until the code moves to a more permanent location.


(Muhammad Salman Zafar) #20

My name is Salman Zafar 6th semester student at Iqra University Islamabad, Pakistan (department of Computer Science)

I'm interested in SourceKit-LSP and i would like to participate.

I am using swift for over a year in iOS development and i am familiar with swift-package-manager

As @blangmuir and @akyrtzi mention that the tool swift-format format the whole file, but in gsoc we are going to format the particular line or range of text, am i right ?