VSCode keeps breaking small objects into multiple lines - format

I don't know what's causing this. I love having auto-format on save. But there is a rule that keeps making objects jump into multiple lines. When I type something like this:
const { promisify } = require("util");
VSCode formats it to:
const {
promisify
} = require("util");
It looks very ugly.

for html formatting:
"html.format.wrapAttributes": "force-aligned",

Nevermind I found it. It was the beautify plugin.

Related

qtcreator does not autocomplete when structure bindings is used?

I seem to have an issue with qtcreator not autocompleting my code, which is getting pretty annoying.
Currently is it not able to autocomplete when i try to use structure bindings in for loops like this..
std::vector<pair<string,AudioFile<double>>> list_of_files;
// Some init of list_of_file
for (const auto& [name,file]: this->list_of_files) // red line under this.. does seem to like structure bindings?
{
file.printSummary(); // qtcreator don't offer any autocomplete options?
}
qtcreator basically complains about everything about the code posted above..
But when I write it like this:
for (int i = 0 ; i <list_of_file.size() ; i++) // No red lines under this..
{
list_of_files[i].second.printSummary() // Autocompletes without any problems.
}
qtcreator does not complain about this code, and seem to autocomplete it just fine.. So why is it causing so many problems with c++17 style?
Any fixes for this?
A temporary solution for this seem to be something like this - which autocompletions does not complain about, and seem to suit my definition of (readability):
for ( const auto &elements : this->list_of_files)
{
auto name = std::get<0>(elements);
auto file = std::get<1>(elements);
}

How to get the entire Visual Studio active document... with formatting

I know how to use VS Extensibility to get the entire active document's text. Unfortunately, that only gets me the text and doesn't give me the formatting, and I want that too.
I can, for example, get an IWpfTextView but once I get it, I'm not sure what to do with it. Are there examples of actually getting all the formatting from it? I'm only really interested in text foreground/background color, that's it.
Note: I need the formatted text on every edit, so unfortunately doing cut-and-paste using the clipboard is not an option.
Possibly the simplest method is to select all of the text and copy it to the clipboard. VS puts the rich text into the clipboard, so when you paste, elsewhere, you'll get the colors (assuming you handle rich text in your destination).
Here's my not-the-simplest solution. TL;DR: you can jump to the code at https://github.com/jimmylewis/GetVSTextViewFormattedTextSample.
The VS editor uses "classifications" to show segments of text which have special meaning. These classifications can then be formatted differently according to the language and user settings.
There's an API for getting the classifications in a document, but it didn't work for me. Or other people, apparently. But we can still get the classifications through an ITagAggregator<IClassificationTag>, as described in the preceding link, or right here:
[Import]
IViewTagAggregatorFactoryService tagAggregatorFactory = null;
// in some method...
var classificationAggregator = tagAggregatorFactory.CreateTagAggregator<IClassificationTag>(textView);
var wholeBufferSpan = new SnapshotSpan(textBuffer.CurrentSnapshot, 0, textBuffer.CurrentSnapshot.Length);
var tags = classificationAggregator.GetTags(wholeBufferSpan);
Armed with these, we can rebuild the document. It's important to note that some text is not classified, so you have to piece everything together in chunks.
It's also notable that at this point, we have no idea how any of these tags are formatted - i.e. the colors used during rendering. If you want to, you can define your own mapping from IClassificationType to a color of your choice. Or, we can ask VS for what it would do using an IClassificationFormatMap. Again, remember, this is affected by user settings, Light vs. Dark theme, etc.
Either way, it could look something like this:
// Magic sauce pt1: See the example repo for an RTFStringBuilder I threw together.
RTFStringBuilder sb = new RTFStringBuilder();
var wholeBufferSpan = new SnapshotSpan(textBuffer.CurrentSnapshot, 0, textBuffer.CurrentSnapshot.Length);
// Magic sauce pt2: see the example repo, but it's basically just
// mapping the spans from the snippet above with the formatting settings
// from the IClassificationFormatMap.
var textSpans = GetTextSpansWithFormatting(textBuffer);
int currentPos = 0;
var formattedSpanEnumerator = textSpans.GetEnumerator();
while (currentPos < wholeBufferSpan.Length && formattedSpanEnumerator.MoveNext())
{
var spanToFormat = formattedSpanEnumerator.Current;
if (currentPos < spanToFormat.Span.Start)
{
int unformattedLength = spanToFormat.Span.Start - currentPos;
SnapshotSpan unformattedSpan = new SnapshotSpan(textBuffer.CurrentSnapshot, currentPos, unformattedLength);
sb.AppendText(unformattedSpan.GetText(), System.Drawing.Color.Black);
}
System.Drawing.Color textColor = GetTextColor(spanToFormat.Formatting.ForegroundBrush);
sb.AppendText(spanToFormat.Span.GetText(), textColor);
currentPos = spanToFormat.Span.End;
}
if (currentPos < wholeBufferSpan.Length)
{
// append any remaining unformatted text
SnapshotSpan unformattedSpan = new SnapshotSpan(textBuffer.CurrentSnapshot, currentPos, wholeBufferSpan.Length - currentPos);
sb.AppendText(unformattedSpan.GetText(), System.Drawing.Color.Black);
}
return sb.ToString();
Hope this helps with whatever you're doing. The example repo will ask if you you want the formatted text in the clipboard after each edit, but that was just a dirty way that I could test and see that it worked. It's annoying, but it was just a PoC.

Is there any way to set options in beautify extension for Ace Editor?

I've found the beautify extension in Ace editor but I don't see any examples of how to use it. Is there a way to set any options?
Example what I have so far:
var beautiful = ace.require("ace/ext/beautify"); // get extension
var editor = ace.edit("editor"); // reference to our editor
editor.setValue(someCode); // add some code to the editor
beautiful.beautify(editor.session); // beautify the code
When I call this method the code is formatted but it is all unindented / outdented all the way to the left and some spaces are removed. It doesn't look quite right. So I want to know if there are any options. I looked at the code but it is minified. Which is why I'm asking this question here.
Before call:
After call:
UPDATE:
I found an unminifed copy of the extension. It looks like there are no options and it looks like it only works for PHP:
exports.beautify = function(session) {
var iterator = new TokenIterator(session, 0, 0);
var token = iterator.getCurrentToken();
var context = session.$modeId.split("/").pop();
var code = phpTransform(iterator, context);
session.doc.setValue(code);
};
I did not find any options in the beautify.js code for the beautify() method. I found a comment saying that it is not being worked on or supported any more. I do not have the source but it said it was not working and was then abandoned.
It might work alright for JavaScript but is not working well for ActionScript.

Is there any setting in Xcode to have all autocomplete code follow a different curly bracket style?

Per Wikipedia, Xcode defaults to using the K&R brace/indentation style. It looks like this:
- (void) someFunction {
if (condition) {
// do this
} else
// do that;
}
}
I've grown up with a strong preference for Allman style, which looks like this:
- (void) someFunction
{
if (condition)
{
// do this
}
else
{
// do that
}
}
It's not too much of a chore just to return the bracket of a code-completed statement to the next line, but if I could tell Xcode to do this for me, it would save me a lot of meticulous click-and-return'ing.
I don't think you can remove the K&R braced code snippets, but you could simply create your own Allman-style snippets and add them to the Xcode "Code Snippet Library". It's a cumbersome solution, but it works.
So, when writing your snippets, you can use the <# and #> to delimit your expressions. Thus:
if (<#condition#>)
{
<#statements-if-true#>
}
else
{
<#statements-if-false#>
}
Then drag and drop that onto your snippet library:
And give it a meaningful name and shortcut:
Now when you start typing, you'll leverage the new snippet:
Go ahead and give your snippet a unique completion shortcut if you want to make disambiguation even easier.
There are more effective uses of the snippet library, but it can be used to address your problem.

CKEditor: How to stop angle brackets from converting to HTML entities

Our site is using tags like <# TAGNAME #> but CKEditor converts < and > to &lt and &gt which breaks these tags for use in our software.
I've discovered this option: config.protectedSource.push( /<#[\s\S]*##>/g ); which seems to stop the conversion if the data is saved from Source mode, but in WYSIWYG mode I can't find a way to stop the conversion. I've tried many options in their API but none of them seem to have helped, how can I fix this problem?
Were were looking at using CKEDitor to edit Smarty templates. The problem we were hitting was that it was replacing all the angle brackets and ampersands within the curly brackets, which messed everything up. This came up in a Google search so our solution should help anyone with similar issues.
CKEditor rebuilds the HTML every time you switch to Source mode and when you save, so you need to add to the HTML http://docs.cksource.com/CKEditor_3.x/Developers_Guide/Data_Processor htmlFilter.
This worked for us:
//replace Form_content with whatever your editor's id is.
htmlParser = CKEDITOR.instances.Form_content.dataProcessor.htmlFilter;
//We don't want HTML encoding on smarty tags
//so we need to change things in curly brackets
htmlParser.onText = function(text) {
//find all bits in curly brackets
var matches = text.match(/\{([^}]+)\}/g);
//go through each match and replace the encoded characters
if (matches!=null) {
for (match in matches) {
var replacedString=matches[match];
replacedString = matches[match].replace(/>/g,'>');
replacedString = replacedString.replace(/</g,'<');
replacedString = replacedString.replace(/&/g,'&');
text = text.replace(matches[match],replacedString);
}
}
return text;
}
The onText function processes all the bits that aren't in tags or comments.
I'd imagine you can do something similar by altering the code above - I've left it as is as I think our problems and required solutions are very similar.
editor.on( 'mode', function(ev) {
if ( ev.editor.mode == 'source' ) {
var str=ev.editor.getData();
str=str.replace(/&/g, "&").replace(/>/g, ">").replace(/</g, "<").replace(/"/g, "\"");
ev.editor.textarea.setValue(str);
}
});
http://cksource.com/forums/viewtopic.php?f=11&t=20647&start=10
If you type < or > in any WYSIWYG editor, they will be converted to their HTML entities in source mode.

Resources