branch: externals/csharp-mode
commit 5b89db275605b917bda9cabc23e8309c7cf5d512
Author: Jostein Kjønigsen <jost...@kjonigsen.net>
Commit: Jostein Kjønigsen <jost...@kjonigsen.net>

    Imenu: Correctly handle default-values in param-lists.
    
    Fixes https://github.com/josteink/csharp-mode/issues/35
---
 csharp-mode-tests.el | 42 ++++++++++++++++++++++++++++++++----------
 csharp-mode.el       | 34 +++++++++++++++++++++++++++++++---
 2 files changed, 63 insertions(+), 13 deletions(-)

diff --git a/csharp-mode-tests.el b/csharp-mode-tests.el
index dd99934..d78a5ae 100644
--- a/csharp-mode-tests.el
+++ b/csharp-mode-tests.el
@@ -54,13 +54,13 @@
 
 (ert-deftest build-warnings-and-errors-are-parsed ()
   (dolist (test-case
-          `(("./test-files/msbuild-warning.txt" 
,csharp-compilation-re-msbuild-warning 8
+           `(("./test-files/msbuild-warning.txt" 
,csharp-compilation-re-msbuild-warning 8
               ,(list-repeat-once
                 '("Class1.cs"
                   "Folder\\Class1.cs"
                   "Program.cs"
                   "Program.cs")))
-            ("./test-files/msbuild-error.txt" 
,csharp-compilation-re-msbuild-error 2
+             ("./test-files/msbuild-error.txt" 
,csharp-compilation-re-msbuild-error 2
               ,(list-repeat-once
                 '("Folder\\Class1.cs")))
              ("./test-files/msbuild-concurrent-warning.txt" 
,csharp-compilation-re-msbuild-warning 2
@@ -69,28 +69,50 @@
              ("./test-files/msbuild-concurrent-error.txt" 
,csharp-compilation-re-msbuild-error 2
               ,(list-repeat-once
                 '("Program.cs")))
-            ("./test-files/xbuild-warning.txt" 
,csharp-compilation-re-xbuild-warning 10
+             ("./test-files/xbuild-warning.txt" 
,csharp-compilation-re-xbuild-warning 10
               ,(list-repeat-once
                 
'("/Users/jesseblack/Dropbox/barfapp/ConsoleApplication1/ClassLibrary1/Class1.cs"
                   
"/Users/jesseblack/Dropbox/barfapp/ConsoleApplication1/ClassLibrary1/Folder/Class1.cs"
                   
"/Users/jesseblack/Dropbox/barfapp/ConsoleApplication1/ConsoleApplication1/Program.cs"
                   
"/Users/jesseblack/Dropbox/barfapp/ConsoleApplication1/ConsoleApplication1/Program.cs"
                   
"/Users/jesseblack/Dropbox/barfapp/ConsoleApplication1/ConsoleApplication1/Program.cs")))
-            ("./test-files/xbuild-error.txt" 
,csharp-compilation-re-xbuild-error 2
+             ("./test-files/xbuild-error.txt" 
,csharp-compilation-re-xbuild-error 2
               ,(list-repeat-once
                 
'("/Users/jesseblack/Dropbox/barfapp/ConsoleApplication1/ClassLibrary1/Folder/Class1.cs")))
-            ))
+             ))
 
     (let* ((file-name (car test-case))
-          (regexp    (cadr test-case))
-          (times     (caddr test-case))
+           (regexp    (cadr test-case))
+           (times     (caddr test-case))
            (matched-file-names (cadddr test-case))
-          (find-file-hook '()) ;; avoid vc-mode file-hooks when opening!
-          (buffer (find-file-read-only file-name)))
+           (find-file-hook '()) ;; avoid vc-mode file-hooks when opening!
+           (buffer (find-file-read-only file-name)))
       (dotimes (number times)
-       (re-search-forward regexp)
+        (re-search-forward regexp)
         (should
          (equal (nth number matched-file-names) (match-string 1))))
       (kill-buffer buffer))))
 
+(ert-deftest imenu-parsing-supports-default-values ()
+  (dolist (test-case
+           '(;; should support bools
+             ("(bool a, bool b = true)"                  "(bool, bool)")
+             ("(bool a=true, bool b)"                    "(bool, bool)")
+             ;; should support strings
+             ("(string a, string b = \"quoted string\")" "(string, string)")
+             ("(string a = \"quoted string\", string b)" "(string, string)")
+            ;; should support chars
+             ("(char a, char b = 'b')"                   "(char, char)")
+             ("(char a = 'a', char b)"                   "(char, char)")
+            ;; should support self-object-access
+            ("(object o = Const)"                       "(object)")
+            ;; should support other-object-access
+            ("(object o = ConstObject.Const)"           "(object)")
+            ))
+    (let* ((test-value     (car test-case))
+          (expected-value (cadr test-case))
+          (result         (csharp--imenu-remove-param-names-from-paramlist 
test-value)))
+      (should (equal expected-value result)))))
+
 ;;(ert-run-tests-interactively t)
+
diff --git a/csharp-mode.el b/csharp-mode.el
index fc21c89..7630d96 100644
--- a/csharp-mode.el
+++ b/csharp-mode.el
@@ -275,6 +275,9 @@
 ;;    0.8.9 2015 March 15
 ;;          - (Re)add compilation-mode support for msbuild and xbuild.
 ;;
+;;    0.8.x 2015 May 14th
+;;          - Imenu: Correctly handle support for default-values in paramlist.
+;;
 
 (require 'cc-mode)
 
@@ -2232,6 +2235,7 @@ Upon entry, it's assumed that the parens included in S.
              (state 0)  ;; 0 => ws, 1=>slurping param...
              c
              cs
+            quoting
              nesting
              need-type
              ix2
@@ -2248,9 +2252,14 @@ Upon entry, it's assumed that the parens included in S.
            ;; backing over whitespace "after" the param
            ((= state 0)
             (cond
-             ;; more ws
-             ((string-match "[ \t\f\v\n\r]" cs)
+             ;; more ws. = is equal to whitespace in the sense that its 
follows a param-name.
+             ((string-match "[ \t\f\v\n\r=]" cs)
               t)
+            ((string-match "[\"']" cs)
+             ;; a quote means we're probably dealing with a stringy 
default-value
+             ;; back out until we're back into unquoted context
+             (setq quoting cs
+                   state 5))
              ;; a legal char for an identifier
              ((string-match "[A-Za-z_0-9]" cs)
               (setq state 1))
@@ -2264,8 +2273,13 @@ Upon entry, it's assumed that the parens included in S.
              ;; ws signifies the end of the param
              ((string-match "[ \t\f\v\n\r]" cs)
               (setq state 2))
+            ((string-match "[=]" cs)
+             ;; = means what we slurped was a default-value for a param
+             ;; go back to slurping param-name
+             (setq state 0))
              ;; a legal char for an identifier
-             ((string-match "[A-Za-z_0-9]" cs)
+            ;; (or . for object-access in default value)
+             ((string-match "[A-Za-z_0-9\.]" cs)
               t)
              (t
               (error "unexpected char (B)"))))
@@ -2276,6 +2290,10 @@ Upon entry, it's assumed that the parens included in S.
             (cond
              ((string-match "[ \t\f\v\n\r]" cs)
               t)
+            ((string-match "[=]" cs)
+             ;; = means what we slurped was a default-value for a param
+             ;; go back to slurping param-name
+             (setq state 0))
              ;; non-ws indicates the type spec is beginning
              (t
               (incf i)
@@ -2343,6 +2361,16 @@ Upon entry, it's assumed that the parens included in S.
 
              (t
               (error "unexpected char (C)"))))
+
+          ;; in a quoted context of a default-value.
+          ;; we're basically waiting for a matching quote, to go back to 
slurping param-name
+          ((= state 5)
+           (cond
+            ((equal quoting cs)
+             ;; we're back to unquoted! slurp param-name!
+             (setq state 0))
+            (t
+             t)))
            )
 
           (decf i))

Reply via email to