gitea icon indicating copy to clipboard operation
gitea copied to clipboard

Configure tab size when viewing a file

Open markusamshove opened this issue 6 years ago • 32 comments
trafficstars

Hi,

Is there an option to configure how many spaces a tab contains?

Our java files are indented with tabs and when I view them, every tab seems to be 8 spaces wide. Normally I would expect 4 spaces for java, c# and similar languages

markusamshove avatar Nov 19 '19 08:11 markusamshove

I think you can put a .editorconfig in your repo and some aspects like the editor will honour it, not sure about repo viewer.

silverwind avatar Nov 19 '19 19:11 silverwind

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs during the next 2 weeks. Thank you for your contributions.

stale[bot] avatar Jan 19 '20 00:01 stale[bot]

This issue has been automatically closed because of inactivity. You can re-open it if needed.

stale[bot] avatar Feb 02 '20 01:02 stale[bot]

Hello,

How can I change Gitea's global tab size from 8 to 4 ?

Thanks.

KaKi87 avatar May 24 '20 11:05 KaKi87

please reopen. (stalebot is bad project management)

does this mean that managing projects that use tabs is not supported on Gitea? it looks like .editorconfig is used when editing files, but ignored when viewing them.

workaround: add custom/templates/custom/header.tmpl:

<style>
.tab-size-8 {
        tab-size: 4 !important;
        -moz-tab-size: 4 !important;
}
</style>

edit: added -moz-tab-size after @tmont, below.

Spongman avatar Jun 25 '20 17:06 Spongman

This issue has been automatically marked as stale because it has not had recent activity. I am here to help clear issues left open even if solved or waiting for more insight. This issue will be closed if no further activity occurs during the next 2 weeks. If the issue is still valid just add a comment to keep it alive. Thank you for your contributions.

stale[bot] avatar Aug 31 '20 05:08 stale[bot]

up

KaKi87 avatar Aug 31 '20 12:08 KaKi87

This issue has been automatically marked as stale because it has not had recent activity. I am here to help clear issues left open even if solved or waiting for more insight. This issue will be closed if no further activity occurs during the next 2 weeks. If the issue is still valid just add a comment to keep it alive. Thank you for your contributions.

stale[bot] avatar Oct 31 '20 04:10 stale[bot]

up

KaKi87 avatar Oct 31 '20 09:10 KaKi87

I build Gitea with this patch:

diff --git a/modules/templates/helper.go b/modules/templates/helper.go
index e4107dfa9..38c4c661f 100644
--- a/modules/templates/helper.go
+++ b/modules/templates/helper.go
@@ -185,18 +185,18 @@ func NewFuncMap() []template.FuncMap {
 			)
 			if ec != nil {
 				if value, ok = ec.(*editorconfig.Editorconfig); !ok || value == nil {
-					return "tab-size-8"
+					return "tab-size-4"
 				}
 				def, err := value.GetDefinitionForFilename(filename)
 				if err != nil {
 					log.Error("tab size class: getting definition for filename: %v", err)
-					return "tab-size-8"
+					return "tab-size-4"
 				}
 				if def.TabWidth > 0 {
 					return fmt.Sprintf("tab-size-%d", def.TabWidth)
 				}
 			}
-			return "tab-size-8"
+			return "tab-size-4"
 		},
 		"SubJumpablePath": func(str string) []string {
 			var path []string

Exagone313 avatar Nov 15 '20 18:11 Exagone313

I had to add this to @Spongman's custom template to get things working in Firefox as it does not (yet) support un-prefixed tab-size:

<style>
.tab-size-8 {
        tab-size: 4 !important;
        -moz-tab-size: 4 !important;
}
</style>

tmont avatar Dec 20 '20 06:12 tmont

Here's a bookmarklet you can use to format the tab size for Gitea when viewing code in compare/diff view through the web interface.

javascript:document.querySelectorAll('table.chroma').forEach((el)=>el.style.tabSize=2)

Adjust to your liking. Alternative options are to (a) add an .editorconfig file to the repository as mentioned by @silverwind or (b) use a different diff viewer such as delta.

vhscom avatar Jan 19 '22 02:01 vhscom

Well, you'd better use a userstyle rather than a bookmarklet

KaKi87 avatar Jan 19 '22 08:01 KaKi87

I think this issue is still open because the code view doesn't respect the configured tab-size in .editorconfig. The web editor itself certainly already does.

silverwind avatar Jan 19 '22 14:01 silverwind

Requiring such file doesn't seem right.

KaKi87 avatar Jan 19 '22 17:01 KaKi87

Yes, I think a user setting for default tab size is also desirable. But .editorconfig should have precedence when present.

silverwind avatar Jan 19 '22 17:01 silverwind

I'd say:

User settings > editorconfig > highlighting lib > default

markusamshove avatar Jan 19 '22 18:01 markusamshove

Who the heck uses a tab size of 8 by default? lol. Really surprised this isn't fixed. In addition to this, the default tab size should be 4 since almost every IDE out of the box uses 4.

(Oh I guess this fight goes along with the Linux Kernel standards. Well since I doubt gitea is being used by the Linux Kernal peops, I still argue 4 is a good default)

danebou avatar Feb 03 '22 19:02 danebou

One thing to take into consideration is that I doubt many projects do, or would even want, to add an .editorconfig file if they're using prettier. I could see that causing headaches if they're not in perfect alignment. Besides, who wants to add another dotfile to their repo. A sensible default of 4 spaces would be fine. But it would be even better to give the user control straight from the interface.

vhscom avatar Feb 03 '22 19:02 vhscom

Yes. Personally, I started using spaces as indentation character instead of tabs because of this issue. And this isn't right.

KaKi87 avatar Feb 03 '22 20:02 KaKi87

Who the heck uses a tab size of 8 by default? lol. Really surprised this isn't fixed. In addition to this, the default tab size should be 4 since almost every IDE out of the box uses 4.

FWIW, browsers are dictated by spec to use default size 8 thought I do agree 4 is a generally better default.

silverwind avatar Feb 04 '22 17:02 silverwind

If we add this (?ts=4): https://github.com/tiimgreen/github-cheat-sheet#adjust-tab-space

Then we can just make the links with the different tab size parameter as the "Change tab size" button, and we also got permalink for them. If someone figured out the right tab size for a file, then he can just share the permalink to others and save their time by a little bit.

With some tab-size-changing JavaScript and history.pushState(), we can also save the navgation/reloading of the "Change tab size" link-buttons.

const setTabSize = tabSize => {

	if ( !Number.isInteger(tabSize) ) {
		tabSize = Number.parseInt(tabSize);
	}

	if (tabSize <= 0 || tabSize > 16) {
		throw new RangeError("tabSize must be an integer between 1 to 16!");
	}

	let otherSizes = new Array(16).fill(null).map( (_, i) =>  i + 1 );
	otherSizes.splice(tabSize - 1, 1);

	document.querySelectorAll(
		otherSizes.map( size => ".tab-size-" + size ).join(",")
	).forEach(
		elem => elem.classList.replace(
			[...elem.classList].find( cls => cls.startsWith("tab-size-") ),
			"tab-size-" + tabSize
		)
	);

};

document.addEventListener("DOMContentLoaded", () => {
	let ts = new URLSearchParams(location.search).get("ts");
	if (ts !== null) {
		ts = Number.parseInt(ts);
		if (ts <= 0 && ts > 16) {
			setTabSize(ts);
		}
	}
});

// Just one button as an example...
const button = Object.assign(
	document.createElement("button"), { textContent: "Change tab size to 4" }
);

button.addEventListener("click", () => {
	setTabSize(4);
	const url = new URL(location);
	url.searchParams.set("ts", "4");
	history.pushState({}, "", url);
});

// Put this button into the document with methods like append() to give it a try.

Crystal-RainSlide avatar Jun 23 '22 11:06 Crystal-RainSlide

While I can definitely see why adding it as a query param would be useful, how would it handle the whitespace for files with differing behavior defined in the editorconfig? Should the editorconfig simply be ignored globally when that parameter is set?

delvh avatar Jun 23 '22 20:06 delvh

I think in general we want a setting UI where user can set their preference, similar to what GitHub now has for tab size:

https://github.com/settings/appearance

That setting should overrule .editorconfig. Only through the special value default would editorconfig then be applied.

We can du the url param too, but I think it's too obscure of a feature in general to be useful.

silverwind avatar Jun 24 '22 13:06 silverwind

While I can definitely see why adding it as a query param would be useful, how would it handle the whitespace for files with differing behavior defined in the editorconfig? Should the editorconfig simply be ignored globally when that parameter is set?

That setting should overrule .editorconfig. Only through the special value default would editorconfig then be applied.

On GitHub: ts parameter > .editorconfig > tab size in the user settings. Everyone can check this by themselves.

This seems to be decided by the scope. ts parameter is specifc to a webpage, .editorconfig is specifc to a repo, and tab size in the user settings is specifc to everything a user sees, and the smaller the scope the higher the priority.


I think in general we want a setting UI where user can set their preference, similar to what GitHub now has for tab size:

github.com/settings/appearance

Yes.


We can du the url param too, but I think it's too obscure of a feature in general to be useful.

Obscure is what GitHub had done wrong, anyone else can get it right (GitHub itself included).

I'd already talked about the "Change tab size" button thing, but let's see if I can come up with some showcase...

Update: I'm trying to write a userscript to add a "Change tab size" button for both try.gitea.io and GitHub.

Crystal-RainSlide avatar Jun 25 '22 07:06 Crystal-RainSlide

it should be simple for the user to easily, and permanently override whatever is in a repo's .editorconfig. the whole point of using tabs is that the indentation amount is configurable by the viewer, not baked into the source.

Spongman avatar Jun 28 '22 17:06 Spongman

please reopen. (stalebot is bad project management)

does this mean that managing projects that use tabs is not supported on Gitea? it looks like .editorconfig is used when editing files, but ignored when viewing them.

workaround: add custom/templates/custom/header.tmpl:

<style>
.tab-size-8 {
        tab-size: 4 !important;
        -moz-tab-size: 4 !important;
}
</style>

edit: added -moz-tab-size after @tmont, below.

image

Unfortunately, with this patch 4 spaces and 1 tab are not the same width :fearful: .

UnlimitedCookies avatar Sep 09 '22 13:09 UnlimitedCookies

Both GitHub and GitLab have a user preference for defining the tabstop width for viewing files. As long as the .editorconfig preferences only apply to the editor view, it shouldn't be viewed as a solution here and other related issues.

Tabstop size is important for languages that enforce tabs over spaces, such as Go. I'm currently using the Stylus extension to override tabstops for forge sites, including my self-hosted instance, but this is a browser specific workaround on my desktop; it does not apply to mobile where screen real estate is even more valuable.

I understand the addition of this feature is not completely simple, but it would be greatly appreciated!

omenos avatar Dec 04 '22 16:12 omenos

I guess we can implement 2,4,8,auto options. Default being auto which derives from editorconfig, and possibly falls back to a default of 4 (or 8, which is still what browsers default to, but I think it's antiquated and 4 is a better default).

silverwind avatar Dec 05 '22 09:12 silverwind

I guess we can implement 2,4,8,auto options. Default being auto which derives from editorconfig, and possibly falls back to a default of 4 (or 8, which is still what browsers default to, but I think it's antiquated and 4 is a better default).

This would be very welcomed change.

arkadiuszmakarenko avatar Feb 17 '23 11:02 arkadiuszmakarenko