📃 Aktif Dokümanın İçeriğini Alma
Copy protected DTE2 dte;
dte2 = ( EnvDTE80 . DTE2 ) GetService ( typeof ( EnvDTE . DTE ));
public string GetCurrentTextFile (){
TextDocument doc = ( TextDocument )( dte . ActiveDocument . Object ( "TextDocument" ));
var p = doc . StartPoint . CreateEditPoint ();
string s = p . GetText ( doc . EndPoint );
return s;
}
📝 Editör Üzerindeki Seçili Metni Sıralama
Copy using EnvDTE80 ;
var dte = await ServiceProvider.GetServiceAsync(typeof(DTE)).ConfigureAwait(false) as DTE2 ?? throw new NullReferenceException("DTE alınamadı");
EnvDTE . TextSelection ts = dte . ActiveWindow . Selection as EnvDTE . TextSelection ;
if (ts == null )
return ;
string [] selectedLines = ts . Text . Split ( '\n' );
selectedLines = selectedLines . OrderBy (p => p). ToArray ();
ts . Text = string . Join ( "\n" , selectedLines);
👨💻 Editör Üzerindeki Seçili Metnin içerisindeki Method İçeriğini Sıralama
Copy using EnvDTE80 ;
var dte = await ServiceProvider.GetServiceAsync(typeof(DTE)).ConfigureAwait(false) as DTE2 ?? throw new NullReferenceException("DTE alınamadı");
EnvDTE . TextSelection ts = dte . ActiveWindow . Selection as EnvDTE . TextSelection ;
if (ts == null )
return ;
EnvDTE . CodeFunction func = ts . ActivePoint . CodeElement [ vsCMElement . vsCMElementFunction ] as EnvDTE . CodeFunction ;
if (func == null )
return ;
// Func içerğini al -> sırala -> güncelle
string selectedCodeText = func . GetStartPoint ( vsCMPart . vsCMPartBody ). CreateEditPoint (). GetText ( func . EndPoint );
selectedCodeText = string . Join ( "\n" , selectedCodeText . Split ( '\n' ). OrderBy (p => p));
func.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint().ReplaceText(func.EndPoint, selectedCodeText, (int) vsEPReplaceTextOptions.vsEPReplaceTextAutoformat);
📂 Aktif Dokümandaki Üretilen Kodları Sıralama
Copy using EnvDTE80 ;
private async void Execute ( object sender , EventArgs e) {
DTE2 dte = await Utility . GetDTE2Async (ServiceProvider);
ProjectItem tempProjectItem = dte2 . ItemOperations . AddExistingItem (tempFilePath);
if ( Utility . SortFunctionBodyIfExist ( tempProjectItem . FileCodeModel , Utility . GeneratedFunctionName ))
{
tempProjectItem . Save ();
string oldFilePath = filePath . Replace ( selectedProjectItem . Name , tempProjectItem . Name );
Utility . DiffFiles (dte2 , oldFilePath , filePath);
}
}
public static async Task<DTE2> GetDTE2Async(IAsyncServiceProvider asyncServiceProvider) => await asyncServiceProvider.GetServiceAsync(typeof(DTE)).ConfigureAwait(false) as DTE2 ?? throw new NullReferenceException("DTE alınamadı");
public static bool SortFunctionBodyIfExist ( FileCodeModel fcm , string funcName)
{
ThreadHelper . ThrowIfNotOnUIThread ();
if ( IsFuncExistInFileCodeModel (fcm , funcName , out CodeFunction cf))
{
SortFunctionBody (cf);
return true ;
}
return false ;
}
public static bool IsFuncExistInFileCodeModel ( FileCodeModel fcm , string name , out CodeFunction cf)
{
ThreadHelper . ThrowIfNotOnUIThread ();
return IsFuncExistInCodeElements ( fcm . CodeElements , name , out cf);
}
public static void SortFunctionBody ( CodeFunction cf)
{
ThreadHelper . ThrowIfNotOnUIThread ();
string generatedCode = GetFunctionBodyText (cf);
generatedCode = StripComments (generatedCode);
generatedCode = SortContentBy (generatedCode , '\n' );
ReplaceFunctionBodyText (generatedCode , cf);
}
public static bool IsFuncExistInCodeElements ( CodeElements codeElements , string name , out CodeFunction cf)
{
ThreadHelper . ThrowIfNotOnUIThread ();
foreach ( CodeElement element in codeElements)
{
if (element is CodeNamespace )
{
CodeNamespace nsp = element as CodeNamespace ;
foreach ( CodeElement subElement in nsp . Children )
{
if (subElement is CodeClass )
{
CodeClass c2 = subElement as CodeClass ;
foreach ( CodeElement item in c2 . Children )
{
if (item is CodeFunction )
{
CodeFunction _cf = item as CodeFunction ;
if ( _cf . Name == name)
{
cf = _cf;
return true ;
}
}
}
}
}
}
}
cf = null ;
return false ;
}
♊ İki Dosya Arasındaki Farklılıkları Gösterme
Copy string [] splitFilepath = filepath . Split ( '\\' );
string bareFilename = splitFilepath [ splitFilepath . Length - 1 ];
string tempFilepath = System . IO . Path . GetTempPath () + bareFilename;
System . IO . File . WriteAllText (tempFilepath , fileContent , System . Text . Encoding . UTF8 );
dte2 . ExecuteCommand ( "Tools.DiffFiles" , $"\"{tempFilepath}\" \"{filepath}\"" );
🚩 Proje dizinini ve dosya yolunu alma
Copy string filepath = "..." ;
string solutionDir = System . IO . Path . GetDirectoryName ( dte2 . Solution . FullName );
filepath = filepath . Replace ( $"{solutionDir}\\" , "" ). Replace ( "\\" , "/" );
Copy static System . Diagnostics . Process GitProcess ( string arguments , string workdir)
{
return new System . Diagnostics . Process
{
StartInfo = {
FileName = "git.exe" ,
WorkingDirectory = workdir ,
Arguments = $"--no-pager {arguments}" ,
RedirectStandardOutput = true ,
UseShellExecute = false ,
CreateNoWindow = true
} ,
EnableRaisingEvents = true
};
}
Copy string fileContent = "" ;
gitProcess . Start ();
while ( ! gitProcess . StandardOutput . EndOfStream )
{
string line = gitProcess . StandardOutput . ReadLine ();
fileContent += line + "\n" ;
}
👨💻 Dosyadan FileCodeModel
Oluşturma
Copy using EnvDTE ;
public static async Task<DTE2> GetDTE2Async(IAsyncServiceProvider asyncServiceProvider) => await asyncServiceProvider.GetServiceAsync(typeof(DTE)).ConfigureAwait(false) as DTE2 ?? throw new NullReferenceException("DTE alınamadı");
string filepath = "TODO" ;
DTE2 dte2 = await Utility . GetDTE2Async (asyncServiceProvider);
ProjectItem projectItem = dte2 . ItemOperations . AddExistingItem (filepath);
FileCodeModel fcm = projectItem . FileCodeModel ;
projectItem . Delete ();