Author Topic: [ Patch #3180 ] Single quote, double quote and curly brace completion patch  (Read 17296 times)

Offline dpurgin

  • Multiple posting newcomer
  • *
  • Posts: 10
Hi everyone,

CB is a masterpiece, but I've found a few things, that were very annoying for me after using Carbide.c++ IDE based on Eclipse. I've submitted a little patch for 10.05-release, and here what it does:

Single and double quote completion revised (C/C++ style quotes)

When opening a string or character, CB automatically appends closing quote for the entered one. This is awesome, but when you have to re-factor such a code, CB adds closing quotes no matter if it is needed or not. For example, we have code:

Code
   std::cout << '\n;
   std::cout << \n';
   std::cout << "Hello, world!\n;
   std::cout << Hello, world!\n";
   std::cout << "Hello, \
      world;
   // etc, could be anything

You put closing or opening quote respectively, but CB automatically adds another one. This patch checks whether we're currently starting a string, ended by closing quote, or closing a string, started with quote (with respect of carry in multi-character strings). If so, no additional quote is added. If we're inside the string, then additional quote is added to divide the current string. Checking is performed based on line content, because I found lexer (namely, GetStyleAtPos() call) not precise enough.

Curly braces completion revised

This patch makes curly brace auto-completion a little bit more context-sensitive. Whenever you put an opening curly brace, the code checks whether the next line is not empty. If it is empty, then behavior is not altered - we put a new line and a closing brace after it. But when the next line is not empty and contains some code, then behavior depends on the next line's indentation. If the next line is indented more, than the current one, then we find the end of indented block (until line indentation is less or equal to current line indentation) and put closing curly brace right after the indented block (if there is one, do nothing). If next line indentation is less, that the current one, then behavior is the same as putting opening curly brace before an empty line. If next line is indented just as the current line, than we do not add a closing curly brace, as it is not clear, where the coder wants to put the closing brace.

Whitespace stripping when saving: turned off

This is the most annoying thing for me. I save my code often and quite often put a carret on an indented new line. When I save my code, I don't really want to lose the indentation.

I hope, this would be useful for somebody.

Cheers,
   Dmitriy

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6034
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Very interesting patch, but it would be better that the patch is against the trunk (not 10.05)
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline dpurgin

  • Multiple posting newcomer
  • *
  • Posts: 10
Thanks for reply and interest!

The reason I've made this patch for 10.05 is very simple: I have an RPM Linux distribution and it is more convenient to patch codeblocks from source-rpm and then rebuild binary rpms. Thus I get a "native" distro's codeblocks with my patch.

I've checked trunk out and, as far as I can see, the quote refinement is already partly implemented. I'll merge rest of my code a bit later.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6034
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Ok, I will test it when you update the patch. :D
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13406
    • Travis build status
dpurgin: thanks for the contribution, I don't use the quote/brace completion, but will test the white space patch.

I have two questions:
1. Can you extract the white-spaces changes, so they can be tested separately?
2. Can you describe the steps you use to build your patched binary packages. I'm on centos at work and need to build debugger branch rpm-s, but last time I've failed to do so :(
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline dpurgin

  • Multiple posting newcomer
  • *
  • Posts: 10
Hi oBFusCATed,

1. Yep, sure. The whitespace patch is:

Code
diff -uNrp codeblocks-10.05-release/src/sdk/cbeditor.cpp codeblocks-10.05-release.new/src/sdk/cbeditor.cpp
--- codeblocks-10.05-release/src/sdk/cbeditor.cpp 2010-05-22 16:10:05.000000000 +0600
+++ codeblocks-10.05-release.new/src/sdk/cbeditor.cpp 2011-06-30 15:48:42.496262953 +0600
@@ -1678,7 +1678,8 @@ bool cbEditor::Save()
     {
         if(m_pData->m_strip_trailing_spaces)
         {
-            m_pData->StripTrailingSpaces();
+            // very annoying call
+            //m_pData->StripTrailingSpaces();
         }
         if(m_pData->m_ensure_consistent_line_ends)
         {

2. I've learned these steps from Fedora's custom kernel tutorial.

At first, you should have some rpm and yum utils:

Code
$ su -c 'yum install rpmdevtools yum-utils'

Then you set up rpm development tree. After that you'll see ~/rpmbuild/SPECS, ~/rpmbuild/RPMS, ~/rpmbuild/BUILD and so on. I have done this in my home directory:

Code
$ cd ~
$ rpmdev-setuptree

Then you have to download the source package (say, for codeblocks the source RPM would be codeblocks-10.05-4.fc14.src.rpm), install its dependencies and the package itself:

Code
$ yumdownloader --source codeblocks
$ su -c 'yum-builddep codeblocks-10.05-4.fc14.src.rpm'
$ rpm -ihv codeblocks-10.05-4.fc14.src.rpm

Note, that the last rpm command should be executed under regular user (non-root). The original source archive and all the patches of your distribution will be installed to ~/rpmbuild/SOURCES without any hierarchy (which is not really convenient, when developing several rpms).

Next step is to prepare the source tree, using the .spec file for your package:

Code
$ cd ~/rpmbuild/SPECS
$ rpmbuild -bb --target=$(uname -m) codeblocks.spec

After that you will have the sources deployed to ~/rpmbuild/BUILD/codeblocks-10.05-release
Then you should copy the original source tree:

Code
$ cd ~/rpmbuild/BUILD
$ cp -r codeblocks-10.05-release codeblocks-10.05-release.new

Then you modify the sources in codeblocks-10.05-release.new as you wish.
After you're done with editing, you make a patch using diff and put it to the ~/rpmbuild/SOURCES directory:

Code
$ cd ~/rpmbuild/BUILD
$ diff -uNrp codeblocks-10.05-release codeblocks-10.05-release.new > ../SOURCES/codeblocks-my.patch

Then you should edit .spec-file in order to include your patch to RPM build. Spec-files have similar structure, but its content differs, so you should check your package's spec. What you have to do is: find list of patches, add your own patch to the list, find the patch commands and add your patching command there. You should also change release number, so that your newly build packages are not messed with the original. For ~/rpmbuild/SPECS/codeblocks.spec:

Code
Name:		codeblocks
Version: 10.05
Release: 4%{?dist}             # here you put your release. For example, 4a%{?dist}, then packages will have version 10.05-4a.fc14
Summary: An open source, cross platform, free C++ IDE
Group: Development/Tools
here go distribution patches:
Code
Patch2:		%{name}-dso.patch
# update for tinyxml 2.6
Patch3: %{name}-tinyxml-26.patch
# D support - svn revisions 6553-6556
Patch4:         %{name}-10.05-D.patch
# we add our own patch to the list:
Patch5:        %{name}-my.patch # %{name} will be substituted with "codeblocks"
here goes patch application:
Code
%prep
%setup -q -n %{name}-%{version}-release
%patch1 -p1 -b .tinyxml
%patch2 -p1 -b .dso
%patch3 -p1 -b .tinyxml-26
%patch4 -p1 -b .D
# we add our own patch
%patch5 -p1 -b .my

After these steps all you have to do is to build the rpms using the modified .spec:

Code
$ rpmbuild -bb --target=$(uname -m) codeblocks.spec

You will have your RPMs under ~/rpmbuild/RPMS/<architecture>/
I also add these packages to exclude statement in /etc/yum.conf, so that they are not crashed by update.

Hope this helps.

Cheers!

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13406
    • Travis build status
Hm,
Does stripping of the trailing spaces work with your patch?
This is a feature of C::B and I like it a lot, when dealing with my source.
If you've disabled it completely, this is not the correct way to do it - there is an option which does the same.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline dpurgin

  • Multiple posting newcomer
  • *
  • Posts: 10
Yep, just the call is commented out, so no line is stripped. I've understood your concern - a bit later I'll modify this so that only the current line won't be stripped, that is what I actually needed, not every line  :)

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9693
1. Yep, sure. The whitespace patch is:
Are you kidding? Why don't you simple disable this options in the editor settings: Settings -> Editor, section "End-of-line options" -> "Strip trailing blanks"??? :shock:
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline dpurgin

  • Multiple posting newcomer
  • *
  • Posts: 10
MortenMacFly, thanks for reply - I haven't noticed that option! :D
My code is a dirty hack, I know, but with respect of proposal of oBFusCATed, the aim now is NOT to strip the current line when saving, while leave the others to be stripped. I've tried the trunk code - when "Strip trailing blanks" is on, then the current line is still stripped.

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9693
when "Strip trailing blanks" is on, then the current line is still stripped.
Sure it will be stripped if this options is enabled.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline dpurgin

  • Multiple posting newcomer
  • *
  • Posts: 10
Yep, but is this really convenient? Imagine you're inside of 4- or 5-tabs indented block, you save your code and the caret goes to the beginning of the line? I'd like to have every line except the current stripped. I'm not sure if there are other people like me, anyway  :D

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13406
    • Travis build status
There are other people in the same situation, but there is no clean solution for the problem, unfortunately.
The best will be if there is a chance to save the file without the white-spaces and tell scintilla to preserve the cursor position.
There are some virtual white-spaces or something like, that but I'm not sure if they are suitable/usable for this problem.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline active0000

  • Single posting newcomer
  • *
  • Posts: 6
When I noticed a log entry to see the code:: block log panel, I see several "add (item) parser" I think it should be written on the right side of the workspace for all projects of this? Then only the first 40 projects have been my workspace like this (and events) mentioned in the next 64 items are not. This project has been mentioned activities: create project "rich" new parser! Project "rich" and then parse all of these "add items ...". We have a wonderful place to limit the number?

Offline dpurgin

  • Multiple posting newcomer
  • *
  • Posts: 10
I've merged my code into rev. 7268 and updated patch at the patch tracker. I left whitespace stripping intact, thus merging only quotes and curly brace completion.

Cheers

Offline Folco

  • Regular
  • ***
  • Posts: 343
    • Folco's blog (68k lover)
Curly brace patch is excellent !
Kernel Extremist - PedroM power ©

Offline dpurgin

  • Multiple posting newcomer
  • *
  • Posts: 10
I'm glad you've found it useful :)

cheers