VS file synchronization plug-in development, vs synchronization plug-in

Source: Internet
Author: User

VS file synchronization plug-in development, vs synchronization plug-in
I. Plug-in Function Description

The plug-in monitors an xml file. When a new element is added to the file, the new element is synchronized to the specified directory when it is saved.

Ii. Template Selection

This function is related to code editing. To monitor document saving events, you must register the event saving response method when the document is opened. VS provides an interface to listen to open events of a document. This interface is IWpfTextViewCreationListener. The interface code is as follows. This interface has only one method and will call the TextViewCreated Method for execution when the document is opened.

// Abstract: // Listens to text view created events. public interface IWpfTextViewCreationListener {// Abstract: // Called when a text view having matching roles is created over a text data // model having a matching content type. //// parameter: // textView: // The newly created text view. void TextViewCreated (IWpfTextView textView );}

You can select any template. You only need to change the plug-in entry class created by the template in the project to inherit the IWpfTextViewCreationListener interface and implement the interface. Because this feature requires option configuration, we recommend that you use the Visual Stuido Package template because it is easier to add option pages using this template. This is a tutorial on how to add the option page when using the Visual Studio Package template on msdn: http://msdn.microsoft.com/en-us/library/bb166195.aspx. I chose the Editor Text Adornment template, because the portal class created by this template inherits the IWpfTextViewCreationListener interface. However, it is more troublesome to add the option page when using this template.

3. Add option page

According to the tutorial on msdn, we know that we need to add two classes: the class that inherits Package and the class that inherits DialogPage. I will not repeat the process. If you follow the tutorial on msdn closely, you will find that there is no option page (not the Visual Studio Package template) under the tool-option, why? Some elements are missing in our csproj file and source. extension. vsixmanifest. You can add the option page by following the steps below:

1. Open source. extension. vsixmanifest, select the Assets option, and click the New button. The figure-1 window is displayed.

Figure-1

Select Microsoft. visual Studio. assembly, Source select A project in current solution, Project select current plug-in project, click OK to add, and then click the New button again, this time Type select Microsoft. visual Studio. vsPackage, Source and Project are the same as those of the first time.

2. Change the GeneratePkgDefFile element in the csproj file to true.

3. Add <CopyBuildOutputToOutputDirectory> true </CopyBuildOutputToOutputDirectory> to the GeneratePkgDefFile element.

4. Add <strong> true </strong deassemblyinvsixcontainer> before the GeneratePkgDefFile element. Note that the IncludeAssemblyInVSIXContainer element must be added before GeneratePkgDefFile and CopyBuildOutputToOutputDirectory.

After the four steps above, the options page we added will be displayed in tools-Options, if the first step is missing, an error occurs when loading this property page.

4. Listening for saving events

By viewing the textView parameter type of the TextViewCreated function, you can know that the IWpfTextView interface does not contain the event saved in the document. So how can we subscribe to save events? By searching for relevant materials, you can find the document saving events in the following ways:

//EnvDTE.DTE _dte
this._dte = ServiceProvider.GlobalProvider.GetService(typeof(DTE)) as DTE;
//EnvDTE.Events _eventsthis._events = this._dte.Events;
//EnvDTE.DocumentEvents _docEventsthis._docEvents = this._events.DocumentEvents;

Be sure to define the above three objects as global variables. Otherwise, you will not be able to respond to the saving event because of the garbage collection mechanism of C, you can define it as a local variable.

The following are some codes of this plug-in.

1 namespace deleettingssync 2 {3 public class TextViewListener 4 {5 /// <summary> 6 // document information 7 /// </summary> 8 private ITextView _ view; 9 10 private DTE _ dte; 11 private Events _ events; 12 private incluentevents _ docEvents; 13 /// <summary> 14 // whether the document has been modified 15 /// </summary> 16 private bool _ isChanged; 17 /// <summary> 18 /// whether to automatically synchronize data to other deleteheaders during storage. xml 19 // </summary> 20 private bool _ isAutoR Eplace = true; 21 /// <summary> 22 // producer etting that triggers the synchronization operation. xml 23 // </summary> 24 private string _ sourceFile; 25 /// <summary> 26 // The deletemetadata to be synchronized. directory of the xml file 27 /// </summary> 28 private string _ targetFolder; 29 30 /// <summary> 31 // 32 // triggered when the document is opened /// </summary> 33 // <param name = "textView"> </param> 34 public TextViewListener (IWpfTextView textView) 35 {36 this. _ view = textView; 37 this. _ dte = ServiceProvider. globalProvider. getService (typeof (DTE) as DTE; 38 Properties props = this. _ dte. get_Properties ("IStrong", "AppSettingsSync"); 39 if (props = null) 40 return; 41 this. _ sourceFile = (string) props. item ("SourceXmlFilePath "). value; 42 // File. appendAllText (@ "D: \ log.txt", "source file" + this. _ sourceFile + "current file" + this. _ dte. activeDocument. fullName); 43 if (! This. _ dte. activeDocument. fullName. equals (this. _ sourceFile, StringComparison. ordinalIgnoreCase) 44 return; 45 // obtain the DTE object 46 this. _ events = this. _ dte. events; 47 this. _ docEvents = this. _ events. documentEvents; 48 // subscription document save and modify event 49 this. _ docEvents. documentSaved + = _ docEvents_DocumentSaved; 50 this. _ view. textBuffer. changed + = TextBuffer_Changed; 51} 52 53 // <summary> 54 // document modification event 55 /// </summary> 56 /// <Param name = "sender"> </param> 57 // <param name = "e"> </param> 58 void TextBuffer_Changed (object sender, Microsoft. visual Studio. text. textContentChangedEventArgs e) 59 {60 if (e. changes. count ()> 0) 61 this. _ isChanged = true; 62} 63 64 // <summary> 65 // Document save event 66 // </summary> 67 // <param name = "Document"> </param> 68 async void _ docEvents_DocumentSaved (Document) 69 {70 try 71 {72 // File. appendAllText (@ "D: \ log.txt", "Save event triggering"); 73 // get Tool-> Opetions-> IStrong-> AppSettingsSync configuration item content 74 Properties props = this. _ dte. get_Properties ("IStrong", "AppSettingsSync"); 75 if (props = null) 76 return; 77 this. _ sourceFile = (string) props. item ("SourceXmlFilePath "). value; 78 // The Value must be the same as the source parameter ettings. the xml file and the file have been modified 79 if (Document. fullName. equals (this. _ sourceFile, StringComparison. ordinalI GnoreCase) & this. _ isChanged) 80 {81 this. _ isAutoReplace = (bool) props. item ("IsAutoSync "). value; 82 this. _ targetFolder = (string) props. item ("TargetFolder "). value; 83 // manually select the file to be synchronized 84 if (! This. _ isAutoReplace) 85 {86 SelectFiles sf = new SelectFiles (this. _ sourceFile, this. _ targetFolder); 87 sf. showDialog (); 88 this. _ isChanged = false; 89} 90 else 91 {92 // automatically synchronize the file 93 string fileName = System. IO. path. getFileName (this. _ sourceFile); 94 string [] files = Directory. getFiles (this. _ targetFolder, fileName, SearchOption. allDirectories); 95 this. _ isChanged = false; 96 await SyncHelper. syncAppS Ettings (this. _ sourceFile, files); 97 // after synchronization, modify the Visual Studio status bar information 98 IVsStatusbar = ServiceProvider. globalProvider. getService (typeof (SVsStatusbar) as IVsStatusbar; 99 bar. setText ("synchronized ettings configuration file synchronization completed. "); 100} 101} 102} 103 catch (Exception ex) 104 {105 MessageBox. show (ex. message, "prompt", MessageBoxButton. OK, MessageBoxImage. error); 106} 107} 108} 109}View Code

Obtain the topic color of.

/// <Summary> /// set the color of the modal window to the background color of Visual Studio /// </summary> /// <param name = "themeColor"> </ param> // <returns> </returns> private Color converVsThemeColor (vsThemeColors themeColor) {DTE2 dte2 = (EnvDTE80.DTE2) System. runtime. interopServices. marshal. getActiveObject ("VisualStudio. DTE.12.0 "); uint color = dte2.GetThemeColor (themeColor); int a = (int) color/0x1000000; int B = (int) (color-a * 0x1000000) /0x10000; int g = (int) (color-a * 0x1000000-B * 0x10000)/0x100; int r = (int) (color-a * 0x1000000-B * 0x10000-g * 0x100); return Color. fromArgb (0xFF, r, g, B );}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.