mickey avatar mickey committed cdcd754

* Added `python-mp-extract-dwim' to extract the sexp, string, or expression at point.

Comments (0)

Files changed (1)

   ;; string symbol?
     (eq 'string (syntax-ppss-context (syntax-ppss pt))))
 
-(defun python-mp-extract-to-constant (name &optional arg)
-  "Extracts the expression at point as a constant, NAME. If point is in
-a class then the constant is declared as a class field. If point
-is not in a class it is extracted as a global constant. If
-numerical ARG is set then it is made global regardless."
-  (interactive "*sConstant Name:")
-  (if (python-mp-stringp (point))
-      (progn
-        (save-excursion
-          (python-beginning-of-string)
-          (let ((constant
-                 (delete-and-extract-region
-                  (point) (save-excursion (forward-sexp) (point)))))
-            )))))
+(defun python-mp-commentp (pt)
+  "Returns t if PT is in a Python comment."
+  (eq 'comment (syntax-ppss-context (syntax-ppss pt))))
+
+(defun python-mp-extract-dwim ()
+  "Extracts the expression, string or sexp at point and returns
+it."
+  ;; if point is in a string we want to extract all of it.
+  (cond
+   ((python-mp-stringp (point))
+    (save-excursion
+      (python-beginning-of-string)
+      (delete-and-extract-region (point) (save-excursion (forward-sexp) (point)))))
+   ((python-mp-commentp (point))
+    (error "Cannot use Extract Expression in a comment"))
+   (t
+    (let ((bounds (bounds-of-thing-at-point 'sexp)))
+      (if bounds
+          (delete-and-extract-region (car bounds) (cdr bounds))
+        (error "Cannot find a valid expression"))))))
+  
+  
 
 
 (defun python-mp-send-func (func arg)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.