                   'filepath filepath 'line_num line-num
                   'column_num column-num))))
 
+;; The next two function are taken from company-sourcekit.el
+;; https://github.com/nathankot/company-sourcekit
+(defun company-ycmd--normalize-source-text-sourcekit (sourcetext)
+  "Make a more readable completion candidate out of one with
+placeholders inserted by SourceKit."
+  (replace-regexp-in-string
+   "<#T##\\(.*?\\)#>"
+   (lambda (str)
+     ;; <#T##Int#> - No label, argument only
+     (save-match-data
+       (string-match "<#T##\\(.*?\\)#>" str)
+       (format "%s" (car (split-string (match-string 1 str) "#")))))
+   sourcetext))
+
+(defun company-ycmd--build-yasnippet-sourcekit (sourcetext)
+  "Build a yasnippet-compatible snippet from the given source
+text template generated by SourceKit."
+  (replace-regexp-in-string
+   "<#T##\\(.*?\\)#>"
+   (lambda (str)
+     ;; <#T##Int#> - No label, argument only
+     (save-match-data
+       (string-match "<#T##\\(.*?\\)#>" str)
+       (format "${%s}" (car (split-string (match-string 1 str) "#")))))
+   sourcetext))
+
+(defun company-ycmd--construct-candidate-swift (candidate)
+  "Construct completion string from CANDIDATE for swift file-types."
+  (company-ycmd--with-destructured-candidate candidate
+    (propertize (company-ycmd--normalize-source-text-sourcekit .insertion_text)
+                'sourcetext .insertion_text)))
+
 (defun company-ycmd--construct-candidate-javascript (candidate)
   "Construct completion string from CANDIDATE for js file-types."
   (company-ycmd--with-destructured-candidate candidate

    ("go" 'company-ycmd--construct-candidate-go)
    ("python" 'company-ycmd--construct-candidate-python)
    ("rust" 'company-ycmd--construct-candidate-rust)
    ("javascript" 'company-ycmd--construct-candidate-javascript)
    ("typescript" 'company-ycmd--construct-candidate-typescript)
    (_ 'company-ycmd--construct-candidate-generic)))


(defun company-ycmd--post-completion (candidate)
  "Insert function arguments after completion for CANDIDATE."
  (if (eq major-mode 'swift-mode)
      (company-ycmd--post-completion-swift candidate)
    (--when-let (and (company-ycmd--extended-features-p)
                     company-ycmd-insert-arguments
                     (get-text-property 0 'params candidate))
      (when (memq major-mode '(python-mode rust-mode))
        (setq it (company-ycmd--remove-self-from-function-args it))
        (when (eq major-mode 'rust-mode)
          (setq it (company-ycmd--remove-template-args-from-function-args it))))
      (insert it)
      (if (string-match "\\`:[^:]" it)
          (company-template-objc-templatify it)
        (company-template-c-like-templatify
         (concat candidate it))))))

(declare-function yas-expand-snippet "yasnippet")
(defun company-ycmd--post-completion-swift (candidate)
  "Insert function arguments after completion for CANDIDATE in
swift mode."
  (let ((template (company-ycmd--build-yasnippet-sourcekit
                   (get-text-property 0 'sourcetext candidate))))
    (yas-expand-snippet template (- (point) (length candidate)) (point))))

(defun company-ycmd--doc-buffer (candidate)
  "Return buffer with docstring for CANDIDATE if it is available."
