
    eI                        U d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZm	Z	 d dl
Z
d dlmZ d dlmZ d dlmZ ej"                  dk\  rd dlZnd dlZ e j(                  e      ZdZe	e   ed	<   	 ej2                  d
k(  r ej4                         dz  Zn>dej6                  v r eej6                  d         dz  Zn ej4                         dz  dz  Ze#ej9                         rej;                  de       	  ed      d        Z! ed      d        Z"ed        Z#ddZ$d Z% ed      de&dedefd       Z'de&dedefdZ(y# e$ rZejA                  de       Y dZ[idZ[ww xY w)    N)	lru_cache)Path)DictOptional)hookimpl)get_eol_chars)Config)      GLOBAL_CONFIGntz.blackXDG_CONFIG_HOMEblackz.configz$Found black global config file at %sz3Error determining black global config file path: %sT)tryfirstc                     t        | |      S Nformat_document)configdocuments     4/usr/lib/python3/dist-packages/pylsp_black/plugin.pypylsp_format_documentr   $   s    68,,    c                 \    d|d   d<   |d   dxx   dz  cc<   d|d   d<   t        | ||      S )Nr   start	characterendline   r   )r   r   ranges      r   pylsp_format_ranger!   )   sA    "#E'N;	%LA !E%L68U33r   c                  $    ddddddddiddidiS )z4Configuration options that can be set on the client.pluginsTX   F)enabledline_lengthpreviewcache_configr%   )r   yapfautopep8 r+   r   r   pylsp_settingsr,   1   s9     	!  %	 &"E*	
 r   c                    |j                   }t        |j                  |       }|r|d   d   dz   |d   d   fgnd}	 t        |||      }|rX|j                  d      }|d   d   }|d   d   t        |      t        |j                        z
  z   }	dj                  |||	       }nd	d	d
t        |j                        d	d
d}||dgS # t        j
                  $ r g cY S w xY w)Nr   r   r   r   r+   )textr   linesT r   )r   r   )r   r   )r    newText)
sourceload_configpathformat_textr   NothingChanged
splitlineslenr/   join)
client_configr   r    r.   r   r/   formatted_textformatted_linesr   r   s
             r   r   r   B   s   ??D6FDIeGnV$q(%,v*>?@rE$$vUK
 (33D9gv&El6"c/&:S=P&PQs!;<  a0/a@

 788#  	s   C C C c           	         t        j                  |d   |d   |d   |d    |d    |d         }	 d}t        |       }||d	k7  rd
}| j                  |d	      } t        j                  | |d   ||      }|r|j                  d	|      }|S # t
        t        t        f$ r,}t        j                  d|       t         j                  |d }~ww xY w)Ntarget_versionr&   pyiskip_string_normalizationskip_magic_trailing_commar'   )target_versionsr&   is_pyistring_normalizationmagic_trailing_commar'   F
Tfast)rG   moder/   zError formatting with black: %s)r   FileModer   replaceformat_file_contents
ValueErrorIndentationErrorAssertionErrorloggererrorr6   )r.   r   r/   rH   replace_eols	eol_charsr;   es           r   r5   r5   ^   s    >>/0=)e}!'(C!DD!'(C!DDy!D* !$'	 Y$%6L<<	40D 33vf~D

 +33D)DN 	 	 * 	6:"")*s   AB C'CCd   filenamer:   returnc           
         |j                  d      }|j                  dd      d| j                  d      |j                  dd      |j                  dd      t               |j                  dd      d	}t	        j
                  | f      }t        |t              r	|d
   dz  }n|dz  }|j                         sOt        1t        j                         rt        }t        j                  d|       nt        j                  d|       |S 	 t        |d      5 }t        j                  |      }d d d        j                  di       j                  di       }|j'                         D 	
ci c](  \  }	}
|	j)                  dd      j)                  dd      |
* }}	}
|j'                         D 	ci c]  \  }	}|	|j                  |	|       }}	}|j                  d      rt        d |d   D              }n
t               }||d<   t        j                  d||       |S # 1 sw Y   xY w# t        j                   t"        f$ r t        j%                  d|       |cY S w xY wc c}
}	w c c}}	w )Nr   r&   r$   Fz.pyir@   rA   r'   )r&   rG   r?   r@   rA   r>   r'   r   zpyproject.tomlzUsing global black config at %szUsing defaults: %rrbz1Error decoding pyproject.toml, using defaults: %rtoolz--r0   -_r>   c              3   ^   K   | ]%  }t         j                  |j                             ' y wr   )r   TargetVersionupper).0xs     r   	<genexpr>z_load_config.<locals>.<genexpr>   s&      
/0E	*
s   +-zUsing config from %s: %r)plugin_settingsgetendswithsetr   find_project_root
isinstancetupleis_filer   existsrO   infoopentomllibloadTOMLDecodeErrorOSErrorwarningitemsrJ   )rU   r:   settingsdefaultsrootpyproject_filenamefpyproject_tomlfile_configkeyvaluedefault_valuer   r>   s                 r   _load_configr}      sX   ,,W5H  ||M26  (%-\\2Mu%U%-\\2Mu%U%<<	51H ""H;/D $!!W'77!$44%%'$)=)=)?!.KK9;MNKK,h7O$d+ 	-q$\\!_N	- !$$VR044WbAK &++-C 	D"%%c3/6K  #+.."2C 	[__S-00F 
 '( 
4?@P4Q
 
 -F
KK*,>GM?	- 	-##W- ?	
 
s6   H$ H)H$ &-I)I!H!H$ $1IIc                     |j                  d      }|j                  dd      st        j                  | |      S t        | |      S )Nr   r(   F)rb   rc   r}   __wrapped__)rU   r:   rs   s      r   r3   r3      sB    ,,W5H <<.''-@@-00r   r   ))loggingossys	functoolsr   pathlibr   typingr   r   r   pylspr   pylsp._utilsr   pylsp.config.configr	   version_inform   tomli	getLogger__name__rO   r   __annotations__namehomeenvironrj   rk   	ExceptionrS   rP   r   r!   r,   r   r5   strr}   r3   r+   r   r   <module>r      s    	 
   !   & &w			8	$ !%x~ $K	ww$!		h.	bjj	(RZZ(9:;gE!		i/'9  ]%9%9%;:MJ 
4- - 
44 4 
 
 98(*V 3=3 =v =$ = =@1# 1f 1 1[  K
LLFJJKs   -A"E E"EE"