9 Replies - 342 Views - Last Post: 12 January 2013 - 11:11 AM Rate Topic: -----

#1 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3641
  • View blog
  • Posts: 12,614
  • Joined: 12-December 12

Indenting for snippets

Posted 11 January 2013 - 04:08 PM

Hello. If anyone is using VS Express (2012), I know how to alter the indentation setting so I can use:

namespace ConsoleAppWord {
    class Program {

        public void ProcessDocument(Document aDoc) {

        }
    }
}


(just about works..) but does anyone know of a way I can get snippets to follow this same pattern?

Added: Actually, it seems to be remembering my choice after a while, Doh! So perhaps I don't need a reply. Andy.

This post has been edited by andrewsw: 11 January 2013 - 04:12 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Indenting for snippets

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5595
  • View blog
  • Posts: 11,970
  • Joined: 02-June 10

Re: Indenting for snippets

Posted 11 January 2013 - 04:25 PM

Stop. That is the brace pattern for JAVA but it isn't followed for C#
namespace ConsoleAppWord 
{
    class Program 
    {

        public void ProcessDocument(Document aDoc) 
        {

        }
    }
}


In .NET we line up the braces. It makes matching them pretty easy. Its also what is going to happen as soon as you hit clean-document (cntrol-k, control-d)

So accept the way the rest of of the world does it rather than fight it.

As for snippets, they are tempate based. You just edit the XML file that the snippet uses.
Look between lines 27 and 41 and you see the C# that will be inserted. You can add and reformat all you like. But as I said all the formatting tools for VS expect to reset the braces - all your coworkers (if you get that far) will expect it the way the rest of the industry does it. So embrace it.

<?xml version= "1.0" encoding= "utf-8" ?>
<CodeSnippets  xmlns= "http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet ">
  <CodeSnippet Format= "1.0.0 ">
    <Header>
      <Title> propn</Title>
      <Shortcut> propnot</Shortcut>
      <Description> Code snippet for a property that calls onpropertychanged() in the setter (to assist in INotifyPropertyChanged implementation).      </Description>
      <Author> Aaron Hoffman</Author>
      <SnippetTypes>
        <SnippetType> Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID> type</ID>
          <ToolTip> Property type</ToolTip>
          <Default> int</Default>
        </Literal>
        <Literal>
          <ID> property</ID>
          <ToolTip> Property name</ToolTip>
          <Default> MyProperty</Default>
        </Literal>
      </Declarations>
      <Code Language= "csharp ">
        <![CDATA[
        public $type$ $property$     
        {         
            get { return _$property$; }         
            set         
            {             
                if (_$property$ == value) return;
                _$property$ = value;
               RaisePropertyChanged("$property$");
             }
        }
        // ReSharper disable InconsistentNaming
        private $type$ _$property$; 
        // ReSharper restore InconsistentNaming
        $end$]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3641
  • View blog
  • Posts: 12,614
  • Joined: 12-December 12

Re: Indenting for snippets

Posted 11 January 2013 - 04:58 PM

Thank you.

Yes, I was aware that I'm swimming upstream and that my code would be re-formatted at some point. At the moment, though, I'm just dabbling with C#, and I prefer the familiarity of my braces :)/>/>/>.

But you're right - I might as well accept it from the get-go!

Added: Should I tick ALL the newline settings?

This post has been edited by andrewsw: 11 January 2013 - 05:02 PM

Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Re: Indenting for snippets

Posted 11 January 2013 - 07:30 PM

Do some experimenting, and try stuff out. You can allows un-tick it.

This post has been edited by AdamSpeight2008: 11 January 2013 - 08:07 PM

Was This Post Helpful? 0
  • +
  • -

#5 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Indenting for snippets

Posted 11 January 2013 - 07:33 PM

The rest of the world is wrong, { go on the end of the line. You wouldn't write
if (some condition)
then
    doStuff();
end if


And it's C style, not Java style. Java has no style.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5595
  • View blog
  • Posts: 11,970
  • Joined: 02-June 10

Re: Indenting for snippets

Posted 11 January 2013 - 08:19 PM

View Postandrewsw, on 11 January 2013 - 05:58 PM, said:

Thank you.

Yes, I was aware that I'm swimming upstream and that my code would be re-formatted at some point. At the moment, though, I'm just dabbling with C#, and I prefer the familiarity of my braces :)/>/>/>/>.

But you're right - I might as well accept it from the get-go!

Added: Should I tick ALL the newline settings?


I leave virtually all the VS settings at default except for the font. I like PROGGY CRISP much more for things like coding. But otherwise the settings are pretty much stock meaning I can expect to have my code match any other developer's expectations, and their code looks like what I expect. I can hand my laptop to a co-worker and they don't have a period of adjustment just to use it. My samples here look like everyone else's and so on. If I had received code with that bracing setup, the first thing I would do is apply clean-up and formatting to bring it back into standard alignment. But then you would do the same with mine; and that's fine. That's why we all have options and preference settings.

I do use Resharper's code clean up feature to group all the methods in a #region, properties in their #region and so on. But that's code clean up and not VS settings.

You see the same kind of preferences with WPF projects too. Some people are happy with all the XAML attributes running on one line. I like to have one tag per line alphabetically, making all the tag consistent and easy to find what I'm looking for.
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5883
  • View blog
  • Posts: 12,765
  • Joined: 16-October 07

Re: Indenting for snippets

Posted 12 January 2013 - 04:52 AM

View PosttlhIn`toq, on 11 January 2013 - 06:25 PM, said:

That is the brace pattern for JAVA but it isn't followed for C#


Or K&R. Or, well, me. :P It is called a convention for a reason. There is no single style, though the VS defaults are C# conventions.

First thing I do when setting up VS is go to Options > Text Editor > C# > Formatting > New Line. You see all those check boxes? Uncheck them all! There, much better.

Once you have VS set up the way you like it, it will format any code for you. If the template code that pops in doesn't conform to your preferences, cut it and paste it back. The formatter will then apply your settings.
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3641
  • View blog
  • Posts: 12,614
  • Joined: 12-December 12

Re: Indenting for snippets

Posted 12 January 2013 - 05:05 AM

Yeah! S*d C# conventions! I'M IN CHARGE :bananaman:
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3641
  • View blog
  • Posts: 12,614
  • Joined: 12-December 12

Re: Indenting for snippets

Posted 12 January 2013 - 05:19 AM

Quote

Blow, winds, and crack your cheeks! rage! blow!
You cataracts and hurricanoes, spout
Till you have drenched our steeples, drowned the cocks!

Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3628
  • View blog
  • Posts: 11,320
  • Joined: 05-May 12

Re: Indenting for snippets

Posted 12 January 2013 - 11:11 AM

And then what happens when you run StyleCop and it starts whining about formatting? Just disable the StyleCop "errors" without telling to rest of the team?

LOL! I have my own personal style and I just disable StyleCop and fix things up right before checking in code.

This post has been edited by Skydiver: 12 January 2013 - 11:17 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1