Reputation: 18895
This question is similar to Disabling single line copy in Visual Studio, except that I'm wanting to change it to just copy the word the cursor is on, if nothing is not selected. If it's on white space, sure, I don't care, copy the line, but 99% I'm trying to copy a word, not the line Is this possible?
Upvotes: 2
Views: 634
Reputation: 27910
To copy the word the caret is on, you can assign a shortcut to the following Visual Commander (developed by me) command (language C#):
public class C : VisualCommanderExt.ICommand
{
public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package)
{
this.DTE = DTE;
EnvDTE.TextSelection ts = TryGetFocusedDocumentSelection();
if (ts != null && ts.IsEmpty)
CopyWord(ts);
else if (IsCommandAvailable("Edit.Copy"))
DTE.ExecuteCommand("Edit.Copy");
}
private void CopyWord(EnvDTE.TextSelection ts)
{
EnvDTE.EditPoint left = ts.ActivePoint.CreateEditPoint();
left.WordLeft();
EnvDTE.EditPoint right = ts.ActivePoint.CreateEditPoint();
right.WordRight();
System.Windows.Clipboard.SetText(left.GetText(right));
}
private EnvDTE.TextSelection TryGetFocusedDocumentSelection()
{
try
{
return DTE.ActiveWindow.Document.Selection as EnvDTE.TextSelection;
}
catch(System.Exception)
{
}
return null;
}
private bool IsCommandAvailable(string commandName)
{
EnvDTE80.Commands2 commands = DTE.Commands as EnvDTE80.Commands2;
if (commands == null)
return false;
EnvDTE.Command command = commands.Item(commandName, 0);
if (command == null)
return false;
return command.IsAvailable;
}
private EnvDTE80.DTE2 DTE;
}
Upvotes: 4