I'm writing a test for my C# diagnostic analyzer with code fixes, which requires input code and expected fixed code.
The most simple way to specify the codes is to write them as string literal.
However, it bothers me because there is no syntax highlighting in the string literal.
So I thought it would be better to write the codes as separate files.
At this time I'm considering the following syntax:
class C
{
void M()
{
var i = 0;
- {|#0:i = 1|};
+ var i1 = 1;
}
}
- and + represent the rows to be deleted and added by the code fix, respectively, and {|#n:...|} indicates the location of the diagnostic result.
However, writing in a separate file does not solve the syntax highlighting problem.
I'd like to indicate deletions and additions with colors (like red and green) and also apply C# syntax highlighting.
So my question is, is there a simple way to apply my own syntax highlighting to a specific extension (e.g. cst) in Visual Studio?
If it is too difficult or too much work for the benefits, I'll give up.
Related
In Visual Studio you can minimize huge chunks of code using regions; they essentially just surround the code and minimize it in the window.
Does Sublime have a feature similar to this?
By default, you can select some code the go to Edit > Code Folding > Fold. There are tons of plugins that leverage the code-folding api for more options.
There's a request on the official site to "ask for features" here.
But apparently:
FYI, Jon has stated that this is not possible in the current
implementation of the editor control. Looks like we're waiting till V3
guys.
Jon being the programmer behind Sublime Text 2.
There might be a way to fake it by creating a plugin that looks for markers and removes the code region in between the markers, but it probably wouldn't look good. With the current API, it's probably your best bet!
By the way, there is some code folding in Sublime Text, if you hover your mouse next to the line number, you will see some arrows appearing when you can fold / unfold.
I ended up using custom comment tags, indented one level less than the code I want to fold. It doesn't look best, though it serves its purpose.
class Foobar {
// ...some code
// <fold
function foo() {
}
function bar() {
}
// </fold
// more code...
}
This (at the moment) folds to:
class Foobar {
// ...some code
// <fold[...]
// </fold
// more code...
}
Having a native ST2 support for this would be nice.
This looks what you are looking for. You can define tags for #region and #endregion for each language, or a generic tag for all of them.
If you are obsessed with intendation, this solution may make you uncomfortable but here it is, once upon a time while I had been writing a semi-complex jQuery plugin I've had constants, variables, private and public functions sections and foldings like so;
;(function($, undefined, window) {...
/* Consts */
var FOO = "BAR";
/* Variables */
var m_Foo = "bar";
/* Functions */
/* Public Functions */
function foo() {...}
function bar() {...}
/* Private Functions */
function _foo() {...}
function _bar() {...}
})(jQuery, window);
As you can see it is all about intendation. Sections can be folded; Consts, Variables, Functions. And also inside Functions section, Public Functions and Private Functions are both can be folded.
You can also use one line comment (//) to name your fold. So the idea underneath that is simple; ST2 thinks that the more intended lines belongs to first less-intended comment above them, like C/C++ compilers how handle brackets as own unique code blocks.
To fold the code select the code and press
ctrl + shift + [
To unfold the code put the cursor there and press
ctrl + shift + ]
I think that like myself, the OP has come to appreciate a little-known feature in VS called regions that many equate to code-folding, but is FAR more powerful and above, Dio Phung provided the answer that I wanted, and I suspect the OP wanted, but he didn't share as an answer so here it is.
The difference between "code-folding" as it's provided in Sublime Text is that it's based on code/compiler syntax while "regions" and what this plugin does, allow you infinitely more freedom, though it's a freedom that's more or less dependant on the code you're working with to begin with (deeply nested, or properly modularized).
If you are on Sublime Text 3, here is a plugin that can do it :
github.com/jamalsenouci/sublimetext-syntaxfold – Dio Phung
In languages which support 3 types of comments (e.g. PHP) I use the hashtag comment for regions, as shown in the other answers. It's also good for keeping track of what's being done
# default options
$a = 3;
$b = 'bob';
$old_code = 1;
# bugfix #130
$result = magic_function($data);
fix_stuff($result);
$old_code = $result;
Otherwise use triple slash ///, or //# etc.
In sublime text, it works like this, it shades the lines you want to collapse and presses (Control + Shift +?)
I have the most recent version of sublimetext.
I have a JavaScript line similar to:
var a = (b / 2) + (c / 2);
In Dreamweaver, it highlights this segment as a comment and treats it like this:
var a = (b /* 2 ) + (c */ 2);
It's incorrect syntax highlighting and very annoying. Where do I find syntax highlighting definitions and how do I modify them to correct this?
You can delete/modify the regex definition yourself by finding CodeColoring.xml in your Dreamweaver's configuration path. For CS6 in Windows 7, the default is:
C:\Program Files (x86)\Adobe\Adobe Dreamweaver CS6\configuration\
You will then need to find the JavaScript scheme:
<scheme MMString:name="JavaScript/scheme/name" id="JavaScript" ...>
And within it, you'll find the regexp definition:
<regexp name="RegExp" id="CodeColor_JavascriptRegexp" delimiter="/" escape="\\">
<searchPattern><![CDATA[/\e+\\/]]></searchPattern>
</regexp>
This can probably be refined, but I don't use regular expressions in most scenarios so I just deleted this segment. Restart DW, and voila.
If you want to refine the definition, StackOverflow seems to have its regex highlighting down:
var regex = /a+b/;
var number = (window.innerWidth / window.innerHeight) / 2;
This article explains how to do it:
Modifying Dreamweaver’s syntax highlighting
http://realworldz.wordpress.com/2007/10/04/modifying-dreamweavers-syntax-highlighting/
It gives an example of how to add syntax highlighting to the keyword new for VBScript:
Close Dreamweaver if it’s already open.
Go to C:\Documents and Settings\<YOUR USERNAME>\Application Data\Macromedia\Dreamweaver 8\Configuration\CodeColoring
Open the “ASP VBScript.xml” file in Notepad.
Look for the tags and after the one for “Mod”, add in a new one called for the keyword “New” like this; <keyword>New</keyword>
I have looked everywhere and I agree with the answer from this previous question: Dreamweaver CS5 code hinting
There is just not that much control over syntax highlighting. You can use the method described by Robert's answer here to add more reserved words and such. But editing that file does not apply to changing how Dreamweaver handles highlighting of constants and operators.
Here is a reasonable way to change the way you write you code so that you syntax will still be highlighted in examples like this.
<script>
var a =
(b/2) //Because the forward slashes are not on the same line,
+(c/2); //Dreamweaver will not stop highlighting the numbers
//and operators
document.write(a);
</script>
Note: in this specific example your code can also be simplified to var a = (b+c)/2;
Since the characters /([{;,=!|&~^<>+-*%?:} can't be followed by a division sign, this is what I came up with:
<searchPattern><![CDATA[/\s+/\e+\\/]]></searchPattern>
(this one must have at least one whitespace character to avoid conflicts with end-of-line comments)
<searchPattern><![CDATA[(\s*/\e+\\/]]></searchPattern>
<searchPattern><![CDATA[[\s*/\e+\\/]]></searchPattern>
<searchPattern><![CDATA[{\s*/\e+\\/]]></searchPattern>
<searchPattern><![CDATA[;\s*/\e+\\/]]></searchPattern>
<searchPattern><![CDATA[,\s*/\e+\\/]]></searchPattern>
<searchPattern><![CDATA[=\s*/\e+\\/]]></searchPattern>
<searchPattern><![CDATA[!\s*/\e+\\/]]></searchPattern>
<searchPattern><![CDATA[|\s*/\e+\\/]]></searchPattern>
<searchPattern><![CDATA[&\s*/\e+\\/]]></searchPattern>
<searchPattern><![CDATA[~\s*/\e+\\/]]></searchPattern>
<searchPattern><![CDATA[^\s*/\e+\\/]]></searchPattern>
<searchPattern><![CDATA[<\s*/\e+\\/]]></searchPattern>
<searchPattern><![CDATA[>\s*/\e+\\/]]></searchPattern>
<searchPattern><![CDATA[+\s*/\e+\\/]]></searchPattern>
<searchPattern><![CDATA[-\s*/\e+\\/]]></searchPattern>
<searchPattern><![CDATA[*\s*/\e+\\/]]></searchPattern>
<searchPattern><![CDATA[%\s*/\e+\\/]]></searchPattern>
<searchPattern><![CDATA[?\s*/\e+\\/]]></searchPattern>
<searchPattern><![CDATA[:\s*/\e+\\/]]></searchPattern>
<searchPattern><![CDATA[}\s*/\e+\\/]]></searchPattern>
For scenarios like if (x) /foo/.exec('bar'), just put the RegExp in parenthesis to format it properly. The only other ill effect is that the preceding character will just be formatted as regular text (not bold or colored).
in VS 2010 Ultimate if You type a JS code an then press Enter, You'll notice that the 1st bracket is in the same line as e.g function header. How to turn it off ? It is very annoying for me
after pressed enter... =>
function a() {
}
I want it as:
function a()
{
}
Using braces on the same line as the function declaration is proper JavaScript coding style (see Crockford). Using braces on the same line as the opening block is recommended due to the way JavaScript inserts semicolons wherever possible. Take, for example, this code:
return
{
hello: "world"
};
JavaScript parsers will rewrite this as:
return;
{
hello: "world"
};
This has a substantially different meaning, and there is no warning to the developer that this has happened, other than incorrect behavior from their script. While function declarations are ok, since function foo(); is not valid JavaScript, and parsers will therefore not insert a semicolon there, such formatting is still strongly discouraged.
If you still want to do this, you can alter this setting: Tools - Options - Text Editor - JScript - Formatting - Place open brace on new line for functions.
Code convention aside, the option is there in Visual Studio 2010 (Premium in my case)
Tools > Options > Text Editor > JScript > Formatting > Place open brace on new line for functions [check]
I want to modify Xcode syntax highlighting. Namely, I do a lot of 'NSAsserts', which I find visually distracting, and so I would like lines starting with 'NSAssert' to be a light gray. This way, I can focus upon my code logic instead of having to cognitively filter-out the NSAssert lines.
I use a lot of these too, and I liked your idea enough to work out the answer. Well, sort of: I have not worked out how to treat NSAsserts as a new item but I have worked out how to make them appear as comments in the syntax highlighter.
Create the directory ~/Library/Application Support/Developer/Shared/Xcode/Specifications
Copy BaseSupport.xclangspec from /Developer/Library/PrivateFrameworks/XcodeEdit.framework/Versions/A/Resources to that directory
Apply this patch to the new copy:
--- /Developer/Library/PrivateFrameworks/XcodeEdit.framework/Versions/A/Resources/BaseSupport.xclangspec 2010-10-05 00:27:45.000000000 +0100
+++ /Users/philwill/Library/Application Support/Developer/Shared/Xcode/Specifications/BaseSupport.xclangspec 2010-12-14 11:36:51.000000000 +0000
## -100,9 +100,8 ##
Identifier = "xcode.lang.comment.singleline";
BasedOn = "xcode.lang.comment"; // for text macros
Syntax = {
- Start = "//";
- EscapeChar = "\\";
- Until = "\n";
+ StartChars = "/N";
+ Match=("//.*$","NSC?Assert[12345]?[[:space:]]*\\([^;]*\\)[[:space:]]*;");
IncludeRules = ( "xcode.lang.url", "xcode.lang.url.mail", "xcode.lang.comment.mark" );
Type = "xcode.syntax.comment";
};
Caveats:
This will mess up any //-comments
which contain escaped newline
characters. Don't do that.
This will theoretically slow down
syntax highlighting a little. I
haven't noticed any difference.
This will affect all languages you use in Xcode which normally allow //-comments.
This is the best that Xcode currently permits you to do.
This question already has answers here:
How can I reverse code around an equal sign in Visual Studio?
(6 answers)
Closed 4 years ago.
I have a bunch of assignment operations in Visual Studio, and I want to reverse them:
i.e
i = j;
would become
j = i;
i.e. replacing everything before the equals with what's after the equals, and vice versa
Is there any easy way to do this, say something in the regular expression engine?
Select the lines you want to swap, Ctrl+H, then replace:
{:i}:b*=:b*{:i};
with:
\2 = \1;
with "Look in:" set to "Selection"
That only handles C/C++ style identifiers, though (via the ":i"). Replace that with:
{.*}:b*=:b*{.*};
to replace anything on either side of the "=".
Also, since you mentioned in a comment you use ReSharper, you can just highlight the "=", Alt+Enter, and "Reverse assignment".
Just a slight improvement on Chris's answer...
Ctrl+H, then replace:
{:b*}{[^:b]*}:b*=:b*{[^:b]*}:b*;
with:
\1\3 = \2;
(better handling of whitespace, esp. at beginning of line)
EDIT:
For Visual Studio 2012 and higher (I tried it on 2015):
Replace
(\s*)([^\s]+)\s*=\s*([^\s]+)\s*;
with:
$1$3 = $2;
In Visual Studio 2015+ after selecting code block press Ctrl + H (Find & Replace window) and check "Use Regular Expression" option, then:
Find: (\w+.\w+) = (\w+);
Replace: $2 = $1;
For example:
entity.CreateDate = CreateDate;
changes to:
CreateDate = entity.CreateDate;
Thank you #Nagesh and Revious, mentioned details added.
The robust way to do this is to use a refactoring tool. They know the syntax of the language, so they understand the concept of "assignment statement" and can correctly select the entire expression on either side of the assignment operator rather than be limited to a single identifier, which is what I think all the regular expressions so far have covered. Refactoring tools treat your code as structured code instead of just text. I found mention two Visual Studio add-ins that can do it:
ReSharper
MZ-Tools
(Inverting assignment isn't technically refactoring since it changes the behavior of the program, but most refactoring tools extend the meaning to include other generic code modifications like that.)
Please see this question: Is there a method to swap the left and right hand sides of a set of expressions in Visual Studio?
My answer to that question has a macro that you can use to swap the assignments for a block of code.
I've improved the expression a little.
Replace
(\t+)(\s*)(\S*) = (\S*);
with
$1$2$4 = $3;
The reason is, it will look for lines starting with tab (\t). It will skip the lines starting with definition. E.g.:
TestClass tc = new TestClass();
int a = 75;
int b = 76;
int c = 77;
a = tc.a;
b = tc.b;
a = tc.c;
Would ignore the int a, int b and int c and swap only the assignments.
what about replace all (CTRL-H)
you can replace for example "i = j;" by "j = i;"
you can use regular expressions in that dialog. I'm not so sure about how you should pop-up help about them however. In that dialog, press F1, then search that page for more information on regular expressions.
I like this dialog because it allows you to go through each replacement. Because the chance of breaking something is high, I think this is a more secure solution
You can do search and replace with regular expressions in Visual Studio, but it would be safer to just do a normal search and replace for each assignment you want to change rather than a bulk change.
Unfortunatly I don't have Visual Studio, so I can't try in the target environment, but if it uses standard regexps, you could probably do it like this:
Search for "(:Al) = (:Al);", and replace with "\2 = \1". (\1 and \2 are references to the first and second capture groups, in this case the parenthesises around the \w:s)
EDIT
Ok, not \w... But according to MSDN, we can instead use :Al. Edited above to use that instead.
Also, from the MSDN page I gather that it should work, as the references seem to work as usual.