# \[GSoC 2019\] Code Formatting Functionality for SourceKit-LSP

**URL:** <https://forums.swift.org/t/gsoc-2019-code-formatting-functionality-for-sourcekit-lsp/21033>\
**Category:** SourceKit-LSP\
**Tags:** gsoc-2019\
**Created:** [February 28, 2019, 7:58pm UTC](https://forums.swift.org/t/gsoc-2019-code-formatting-functionality-for-sourcekit-lsp/21033 "2019-02-28T19:58:47Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gumichocopengin8](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gumichocopengin8/32/6853_2.png) [@Gumichocopengin8](https://forums.swift.org/u/Gumichocopengin8)\
**Post date:** [February 28, 2019, 7:58pm UTC](https://forums.swift.org/t/gsoc-2019-code-formatting-functionality-for-sourcekit-lsp/21033/1 "2019-02-28T19:58:47Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![blangmuir](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/blangmuir/32/277_2.png) [@blangmuir](https://forums.swift.org/u/blangmuir)\
**Post date:** [February 28, 2019, 11:03pm UTC](https://forums.swift.org/t/gsoc-2019-code-formatting-functionality-for-sourcekit-lsp/21033/2 "2019-02-28T23:03:36Z")

</div>

Hi @Gumichocopengin8,

> [@Gumichocopengin8](#):
>
> Could you tell me more about Implement Code Formatting Functionality for SourceKit-LSP.

There's a description at [Swift.org - Project Ideas for GSoC 2019](https://swift.org/gsoc2019/#implement-code-formatting-functionality-for-sourcekit-lsp). Did you have a more specific question in mind?

> [@Gumichocopengin8](#):
>
> Can I also propose my own project?

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

---

<div class="post-metadata">

**Author:** ![Gumichocopengin8](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gumichocopengin8/32/6853_2.png) [@Gumichocopengin8](https://forums.swift.org/u/Gumichocopengin8)\
**Post date:** [March 1, 2019, 5:23am UTC](https://forums.swift.org/t/gsoc-2019-code-formatting-functionality-for-sourcekit-lsp/21033/3 "2019-03-01T05:23:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![akyrtzi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/akyrtzi/32/4963_2.png) [@akyrtzi](https://forums.swift.org/u/akyrtzi)\
**Post date:** [March 1, 2019, 7:32pm UTC](https://forums.swift.org/t/gsoc-2019-code-formatting-functionality-for-sourcekit-lsp/21033/4 "2019-03-01T19:32:47Z")

</div>

Hi Keita,

> [@Gumichocopengin8](#):
>
> 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.

I'd recommend that you post in the [Standard Library forum](https://forums.swift.org/c/development/stdlib), (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

---

<div class="post-metadata">

**Author:** ![blangmuir](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/blangmuir/32/277_2.png) [@blangmuir](https://forums.swift.org/u/blangmuir)\
**Post date:** [March 1, 2019, 7:46pm UTC](https://forums.swift.org/t/gsoc-2019-code-formatting-functionality-for-sourcekit-lsp/21033/5 "2019-03-01T19:46:54Z")

</div>

> [@Gumichocopengin8](#):
>
> 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.

Sure! The [swift-format](https://github.com/google/swift/tree/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](https://microsoft.github.io/language-server-protocol/specification#textDocument_formatting) request and the [textDocument/rangeFormatting](https://microsoft.github.io/language-server-protocol/specification#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](https://github.com/llvm-mirror/clang/blob/master/tools/clang-format/git-clang-format), which I've found very helpful.

---

<div class="post-metadata">

**Author:** ![Gumichocopengin8](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gumichocopengin8/32/6853_2.png) [@Gumichocopengin8](https://forums.swift.org/u/Gumichocopengin8)\
**Post date:** [March 2, 2019, 1:40am UTC](https://forums.swift.org/t/gsoc-2019-code-formatting-functionality-for-sourcekit-lsp/21033/6 "2019-03-02T01:40:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Gumichocopengin8](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gumichocopengin8/32/6853_2.png) [@Gumichocopengin8](https://forums.swift.org/u/Gumichocopengin8)\
**Post date:** [March 2, 2019, 2:09am UTC](https://forums.swift.org/t/gsoc-2019-code-formatting-functionality-for-sourcekit-lsp/21033/7 "2019-03-02T02:09:08Z")

</div>

> Sure! The [swift-format](https://github.com/google/swift/tree/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](https://microsoft.github.io/language-server-protocol/specification#textDocument_formatting) request and the [textDocument/rangeFormatting](https://microsoft.github.io/language-server-protocol/specification#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](https://microsoft.github.io/language-server-protocol/specification#textDocument_formatting) request and the [textDocument/rangeFormatting](https://microsoft.github.io/language-server-protocol/specification#textDocument_rangeFormatting) request?

Thank you.

---

<div class="post-metadata">

**Author:** ![akyrtzi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/akyrtzi/32/4963_2.png) [@akyrtzi](https://forums.swift.org/u/akyrtzi)\
**Post date:** [March 5, 2019, 2:14am UTC](https://forums.swift.org/t/gsoc-2019-code-formatting-functionality-for-sourcekit-lsp/21033/8 "2019-03-05T02:14:11Z")

</div>

Hey Keita,

There's the SourceKit-LSP but there's also the [swift-format](https://github.com/google/swift/tree/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.

---

<div class="post-metadata">

**Author:** ![MrBee](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mrbee/32/2312_2.png) [@MrBee](https://forums.swift.org/u/MrBee)\
**Post date:** [March 5, 2019, 5:26am UTC](https://forums.swift.org/t/gsoc-2019-code-formatting-functionality-for-sourcekit-lsp/21033/9 "2019-03-05T05:26:38Z")

</div>

I just downloaded [swift-format](https://github.com/google/swift/tree/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.

---

<div class="post-metadata">

**Author:** ![akyrtzi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/akyrtzi/32/4963_2.png) [@akyrtzi](https://forums.swift.org/u/akyrtzi)\
**Post date:** [March 5, 2019, 6:09am UTC](https://forums.swift.org/t/gsoc-2019-code-formatting-functionality-for-sourcekit-lsp/21033/10 "2019-03-05T06:09:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Gumichocopengin8](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gumichocopengin8/32/6853_2.png) [@Gumichocopengin8](https://forums.swift.org/u/Gumichocopengin8)\
**Post date:** [March 5, 2019, 6:32am UTC](https://forums.swift.org/t/gsoc-2019-code-formatting-functionality-for-sourcekit-lsp/21033/11 "2019-03-05T06:32:05Z")

</div>

> There's the SourceKit-LSP but there's also the [swift-format](https://github.com/google/swift/tree/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](https://github.com/google/swift/tree/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.

---

<div class="post-metadata">

**Author:** ![akyrtzi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/akyrtzi/32/4963_2.png) [@akyrtzi](https://forums.swift.org/u/akyrtzi)\
**Post date:** [March 5, 2019, 6:43am UTC](https://forums.swift.org/t/gsoc-2019-code-formatting-functionality-for-sourcekit-lsp/21033/12 "2019-03-05T06:43:00Z")

</div>

> [@Gumichocopengin8](#):
>
> I cannot use command `swift-format -v` on terminal

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

---

<div class="post-metadata">

**Author:** ![Gumichocopengin8](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gumichocopengin8/32/6853_2.png) [@Gumichocopengin8](https://forums.swift.org/u/Gumichocopengin8)\
**Post date:** [March 5, 2019, 6:59am UTC](https://forums.swift.org/t/gsoc-2019-code-formatting-functionality-for-sourcekit-lsp/21033/13 "2019-03-05T06:59:18Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![akyrtzi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/akyrtzi/32/4963_2.png) [@akyrtzi](https://forums.swift.org/u/akyrtzi)\
**Post date:** [March 5, 2019, 7:10am UTC](https://forums.swift.org/t/gsoc-2019-code-formatting-functionality-for-sourcekit-lsp/21033/14 "2019-03-05T07:10:03Z")

</div>

I'd recommend to familiarize yourself with how to use swiftpm, see this [quick guide on swift.org](https://swift.org/getting-started/#using-the-package-manager).

---

<div class="post-metadata">

**Author:** ![Gumichocopengin8](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gumichocopengin8/32/6853_2.png) [@Gumichocopengin8](https://forums.swift.org/u/Gumichocopengin8)\
**Post date:** [March 5, 2019, 7:16am UTC](https://forums.swift.org/t/gsoc-2019-code-formatting-functionality-for-sourcekit-lsp/21033/15 "2019-03-05T07:16:57Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![krzyzanowskim](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/krzyzanowskim/32/1282_2.png) [@krzyzanowskim](https://forums.swift.org/u/krzyzanowskim)\
**Post date:** [March 6, 2019, 12:45pm UTC](https://forums.swift.org/t/gsoc-2019-code-formatting-functionality-for-sourcekit-lsp/21033/16 "2019-03-06T12:45:01Z")

</div>

> [@akyrtzi](#):
>
> There's the SourceKit-LSP but there's also the [swift-format](https://github.com/google/swift/tree/format) part.

On a side note, [swift-format](https://github.com/google/swift/tree/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.

---

<div class="post-metadata">

**Author:** ![Setleaf](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/setleaf/32/582_2.png) [@Setleaf](https://forums.swift.org/u/Setleaf)\
**Post date:** [March 6, 2019, 3:33pm UTC](https://forums.swift.org/t/gsoc-2019-code-formatting-functionality-for-sourcekit-lsp/21033/17 "2019-03-06T15:33:57Z")

</div>

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](https://github.com/google/swift/tree/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?

---

<div class="post-metadata">

**Author:** ![blangmuir](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/blangmuir/32/277_2.png) [@blangmuir](https://forums.swift.org/u/blangmuir)\
**Post date:** [March 6, 2019, 4:55pm UTC](https://forums.swift.org/t/gsoc-2019-code-formatting-functionality-for-sourcekit-lsp/21033/18 "2019-03-06T16:55:34Z")

</div>

> [@Setleaf](#):
>
> I'm going to fork [swift-format](https://github.com/google/swift/tree/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?

- There is some good information in the Documentation directory of this repo 😄
- 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?

---

<div class="post-metadata">

**Author:** ![allevato](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/allevato/32/385_2.png) [@allevato](https://forums.swift.org/u/allevato)\
**Post date:** [March 6, 2019, 5:09pm UTC](https://forums.swift.org/t/gsoc-2019-code-formatting-functionality-for-sourcekit-lsp/21033/19 "2019-03-06T17:09:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Salman](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/salman/32/6413_2.png) [@Salman](https://forums.swift.org/u/Salman)\
**Post date:** [March 7, 2019, 7:01am UTC](https://forums.swift.org/t/gsoc-2019-code-formatting-functionality-for-sourcekit-lsp/21033/20 "2019-03-07T07:01:21Z")

</div>

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

I'm interested in [SourceKit-LSP](https://github.com/apple/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](https://github.com/google/swift/tree/format) format the whole file, but in gsoc we are going to format the particular line or range of text, am i right ?

[Next page](https://forums.swift.org/t/gsoc-2019-code-formatting-functionality-for-sourcekit-lsp/21033.md?page=2)
