XCode changing default behaviour of comment shortcuts - xcode

Is there a way to customise XCode comment actions. Example:
original
func thisfunc() {
var x = 5
}
when commented (cmd + /)
func thisfunc() {
// var x = 5
}
not
func thisfunc() {
// var x = 5
}
In the default behaviour XCode puts the comment in the beginning of the line but I want it to be like the first commented example (like sublime text comment blocks).

I don't think you can customize XCode the way you want. However there's still a way to do it by the steps are (1) selecting commented code (2) cmd + [ (3) cmd + / (4) ctrl + i. Have a try and this is not that complicated.

Related

Swift 2 arch4random [duplicate]

This question already has an answer here:
How to generate random numbers without repetition in Swift? [duplicate]
(1 answer)
Closed 7 years ago.
In my application, I use something like this to get random text on my label, except in my main let randomNumbercode, in xCode, it has over 300 cases, to much to paste in here.:
let randomNumber = Int(arc4random_uniform(23))
var textLabel = "" as NSString
switch (randomNumber){
case 1:
textLabel = "Kim."
break
case 2:
textLabel = "Phil."
break
case 3:
textLabel = "Tom"
break
case 4:
textLabel = "Jeff"
break
default:
textLabel = "Austin"
}
self.randomLabel.text = textLabel as String
But the problem is, that sometimes it shows the same text on the label 5-6 times, and other cases is not even used yet, because it choose randomly. So how can I choose randomly, but if case example case 1 is already shown, it wont show up again, until all other cases has been shown.
Have an array of Names instead of a gigantic switch case:
var names = ["Kim.", "Phil.", "Tom", "Jeff", "Austin"] // and all your remaining names
let originalNames = names
func getRandomName() -> String {
if (names.count == 0) {
names = originalNames
}
let randomNumber = Int(arc4random_uniform(UInt32(names.count)))
return names.removeAtIndex(randomNumber)
}
This ensures every name gets printed before starting from the beginning again. The sample output is:
Tom, Kim., Austin, Phil., Jeff
and then it starts again
Austin, Jeff, Phil. ...
Finally put something like the following wherever it fits your need:
self.randomLabel.text = getRandomName()

Toggling NSWindow level for "always on top" behaviour

I have a simple single-window application with a menu item that allows users to have the NSWindow appear always on top.
My function looks like this:
#IBAction func changeAlwaysOnTop(sender: AnyObject) {
if (alwaysOnTopMenuItem.state == NSOnState) {
alwaysOnTopMenuItem.state = NSOffState;
window.level = kCGNormalWindowLevelKey;
} else {
alwaysOnTopMenuItem.state = NSOnState;
window.level = kCGStatusWindowLevelKey;
}
}
Turning "Always on Top" on works well - the window floats above all other applications as it should. However, when the option is turned off, the window continues to float above all other windows, as if window.level = kCGNormalWindowLevelKey; isn't actually doing anything.
I've tried different window levels, and I've tried ordering the window out and back in again. The window continues to float above all others.
How can I set the window back to normal after setting the level to kCGNormalWindowLevelKey?
Edit: the following Objective C code works just fine:
- (IBAction)changeOnTop:(id)sender {
if (self.onTopMenuItem.state == NSOnState) {
self.onTopMenuItem.state = NSOffState;
self.window.level = NSNormalWindowLevel;
} else {
self.onTopMenuItem.state = NSOnState;
self.window.level = NSStatusWindowLevel;
}
}
You are using the wrong values for the level. You are using the keys by which window levels are looked up.
First, since you're using Cocoa, you should use the Cocoa constants for the window level: NSNormalWindowLevel and NSStatusWindowLevel.
If you look at the definitions of those constants, you'll find:
#define NSNormalWindowLevel kCGNormalWindowLevel
#define NSStatusWindowLevel kCGStatusWindowLevel
Notice the lack of the word "Key" on the end of those kCG... constants. If you then look up how those constants are defined, you'll find:
#define kCGNormalWindowLevel \
CGWindowLevelForKey(kCGNormalWindowLevelKey)
#define kCGStatusWindowLevel \
CGWindowLevelForKey(kCGStatusWindowLevelKey)
So, the values you used are keys that are passed to CGWindowLevelForKey() to get the actual level.
This works for me, and looks a bit nicer! (Swift 4)
window.level = .floatingWindowLevel
This worked for me in Swift 4.2.
override func viewDidAppear() {
super.viewDidAppear()
view.window?.level = .floating
}

Select hyphenated word with double-click

UPDATE: Per the recommendation below, here's specifically what I'd like to do: If I double-click the mouse cursor anywhere from the "b" to the "n" of "blue-green", I want all of the word "blue-green" should be highlighted. How can this be done? Currently, depending on where you click, it treats "blue-green" as three separate character strings. So, if you double click between the "b" and "e" of "blue" it highlights only "blue" and not "-green." If you double-click the hyphen, it highlights the hyphen alone. And if you double-click between the "g" and "n" of "green" it highlights only "green" and not "blue-".
ORIGINAL: When I double-click a hyphenated word or set of characters (e.g. "123-abc" or "blue-green" etc.), only the part of the word that I double-clicked is highlighted. I'd like the whole word to be highlighted.
I'm using Windows 7 Pro. If it needs to be done on a per-application basis, I'm most interested in fixing it for Google Chrome, but any Windows-compatible web browser would be OK.
Old question, but I happen to have been working on the same issue. Here's my solution:
jsFiddle.net
"use strict"
// Tweak to make a double-click select words with hyphens
//
// As of 2016-0816, None of the major Mac browser selects whole words
// with hyphens, like "ad-lib". This tweak fixes the hypen issue.
//
// Note: Firefox 48.0 doesn't automatically select whole words with
// apostrophes like "doesn't". This tweak also treats that.
;(function selectWholeWordsWithHyphens(){
var pOutput = document.getElementById("output")
var selection = window.getSelection()
// Regex designed to find a word+hyphen before the selected word.
// Example: ad-|lib|
// It finds the last chunk with no non-word characters (except for
// ' and -) before the first selected character.
var startRegex = /(\w+'?-?)+-$/g
// Regex designed to find a hyphen+word after the selected word.
// Example: |ad|-lib
var endRegex = /^-('?-?\w+)+/
// Edge case: check if the selection contains no word
// characters. If so, then don't do anything to extend it.
var edgeRegex = /\w/
document.body.ondblclick = selectHyphenatedWords
function selectHyphenatedWords(event) {
if (!selection.rangeCount) {
return
}
var range = selection.getRangeAt(0)
var container = range.startContainer
var string = container.textContent
var selectionUpdated = false
if (string.substring(range.startOffset, range.endOffset)
.search(edgeRegex) < 0) {
// There are no word characters selected
return
}
extendSelectionBackBeforeHypen(string, range.startOffset)
extendSelectionForwardAfterHyphen(string, range.endOffset)
if (selectionUpdated) {
selection.removeAllRanges()
selection.addRange(range)
}
function extendSelectionBackBeforeHypen(string, offset) {
var lastIndex = 0
var result
, index
string = string.substring(0, offset)
while (result = startRegex.exec(string)) {
index = result.index
lastIndex = startRegex.lastIndex
}
if (lastIndex === offset) {
range.setStart(container, index)
selectionUpdated = true
}
}
function extendSelectionForwardAfterHyphen(string, offset) {
if (!offset) {
return
}
string = string.substring(offset)
var result = endRegex.exec(string)
if (result) {
range.setEnd(container, offset + result[0].length)
selectionUpdated = true
}
}
}
})()
It's a standard through all programs that it will do that because they all run off the operating system's typing configuration/program thing. To fix it you would need to do something in System32. I don't know what you would need to do but I suspect this is your problem. You should probably go into more detail though about specifically what it is you want.

Why Does VS 2010 'Comment' Keyboard Shortcut Change in C++?

For me, Visual Studio's Ctrl + K, Ctrl + C keyboard shortcut is used to comment-out the selected lines. When editing C++, this sometimes uses block comments (/* */) and sometimes uses line comments (//). Why does it change? How does it decide which to use when?
A couple other discussions on the topic:
Visual studio feature - commenting code Ctrl K - Ctrl C
visual studio C++ toggle comment ? comment while not whole line is selected?
Based on my own tinkerings, and what was said in those articles...
It's based on the start/end of the selection. It seems to use double slashes // whenever you start your selection at the beginning of the line AND end it at the end of a line.
It will use /* */ notation whenever the selection occurs midway through lines.
IE:
If I have the code
int main () {
return 0;
}
and highlight only int main, it will convert it to /*int main*/.
If I highlight the entire code section, starting after the indent tab, it will convert it to
/*int main () {
return 0;
}*/
But if I highlight the section starting before the indent tab, it converts it to
//int main () {
// return 0;
//}
Summary of links under Zhais' answer. Because following links is hard!
Selecting entire lines (including leading whitespace) will use //
Selecting at least one partial line
If a // comment is included, will use //
Otherwise, will use /* */

Broken toggle-comment in Textmate

I'm having a problem with the Toggle Comment command ("Comment Line / Selection") in TextMate for Actionscript 2 (I know, I know). I've tried completely stripping the language set down to isolate the issue, and tried walking through the Ruby, both to no avail. My issue is that the command insists on using block comments for comment toggling (⌘ + /) and doesn't respect when I add a preferences file to change TM_COMMENT_MODE. I even tried using this simple preference:
{ shellVariables = (
{ name = 'TM_COMMENT_START';
value = '// ';
},
);
}
but no luck. I'm hoping that someone who speaks Ruby much better than myself (ie. at all) can find a simple fix for this. You can reproduce in any (recent) install of TextMate by creating a new actionscript 2 file and trying to ⌘ + / a section of code (or even a line). Contrast to a JS file which will use a line comment. Copy the "Comments" snippet from JavaScript to Actionscript bundles, and the problem will persist.
Thanks!
In your ActionScript Bundle, add a Preference called "Comments". In the editor part, add:
{ shellVariables = (
{ name = 'TM_COMMENT_START';
value = '// ';
},
{ name = 'TM_COMMENT_DISABLE_INDENT';
value = 'YES';
},
{ name = 'TM_COMMENT_START_2';
value = '/* ';
},
{ name = 'TM_COMMENT_END_2';
value = '*/';
},
);
}
and finally at the bottom, set the scope selector to: scope.actionscript.2
Here is an image of what mine looks like
be sure to use the Reload Bundles menu item after you've made these changes.

Resources