Skip to content

Add extra folding definitions for golang #42

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

Merged
merged 1 commit into from
Jan 10, 2023

Conversation

samrjack
Copy link
Contributor

This PR adds a couple of folding I personally have found quite useful as defaults for working with golang. Figured I'd add them to the general defaults so others can enjoy a better experience out of the box.

field_declaration_list

type point struct {
    x int
    y int
}

/* |
 * V
 */

type point struct {...}

import_spec_list

import (
    "fmt"
    "time"
)

/* |
 * V
 */

import (...)

interface_type

Needs to be offset quite a bit since the tree-sitter definition includes the word interface which should still be shown.

type Dog interface {
    Bark() (string, error)
    Beg() (bool, error)
}

/* |
 * V
 */

type Dog interface {...}

Copy link
Member

@jcs090218 jcs090218 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the interface_type would work by doing the offset. If you add spaces, tabs, newline in front the curly bracket then the offset is simply wrong!

type Dog interface


{
    Bark() (string, error)
    Beg() (bool, error)
}

/* |
 * V
 */

type Dog interface

...}

Others look good. 👍

@samrjack
Copy link
Contributor Author

That was made with go's standardized format in mind. Basically, no matter how someone tries to write go, it gets corrected to the format in my example.

However, I think you bring up a good point that folding should still work regardless of standards so I think I'll write a parser that'll search for the first instance of a provided delimiter character and fold based on that. I think that would be helpful here and elsewhere too.

@samrjack samrjack force-pushed the golang branch 2 times, most recently from dd4da1b to 2df18e1 Compare December 27, 2022 20:02
@samrjack
Copy link
Contributor Author

I've added the new parsing definition which takes two extra parameters (which means it'll need a lambda wrapper just like ts-fold-range-line-comment does). With this a fold can be defined to be between two marker characters. With this functionality added in, I was added in folding on const blocks since this makes the definition much cleaner than what I had in my own config.

const_declaration

In golang, you can either declare consts as a single line or a block. In tree-sitter, these two cases are treated using the same const_declaration node.

const Hello = "world"
const (
	a       = "circle"
	b       = 1
	c float = 4.65
)

/* |
 * v
 */

const Hello = "world"
const (...)

@samrjack
Copy link
Contributor Author

samrjack commented Dec 27, 2022

One other thing I'd like to note is that we could probably replace field_declaration_list and interface_type with a single fold definition '(type_declaration . (lambda (node offset) (ts-fold-range-markers node offset "[{(]" "[})]")) to match both cases plus it future proofs against syntax introduced in golang 1.9 (discussed here) even though it isn't yet supported by tree-sitter.

type (
	Name = string
	Age  = int
)

I chose not to do that for now since I don't have time to look into the edge cases this may run into and I don't know the efficiency impact that may occur by doing more regex searches. Anyways, just wanted to call that out as a possible future addition. I've added it to my own config for now to see how it performs and to see if there's anything I run into in the future.

Copy link
Member

@jcs090218 jcs090218 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try not to use regex base solution, walk through AST is much more dedicated than regex search!

@samrjack
Copy link
Contributor Author

samrjack commented Jan 9, 2023

Sorry for being out for a while, had a lot of stuff suddenly happen 😅 I took the stuff you posted above and changed some things around. I see you've added some of it already so I'll rebase my branch onto master tomorrow 😊.

@samrjack samrjack force-pushed the golang branch 2 times, most recently from a25eb5d to 7ee8a54 Compare January 9, 2023 18:07
Copy link
Member

@jcs090218 jcs090218 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for rebasing it! ❤️

I've tested and it works! Let me know if this is ready to merge! (I will be busy in next couple of days)

@samrjack
Copy link
Contributor Author

If it works for you and looks good to you, then it's ready to merge 😉.

@jcs090218 jcs090218 merged commit 75d6f9e into emacs-tree-sitter:master Jan 10, 2023
@jcs090218
Copy link
Member

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants