<commit_before>;;;
;;;; Namespaces and types expansion
;;;
;;
;; @created   "Mon Mar 07 09:18:01 EST 2005"
;; @author    "<NAME>"
;; @copyright "NuEcho Inc."
;;
;; 


(require 'srfi-1)


(define-simple-class <SimpleSearchCollector> (<Object> <org.eclipse.jdt.core.search.IJavaSearchResultCollector>)
  (elements)
  
  ((aboutToStart) :: <void>
   (set! elements '()))
  
  ((done) :: <void>
   (set! elements (reverse! elements)))
  
  ((accept (resource :: <org.eclipse.core.resources.IResource>)
           (start :: <int>)
           (end :: <int>)
           (element :: <org.eclipse.jdt.core.IJavaElement>)
           (accurracy :: <int>)) :: <void>
   (set! elements (cons element elements)))
  
  ((getProgressMonitor) :: <org.eclipse.core.runtime.IProgressMonitor>
   #!null))


(define (find-symbol-types symbol)
  (let ((engine  (SearchEngine:new))
        (pattern (SearchEngine:createSearchPattern symbol 0 0 #t))
        (scope   (SearchEngine:createWorkspaceScope))
        (collector (make <SimpleSearchCollector>)))
    (SearchEngine:search engine (RsrcPlugin:getWorkspace) pattern scope collector)
    (field collector 'elements)))


(define (find-package-types symbol)
  (let ((engine (SearchEngine:new))
        (pattern (SearchEngine:createSearchPattern (string-append symbol ".*") 0 0 #t))
        (scope (SearchEngine:createWorkspaceScope))
        (collector (make <SimpleSearchCollector>)))
    (SearchEngine:search engine (RsrcPlugin:getWorkspace) pattern scope collector)
    (field collector 'elements)))



(define (namespace-expander symbol)
  (let ((types (find-symbol-types symbol)))
    (if (pair? types)
        (let ((choosen-type-name (choose-type types)))
          (and choosen-type-name
               (format #f "(define-namespace ~a <~a>)~%" symbol choosen-type-name)))
        #f)))


(define (typename-expander symbol)
  (let ((types (find-symbol-types symbol)))
    (if (pair? types)
        (let ((choosen-type-name (choose-type types)))
          (and choosen-type-name
               (format #f "<~a>" choosen-type-name)))
        #f)))


(define (package-expander symbol)
  (let ((types (find-package-types symbol)))
    (and (pair? types)
         (call-with-output-string
          (lambda (port)
            (for-each (lambda (type)
                        (format port "(define-namespace ~a <~a>)~%" 
                                (IType:getElementName type)
                                (IType:getFullyQualifiedName type)))
                      types))))))


(define (choose-type types)
  (cond ((null? types)       #f)
        ((null? (cdr types)) (IType:getFullyQualifiedName (car types)))
        (else
         (choose-from-list "Type is ambiguous"
                           "Choose the right type:"
                           (delete-duplicates!
                            (map (lambda (type) (IType:getFullyQualifiedName type))
                                 types))))))


(define (make-symbol-expander expander)
  (lambda ()
    (let-values (((start end) (%backward-sexp)))
      (when (and start end (eq? (sexp-type start) symbol:))
        (with-buffer-text 
         start end 
         (lambda (text)
           (let ((clause (expander text)))
             (when clause
               (set-point start)
               (run-compound-change
                (lambda ()
                  (delete-text (- end start))
                  (insert-text clause)
                  (set-point (point))
                  (forward-sexp)))))))))))


(define-constant expand-namespace (make-symbol-expander namespace-expander))
(define-constant expand-typename  (make-symbol-expander typename-expander))
(define-constant expand-package   (make-symbol-expander package-expander))
<commit_msg>Fixed single type shown in choose type dialog  * Remove duplicates types returned by search engine right before    exiting function instead of waiting until dialog is shown<commit_after>;;;
;;;; Namespaces and types expansion
;;;
;;
;; @created   "Mon Mar 07 09:18:01 EST 2005"
;; @author    "<NAME>"
;; @copyright "NuEcho Inc."
;;
;;


(require 'srfi-1)


(define-simple-class <SimpleSearchCollector> (<Object> <org.eclipse.jdt.core.search.IJavaSearchResultCollector>)
  (elements)

  ((aboutToStart) :: <void>
   (set! elements '()))

  ((done) :: <void>
   (set! elements (reverse! elements)))

  ((accept (resource :: <org.eclipse.core.resources.IResource>)
           (start :: <int>)
           (end :: <int>)
           (element :: <org.eclipse.jdt.core.IJavaElement>)
           (accurracy :: <int>)) :: <void>
   (set! elements (cons element elements)))

  ((getProgressMonitor) :: <org.eclipse.core.runtime.IProgressMonitor>
   #!null))


(define (find-symbol-types symbol)
  (let ((engine  (SearchEngine:new))
        (pattern (SearchEngine:createSearchPattern symbol 0 0 #t))
        (scope   (SearchEngine:createWorkspaceScope))
        (collector (make <SimpleSearchCollector>)))
    (SearchEngine:search engine (RsrcPlugin:getWorkspace) pattern scope collector)
    (delete-duplicates!
     (map (lambda (type) (IType:getFullyQualifiedName type))
          (field collector 'elements)))))


(define (find-package-types symbol)
  (let ((engine (SearchEngine:new))
        (pattern (SearchEngine:createSearchPattern (string-append symbol ".*") 0 0 #t))
        (scope (SearchEngine:createWorkspaceScope))
        (collector (make <SimpleSearchCollector>)))
    (SearchEngine:search engine (RsrcPlugin:getWorkspace) pattern scope collector)
    (field collector 'elements)))


(define (namespace-expander symbol)
  (let ((types (find-symbol-types symbol)))
    (if (pair? types)
        (let ((choosen-type-name (choose-type types)))
          (and choosen-type-name
               (format #f "(define-namespace ~a <~a>)~%" symbol choosen-type-name)))
        #f)))


(define (typename-expander symbol)
  (let ((types (find-symbol-types symbol)))
    (if (pair? types)
        (let ((choosen-type-name (choose-type types)))
          (and choosen-type-name
               (format #f "<~a>" choosen-type-name)))
        #f)))


(define (package-expander symbol)
  (let ((types (find-package-types symbol)))
    (and (pair? types)
         (call-with-output-string
          (lambda (port)
            (for-each (lambda (type)
                        (format port "(define-namespace ~a <~a>)~%"
                                (IType:getElementName type)
                                (IType:getFullyQualifiedName type)))
                      types))))))


(define (choose-type types)
  (cond ((null? types)       #f)
        ((null? (cdr types)) (car types))
        (else
         (choose-from-list "Type is ambiguous"
                           "Choose the right type:"
                           types))))


(define (make-symbol-expander expander)
  (lambda ()
    (let-values (((start end) (%backward-sexp)))
      (when (and start end (eq? (sexp-type start) symbol:))
        (with-buffer-text
         start end
         (lambda (text)
           (let ((clause (expander text)))
             (when clause
               (set-point start)
               (run-compound-change
                (lambda ()
                  (delete-text (- end start))
                  (insert-text clause)
                  (set-point (point))
                  (forward-sexp)))))))))))


(define-constant expand-namespace (make-symbol-expander namespace-expander))
(define-constant expand-typename  (make-symbol-expander typename-expander))
(define-constant expand-package   (make-symbol-expander package-expander))
{'fraction_non_alphanumeric': 0.14597381991273303, 'fraction_numerical': 0.004495570540790692, 'mean_word_length': 2.419529837251356, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3713468', 'n_tokens_mistral': 2270, 'n_tokens_neox': 2172, 'n_words': 540}