如何在 Visual Studio 2017 中更改 Ctrl+C 以复制单词而不是整行
How to Change Ctrl+C in Visual Studio 2017 to Copy Word, Not Entire Line
这个问题与 Disabling single line copy in Visual Studio 类似,只是我想将其更改为仅复制光标所在的单词(如果未选择任何内容)。如果它是白色的 space,当然,我不在乎,复制该行,但 99% 我正在尝试复制一个单词,而不是该行 这可能吗?
要复制插入符号所在的单词,您可以为以下 Visual Commander(由我开发)命令(语言 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;
}
这个问题与 Disabling single line copy in Visual Studio 类似,只是我想将其更改为仅复制光标所在的单词(如果未选择任何内容)。如果它是白色的 space,当然,我不在乎,复制该行,但 99% 我正在尝试复制一个单词,而不是该行 这可能吗?
要复制插入符号所在的单词,您可以为以下 Visual Commander(由我开发)命令(语言 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;
}