building text editor with code completion feature - c#

Presently I'm trying to build up a text editor for the PHP language which should have the feature of code completion, i.e. if i start to type a word a dynamically created drop down list will display all available keywords starts with my typed letters. Can any body suggest me how it can be done. Idea will be enough for me. If possible please provide me a link to such simple application build in C#.

Considering that in general it's not an easy task, so there is no some "easy" application.
Even if in general idea is not a rocket science. You need to define a dictionary of words, corresponding to some key. When you type "." (in C#) you have to pick from the dictionary all words corresponding to the key equal to the word found on left side from the "."
To do it real working applicaiton is not so easy. By the way I can recommend to have a look on MonoDevlop look on their editor.
I worked with it years ago to make a simple editor for DSL company needed, and spend not small amount of time to correctly understand intenals, integrate well "my new language", "detach" control from Mono and inject into our applicaitons, like a dockable window.

If I where you I would really try to avoid the wheel... code completion is something that most IDE's now come with so what you are after is already available...
That being said, what I would try would be to go over the PHP API and construct a Suffix tree. This type of tree usually allow for a fast way to look for a given word. Once you index the API, you would also add in any other variable the user adds while he/she is performing the actual programming.
You could kick the search automatically in your suffix tree after the user has entered the 3rd letter, or maybe provide means for the user to activate it manually, like the Ctrl-Space keyboard most IDE's (Visual Studio, Netbeans, Eclipse, etc) have.
Note that this could get tricky, since you might want to select the appropriate variable type.

Related

can C# read in a xml file to change its gui at runtime

I am making an application that will generate and SQL scripts from a template and after taking input for different fields from the user.
There are many templates, so the GUI needs to adjust for the fields that the user will be filling out.
In the interest of keeping this scalable, I'd rather not hardcode the GUIs into the program, but would like have it read from an XML file and change based on the template the user has selected.
This is preferred because if a new template were to arise, then all that the program needs is a XML file that corresponds to the template. And the actual code does not need to be changed.
I have my eyes set on using C# for this, as I have good experiences using it.
I am open to suggestions for other languages though.
Edit: This is a project for work, and I wanted to be sure that this is possible with C# before convincing my employers to expand into using C#.
You could do this sort of thing by subclassing Windows.Forms.Form and adding a constructor to accept your XML file as a parameter. Add a parser for your XML file that will interpret instructions for which labels and fields you want to add to a consistent form design (say, two columns with a label for field name on the left and the actual input field on the right, easily achievable by filling the form with a TableLayoutPanel). You just need to lay out your design constraints from the beginning and stick to them.
This is essentially what visual studio does when you create a form through the designer anyway, so I'd suggest you start by creating an example form manually and just looking at the kind of code it places in the form's designer.cs file
Any language can do that. It's more about design patterns than specific technologies.
If you meant writing a GUI only declaratively and with XML, though, then no. You'd have to write your own parsing and GUI assembling code.

ANTLR Syntax Highlighting DSL in Visual Studio

I have an ANTLR grammar that defines a DSL (domain specific language). This grammar is relatively simple. It is parsing the language and outputting C code to create a very basic translator.
This language is meant to be used in C# application (typed into some sort of control, whether it be RichTextBox or a custom control) and one requirement is to have syntax highlighting for this language. I have scoured the Internet in hopes of finding some sort of information on how to accomplish this, or find a tool to make this a little easier on myself.
After not finding too much information, my best assumption would be that I need to use the ANTLR generated lexer to look at the tokens and color them accordingly. Is this the correct path of action, or is there some other method/tool to provide syntax highlighting for custom domain specific languages? If this is the correct method, how do I go about recognizing specific tokens?
If I left out any important information, please ask! Thanks!
I successfully used AvalonEdit for a similar project of mine. I just created a small editor with the correct syntax highlighting.
It is very easy and quick to get it up and running in your project. You just have to provide it with a simple XML file to document the syntax of your DSL and you will have a colored output out-of-the-box as a WPF control.
It looks like they added completion facilities since I used it, I don't have experience on that part though, but I suspect it is also very well done if the quality is the same as the colouring.
This language is meant to be used in C# application (typed into some sort of control, whether it be RichTextBox or a custom control) and one requirement is to have syntax highlighting for this language.
Consider using Scintilla for your control. It's a text control for IDE-style text editing. Notepad++ uses it for its text control, as does the SciTE IDE from which it originates. I've used it in a small, custom IDE project written in C# using an unofficial .NET-specific version -- I think it was ScintillaNET.
Scintilla supports custom keyword highlighting and also a variety of programmable features like squiggly-line underlining and things like that.
If you have a control that you'd rather use, I think it's reasonable to use a small ANTLR lexer to produce tokens for you. Each token contains the line number, starting character position, source text, and token type -- everything you'd need to know what to highlight and how. The only hassle would be running the text through the lexer each time the text is changed. There are efficient ways to do that without re-lexing the entire document, but it's still something to keep in mind.

Development of simple C# application for understanding Resharper Open APIs

I wanted to start developing plugins for Resharper using its Open API.
As there is very less documentation available, for starters I wanted to develop a simple C# application to extract the class details (eg. class name, number of methods in a class etc), so that I can understand the working of Resharper library and the in-built functions it provides.
It would be ideal if I can extract these details and print it on the console, for ease of testing.
Thanks.
in terms of documentation, there is some info available on the Developer Wiki, with more to come. You might also want to check out the ReSharper Extensibility Guide, which is a bit oudated but still relevant.
As for your question of how to examine the file structure - guess what - you don't even need the API! Here's what you need to do:
Start Visual Studio using the /ReSharper.Internal key. This will enhance the ReSharper top-level menu with an Internal section.
Pick ReSharper-->Internal-->PSI Viewer. This will pop up a form similar to the following:
Now, click the Create button, which will let you create a file, statement, expression or member declaration.
The bottom pane will then illustrate the parsed structure and let you navigate it.
Good luck!
If you want to parse the code yourself, that's a bit more complicated. The following is a rough outline of how you could go about parsing a file.
var buffer = new StringBuffer(sourceCode);
var lexer = LexerFactory.CreateLexer(buffer);
var defines = EmptyArray<PreProcessingDirective>.Instance; // assume there are none
var parser = new CSharpParser(lexer, defines);
var file = parser.ParseFile() as CSharpFile;
Now you can use file to navigate the code structure.
BTW, I strongly recommend using F#. It's made for this sort of thing.

VS2005: how to find text in the current function

In the VS2005 code-editor using C#, how do you search for text in the current function only? It allows searching over the Current Document but I cannot see how to limit the search to only the current function.
Select the function's code and then a new option will be available in the dropdown list. This option is called Current Block. This will allow you to search only within that specific function.
From the source on MSDN: Quick Find, Find and Replace Window
Most of the time, your functions should be short enough that this really isn't an issue, and I mean that.
Occasionally, you might have an almost-legitimate need for some kind of indexing function that has a lot of distinct tokens. Even in this case, you should refactor to use a database or reflection or something, as this is the kind of thing that'll get your code featured on DailyWtf.com.
But if it's really unavoidable to have a function this long, if it's legacy code, or for some other weird reason, there are some things you can do:
The Find All References feature in visual studio lists all results at once, in order. You can size the window to show just the results in your function.
You can use partial classes to isolate your function to a specific file
Highlight the function with your mouse, and the Find/Replace dialog can search within selected text only.

Searching the name of web pages according to the word entered in a textbox

I have a textbox and a button in one page.I want to enter a word in the textbox and click the button. After clicking the button I want to display the name of the web pages containing the word entered in the textbox. So please tell me how to do it? I am using C#.
So you want to create a search engine internal to your website. There are a couple of different options
You can use something like google custom search which requires no coding and uses the google technology which I think we all agree does a pretty good job compared to other search engines. More information at http://www.google.com/cse/
Or you can implement it in .net which I will try to give some pointers about below.
A search engine in general exists out of (some of) the following parts:
a index which is searched against
a query system which allows searches to be specified and results shown
a way to get documents into the index like a crawler or some event thats handled when the documents are created/published/updated.
These are non trivial things to create especially if you want a rich feature set like stemming (returning documents containing plural forms of search terms), highlighting results, indexing different document formats like pdf, rtf, html etc... so you want to use something already made for this purpose. This would only leave the task of connecting and orchestrating the different parts, writing the flow control logic.
You could use Lucene.net a opensource project with a lot of features. http://usoniandream.blogspot.com/2007/10/tutorial-implementing-lucenenet-search.html explains how to get started with it.
The other option is Microsoft indexing service which comes with windows but I would advice against it since it's difficult to tweak to work like you want and the results are sub-optimal in my opinion.
You are going to need some sort of backing store, and full text indexing. To the best of my knowledge, C# alone is not enough.

Categories