Load text into array C# problem

Anonymous
2012-09-05
2015-01-09
  • Anonymous - 2012-09-05

    Hello there guys im triying to make a n++  plug in C# and I want to load the text into an array to then manage it. Im really having a problem figuring out how to be able to do this. Cant find in the demo Plugin how to read a file. The scintilla.org documentation is not easy enough for me to figure it out. Any idea where i can find more info ?

     
  • Anonymous - 2012-09-06

    so far i got this

                // get document length
                int doc_length = Convert.ToInt32(Win32.SendMessage(GetCurrentScintilla(), SciMsg.SCI_GETLENGTH, 0, 0));
               
                //create array
                char* p_doc_text = new char*;

                // Open a new document.
                //Win32.SendMessage(nppData._nppHandle, NppMsg.NPPM_MENUCOMMAND, 0, NppMenuCmd.IDM_FILE_NEW);

                // read text into p_doc_text char array 
                Win32.SendMessage(GetCurrentScintilla(), SciMsg.SCI_GETTEXT, doc_length + 1,  &p_doc_text);

     
  • BGuenthner

    BGuenthner - 2012-09-07

    I grabbed some of my code out of my plugin and put it into some functions to demonstrate how to accomplish this.

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Text;
    using NppPluginNET;
    namespace XPatherizerNPP
    {
        class NPPPulgin
        {
            /// <summary>
            /// Get the text of a document.
            /// </summary>
            /// <param name="curScintilla">The Schiintilla of the document to get.</param>
            /// <returns></returns>
            public static string GetDocumentText(IntPtr curScintilla)
            {
                int length = (int)Win32.SendMessage(curScintilla, SciMsg.SCI_GETLENGTH, 0, 0) + 1;
                StringBuilder sb = new StringBuilder(length);
                Win32.SendMessage(curScintilla, SciMsg.SCI_GETTEXT, length, sb);
                return sb.ToString();
            }
            /// <summary>
            /// Get the text of the current active document.
            /// </summary>
            /// <returns></returns>
            public static string GetCurrentDocumentText()
            {
                IntPtr curScintilla = PluginBase.GetCurrentScintilla();
                return GetDocumentText(curScintilla);
            }
            /// <summary>
            /// Get the text of all open documents into an ArrayList.
            /// </summary>
            /// <returns></returns>
            public static ArrayList GetAllDocumentText()
            {
                ArrayList DocumentText = new ArrayList();
                for (int window = 0; window < 2; window++)
                {
                    int currentdoc = -1; int newdoc = -1;
                    while (currentdoc == newdoc)
                    {
                        newdoc++;
                        Win32.SendMessage(PluginBase.nppData._nppHandle, NppMsg.NPPM_ACTIVATEDOC, window, newdoc);
                        currentdoc = (int)Win32.SendMessage(PluginBase.nppData._nppHandle, NppMsg.NPPM_GETCURRENTDOCINDEX, 0, window);
                        if (currentdoc == newdoc)
                        {
                            DocumentText.Add(GetCurrentDocumentText());
                        }
                    }
                }
                return DocumentText;
            }
        }
    }
    
     
  • Anonymous - 2012-09-07

    Thank you !!!!!!

    i was close but my experience in C# is not that much, with all that i should be able to figure it out.

     
  • Shaleen Mishra

    Shaleen Mishra - 2014-10-16

    Do you have a sample of how to get hold of the selected text in current document?

     
  • Adrian

    Adrian - 2015-01-09

    Shaleen,

    The code below may help you. To get the selected text:

    int selectionLength = (int)Win32.SendMessage(GetCurrentScintilla(), SciMsg.SCI_GETSELTEXT, 0, 0);
    StringBuilder inputText = new StringBuilder(selectionLength);
    Win32.SendMessage(GetCurrentScintilla(), SciMsg.SCI_GETSELTEXT, 0, inputText);
    

    The code below replaces the selected text with new text.

    StringBuilder newText = ...;
    Win32.SendMessage(GetCurrentScintilla(), SciMsg.SCI_REPLACESEL, 0, newText);
    
     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks