Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
recently i am working on a code reviewer project but the problem i am facing is that as soon as i click the event's content the event should get highlighted, i can do the highlighted code myself but how to identify where the event is starting and where it is ending.


Example
//private void button1_Click
private void button1_Click(object sender, EventArgs e)
{
     //My Cursor lies here
}

for example: above is my code keeping in mind the comment should not be removed. now how do i get the event start and end as my focus lies in the middle of the function.


Thanks in advance
Posted
Comments
OriginalGriff 23-Feb-15 5:07am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
So what, exactly, is a "code reviewer project" and why is this giving you difficulty?
We have no idea how you are implementing things, or exactly what you are doing, so we can't really give you much help as it stands. Stop, sit back, and try to view your problem from the POV of someone who has no idea of what your project entails, or how far you have got. Then try to explain to him what your problem is...
Use the "Improve question" widget to edit your question and provide better information.
agent_kruger 23-Feb-15 5:27am    
sir please see the example in the question, i suppose it will make things clear but still here is what i am after : there is a textbox where i have code written like in example and my cursor lies in between the event now what i want is to get the starting and the end of the event
OriginalGriff 23-Feb-15 5:37am    
And?
What have you tried?
What are you doing at the moment to get the syntax highlighting?
Seriously, we have no idea how you are doing things - and there are a million ways to do these things - we didn't even know you were using a text box (or is it a rich text box?) until your reply!

We don't know how much processing you are doing on the code, how much existing syntactical analysis you have done. If you even look at the code closely enough to have already identified a method yet - much less how you have done it!
agent_kruger 23-Feb-15 6:55am    
see my friend the code is not at all complex like you mentioned above and as far as selection is concerned it is done by using simple method of textbox : "textbox.Select(start,length)".
agent_kruger 23-Feb-15 6:57am    
to sum the question up i need an algorithm that gets the start and end of the event/function from textbox where my current focus lies.

1 solution

"to sum the question up i need an algorithm that gets the start and end of the event/function from textbox where my current focus lies."

That is not trivial: you need to process the syntax of the whole file.

You can't even just "run backwards" from the current location until you reach the word "public", "private", "protected", or "internal" because:
1) It could be in a string of comment, and you don't know until you get to the start of the string or comment.
2) It might not be there: C# allows you to "default" the access modifier.
3) What if there is a syntax error? How far back do you go?
4) What if you are in an enum, or a constructor, or an embedded class, or a property, or a field, or a lambda, or...

Unless you process the syntax of the whole file you don't "know" where you are. And there is no algorithm for that: it's basically the first couple of stages of writing a new C# compiler...
 
Share this answer
 
Comments
agent_kruger 23-Feb-15 18:55pm    
you are right i didn't think of the enum and constructors but my code reviewer is just simple and only works for public and private functions and events only and yes comments can have the declaration of functions. Can you help me?
OriginalGriff 24-Feb-15 3:23am    
Sorry, but no - you need to process the syntax of the whole file.
agent_kruger 24-Feb-15 5:50am    
but i have just started the project and nothing is left to be posted here and after achieving this i can proceed any further.
OriginalGriff 24-Feb-15 5:53am    
"after achieving this i can proceed any further"
And that's the problem: it's the biggest bit of the project!

What? Did you think "Oh, that'll be easy" and assume it was something we all do every day? :laugh:
No - this is complicated stuff, and you need to look at it very, very carefully.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900