Go lang, Atom, tabs and go-plus in combination produce tabs worth 4 spaces on GitHub code preview

agolangf · · 564 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>So I just got into Go, installed it, opened Atom and installed go-plus package for some IDE utils related to the language. Everything was cool that evening, I learned to code a bit, indented my code with tabs, and was happy about it. So I wanted to share it with my friends but when I pushed my code to GitHub I saw this disaster: <a href="https://i.imgur.com/RSWwrDh.png" rel="nofollow">https://i.imgur.com/RSWwrDh.png</a> </p> <p>All my tabs on GitHub look like they are in non-configured Vim, with 4 spaces? (I expected 2 as it gets formated in other IDEs) I didn&#39;t have this issue with any language so far...is this Atom issue? Go-plus plugin? GitHub displays tabs differently for Golang? Has anyone had similar issue before?</p> <hr/>**评论:**<br/><br/>pdffs: <pre><p>Is that spaces, or tabs? The standard for Go (as produced by <code>gofmt</code>) is to use tabs for indentation, not spaces.</p> <p>I feel like maybe you are using the word <code>tab</code> to describe indentation, but <code>tab</code> is a character.</p></pre>kunokdev: <pre><p>I pressed tab (the button) when I needed indention (like all normal people). I would press enter in Atom and it would auto-indent. Just checked again, it&#39;s actually 1 tab character in GitHub, but that&#39;s really ugly to read. So perhaps it&#39;s due to GitHub? Oh just went to check other Go repos on GitHub and they are all like that :&#39;(</p></pre>pdffs: <pre><p>Tabs are superior to spaces for indentation, and are the standard for Go, you&#39;ll get used to it.</p></pre>kunokdev: <pre><p>Well I do use tabs since beginning, its just the way GitHub shows this code is gross</p></pre>jussij: <pre><p>The actual size of a tab is dependent on the thing that is viewing the tab.</p> <p>In this case GitHub is using the <em>standard default tab size</em> which is 8 characters.</p> <p>For example, if you print you code it will most likely also print with that 8 character tab size as most printers define a tab as 8 charters in size.</p></pre>App1eNerd: <pre><p>Add a .editorconfig with indent_size of 4.</p></pre>gbrlsnchs: <pre><p>There&#39;s nothing wrong with the output. <code>go-plus</code> runs <code>go fmt</code> when you save <code>*.go</code> files, which formats the code to use tabs. GitHub code preview sets tab length to 8, probably respecting Go code style.</p></pre>daveddev: <pre><p>If you use a tool like Tampermonkey, the tab size can be controlled:</p> <pre><code>// ==UserScript== // @name Control tabs // @namespace https://www.codemodus.com/ // @version 0.1 // @description Change the visual size of tabs in github. // @author daved // @match https://github.com/* // @grant none // ==/UserScript== (function() { &#39;use strict&#39;; function tabWidthPref(ext) { if (ext.length == 0) return 0; var prefs = { &#39;.go&#39;: 4, &#39;.cnf&#39;: 8, &#39;.js&#39;: 2, &#39;.html&#39;: 2, &#39;.htm&#39;: 2, &#39;.css&#39;: 2, }; var p = prefs[ext]; return (p &lt; 1) ? 4 : p; } function setTabSizes(size) { if (size &lt; 1) return; var es = document.getElementsByClassName(&#34;tab-size&#34;); for (var i = 0; es[i]; i++) { es[i].setAttribute(&#34;data-tab-size&#34;, size); } } function extension() { var path = window.location.pathname; var i = path.lastIndexOf(&#39;.&#39;); return (i &lt; 0) ? &#39;&#39; : path.substr(i); } function waitForResource(state, fn) { if (!state.includes(&#34;/blob/&#34;)) return; fetch(state).then(function(response) { setTimeout(fn, 333); }); } (function(history){ var ps = history.pushState; history.pushState = function(state) { if (typeof history.onpushstate == &#34;function&#34;) { history.onpushstate({state: state}); } return ps.apply(history, arguments); }; })(window.history); var set = () =&gt; setTabSizes(tabWidthPref(extension())); var waitSet = () =&gt; waitForResource(history.state, set); window.onpopstate = history.onpushstate = waitSet; set(); })(); </code></pre></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

564 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传