Feature #1689

Tab Width

Added by Chris Miller about 9 years ago. Updated over 2 years ago.

Status:ReopenedStart date:2008-07-28
Priority:LowDue date:
Assignee:-% Done:

0%

Category:SCM
Target version:-
Resolution:

Description

There should be a setting (somewhere) to allow the site administrator, or even better, individual project owners, to change the tab width as displayed in the repository source browser. Right now it's set at something crazy like 6 or 8 characters, some prefer it at 2, others at 4, still others at 5. The ability to change the tab width would let people control the code display. If this could be snuck into 0.8 that'd be awesome. If not, oh well, there's always hope for 0.9!


Related issues

Duplicated by Redmine - Feature #3065: "Tab Size" setting Closed 2009-03-28
Duplicated by Redmine - Feature #4217: Allow Changing Tab-Length for Code View Closed 2009-11-14

History

#1 Updated by Grzegorz Adam Hankiewicz about 9 years ago

For those of us using vim modelines it would be great if they were parsed, allowing a per-file display configuration. Looking for tabstop=*X* in the first few lines of the file doesn't seem too hard.

#2 Updated by Chris Miller about 9 years ago

Grzegorz Adam Hankiewicz wrote:

For those of us using vim modelines it would be great if they were parsed, allowing a per-file display configuration. Looking for tabstop=*X* in the first few lines of the file doesn't seem too hard.

Get yer own feature request! ;-) Yeah, that's cool, but I think that'd be a completely different feature request. I'm just asking for a little drop-down combo box in the project settings page to set a tab width. You want a feature that looks for Vim stuff, which is a bit different than my request.

#3 Updated by Thomas Lecavelier about 9 years ago

  • Target version deleted (0.8)

The target version field has to be set when it will actually part of the target release.

#4 Updated by Chris Miller over 8 years ago

  • Assignee set to Thomas Lecavelier

Well, 0.8 came and went, can we please try for 0.9?

I use a tabstop of 4 in all my projects, so at very least tell me where the tabstop is defined so I can make a temporary fix by manually changing it for my installation!

#5 Updated by Thomas Lecavelier over 8 years ago

  • Status changed from New to Closed
  • Assignee deleted (Thomas Lecavelier)

Hey, you see? I can play with statuses too...

-_-

#6 Updated by Chris Miller over 8 years ago

Thomas Lecavelier wrote:

Hey, you see? I can play with statuses too... [...]

That's not nice...

#7 Updated by youngseok yi over 8 years ago

+1 for setting tab-width.
I think this feature is not implemented yet.

Dear Chris Miller. hope you to check SubmittingBugs
Dear Thomas Lecavelier. if it is not invalid request, how about to reopen?

#8 Updated by Chris Miller over 8 years ago

  • Status changed from Closed to Reopened

No, it is not done, so it should be either "open" or "wontfix".

I did some research into it, and I believe that browsers hardcode a tab character in a pre block to eight characters and it cannot be changed by any CSS or HTML attribute (I tried).

I believe the easiest way would be to replace every tab character with n spaces in the method on line 25 in source:tags/0.8.0/app/helpers/repositories_helper.rb however, I am unsure how to do this, and I'm also very unsure how to:

  1. add a new field to the project model (an integer storing the desired tab width)
  2. add stuff in the view to display the new setting so it can be changed by project managers
  3. modify the method previously indicated to replace tabs with characters for display

I'm not a Ruby programmer, and I've attempted to learn Rails before only to come out of it massively confused, so I feel really helpless just standing here pointing and yelling "make magic things happen here!" If I did know how... I would give you a patch and not a feature request.

#9 Updated by Chris Miller almost 8 years ago

Just an update...

I have found that by modifying the following lines of code:

source:tags/0.8.7/app/helpers/application_helper.rb#L192

def syntax_highlight(name, content)
    type = CodeRay::FileType[name]
    type ? CodeRay.scan(content, type).html(:tab_width => 4) : h(content.gsub("\t"," "*4))
end

And

source:tags/0.8.7/lib/redmine/wiki_formatting/textile/formatter.rb#L58

CodeRay.scan($2, $1.downcase).html(:escape => false, :line_numbers => :inline, :tab_width => 4)

I was able to hard-code my site to have a tab width of four characters. Mind you, this isn't a tab-stop - a lot of the time things don't quite line up, but at least they're only off by at most three characters! It's a far sight better than before!

If someone with more Ruby-fu could extract that to a kind of configurable variable we might have a worthwhile patch on our hands.

#10 Updated by Chris Miller almost 8 years ago

I have found an even better way of converting tabs to spaces. This respects tab stops, which is the concept that a tab character doesn't necessarily expand to 4 (or 3, or 2, or 6, or whatever) spaces, but advances the line to a predetermined tab stop. Tabstops are generally at 4, 8, 12, 16, 20, etc.

Enough preaching about the Holy Tabstop. Here's how to make it work:

This uses the GNU CoreUtils expand command. This works on files, so I just use Ruby's Tempfile to create a temporary file, then delete it when we're done. You do need to add this line of code to the beginning of the file, however:

require 'tempfile'

Now, add the instructions to your syntax highlighter to make it expand tabs to spaces:

def syntax_highlight(name, content)
    type = CodeRay::FileType[name]
    t = Tempfile.new("expandfile")
    t.syswrite(content)
    content = `expand -t 4 #{t.path}`
    t.close
    t.delete
    type ? CodeRay.scan(content, type).html : h(content)
end

Same thing applies to Wiki formatting. (don't forget to include the require statement there, either!)

This also works quite well with the Ultraviolet plugin.

#11 Updated by Chris Miller almost 8 years ago

Now, even more cool!

Add the following to lib/redmine/core_ext/string/conversions.rb

        # Expand tabs to spaces
        def expand(tab_width=4)
          text = self.dup
          out = ''
          text.each_line do |line|
            column = 0
            line.each_char do |c|
              if c == "\t" 
                next_tab_column = column + (tab_width - column % tab_width)
                while column < next_tab_column
                  out << " " 
                  column += 1
                end
              elsif c == "\b" 
                column += -1
              else
                column += 1
                out << c
              end
            end
          end
          out
        end

Now, instead of all that tempfile nonsense, you can simply use content.expand in app/helpers/application_helper.rb (or content.expand(4), or however many spaces you want your tabs to expand to).

#12 Updated by Kornelius Kalnbach over 7 years ago

I'd say you just abandon tabs :)

But maybe a per-language setting would be okay. Ruby style is 2 spaces, C is 8, Python is 4, for example. Better than the current 8-for-all setting.

#13 Updated by Chris Miller over 7 years ago

Kornelius Kalnbach wrote:

I'd say you just abandon tabs :)

Over my dead body.

But maybe a per-language setting would be okay. Ruby style is 2 spaces, C is 8, Python is 4, for example. Better than the current 8-for-all setting.

I don't know what planet you live on, but I have yet to actually meet a living C programmer who uses a tab-width of 8. All the programmers I know use a tab width of 4, though I find that a tab stop of 2 for C is actually quite nice and compact while using smaller laptop screens.

Having a per-project per-language setting seems important to me. But this is a system that would require additional database tables and rot like that, so even if I do decide to take up a patch, it'll certainly be one of those things that would only ever be a candidate for a 0.x release, and never a 0.9.x release. I absolutely believe that it should be part of the Redmine system by default, as source code with badly aligned tabs can decrease readability quite dramatically.

#14 Updated by Kornelius Kalnbach over 7 years ago

It this is such a hot topic, I'm happy to stay out of the discussion. CodeRay provides the option, so it's Redmine's decision how to implement it.

@Chris: Earth.

#15 Updated by Andrea Agosti almost 6 years ago

There is a chance that this will be solved? I'd like to have the possibility to set tab at 4 without hacking the code.

#16 Updated by Mario Luzeiro over 5 years ago

+Vote for this! I'm using tab size 2

#17 Updated by Joo Yeon Hwang over 2 years ago

I've changed tab width at source code view. But, at diff view mode, it still shows me the tab width of 8. How can I change the tab width to 4 at diff view.

Also available in: Atom PDF