-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtex_completer.py
executable file
·1060 lines (857 loc) · 37.3 KB
/
tex_completer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python2
#
# TexCompleter - Semantic completer for YouCompleteMe which handles Tex files.
# Copyright (C) 2015 Till Smejkal <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
###
# Standard library imports.
###
from __future__ import print_function
from os.path import dirname, join, isfile, isdir, splitext
from os import listdir
from functools import total_ordering
import logging
###
# YCMD imports.
###
from ycmd.completers.completer import Completer
from ycmd.responses import BuildCompletionData
from ycmd.utils import AddNearestThirdPartyFoldersToSysPath
###
# Third Party imports.
###
# First add the local 'third_party' folder to the package search path.
AddNearestThirdPartyFoldersToSysPath(__file__)
import bibtexparser
logger = logging.getLogger(__name__)
class TexObject:
def _smart_shorten(self, to_shorten, length, delta = 5):
"""
Shorten a given string to the given length but on a smart way.
The specified length is not necessary the one which the string will have
after the shortening process. This is because of the way the method is
working. It tries to shorten the string at word boundaries if possible.
Hence the string can be a bit longer or shorter than specified.
:param to_shorten: The string which should be shortened.
:type to_shorten: str
:param length: The length to which the string should be shortened.
:type length: int
:param delta: The allowed delta which the string is allowed to be longer
or shorter. (Defaults to 5)
:type delta: int
:rtype: str
:return: The smartly shortened string.
"""
# As ' ...' is added to the end of the shortened string a little bit
# more space is needed.
goal_length = length - 4
if len(to_shorten) >= goal_length + delta:
# Find the boundaries of the word which gets shortened.
next_space = to_shorten.find(" ", length)
prev_space = to_shorten.rfind(" ", 0, length)
if next_space != -1 and next_space < goal_length + delta:
# 1. Try to keep the word in the result.
return to_shorten[:next_space] + " ..."
elif prev_space != -1 and prev_space > goal_length - delta:
# 2. Remove the whole word from the result.
return to_shorten[:prev_space] + " ..."
else:
# The word is to large to remove it completely. So it must be
# split.
return to_shorten[:goal_length - 1] + ". ..."
else:
return to_shorten
def completion(self):
"""
The completion text which should be presented to the user of the
completer.
This method must be implemented by every TeX object which the completer
supports.
:rtype: str
:return: The completion text of this object.
"""
raise NotImplementedError()
def extra_info(self, shortened = True):
"""
The additional information for the completion which should be presented
to the user of the completer.
This method must be implemented by every TeX object which the completer
supports.
:param shortened: Whether or not the information text should be
shortened or not. (Defaults to True)
:type shortened: bool
:rtype: str
:return: The additional information of this object.
"""
raise NotImplementedError()
@total_ordering
class TexReferable(TexObject):
MaxNameLength = 50
AbbreviationMap = {
"unknown" : "u",
"chapter" : "C",
"section" : "S",
"subsection" : "s",
"subsubsection" : "U",
"paragraph" : "P",
"subparagraph" : "p",
"figure" : "F",
"table" : "T",
"lstlisting" : "L"
}
def __init__(self, label, name="Unknown", ref_type="unknown"):
"""
Constructor
:param label: The identifier which is used to reference it in the text.
:type label: str
:param name: The actual name of the referable object.
:type name: str
:param ref_type: The type of the referable object.
:type ref_type: str
"""
self._label = label
self._name = name
self._short_name = None
self._ref_type = ref_type
self._abbreviation = self.AbbreviationMap[ref_type] if \
self.AbbreviationMap.has_key(ref_type) else \
self.AbbreviationMap["unknown"]
def __eq__(self, other):
"""
Compare for equality with another referable object.
Equality is reached if all three parameters (label, name, and ref_type)
are equal.
:param other: The object which should be tested for equality.
:type other: TexReferable
:rtype: bool
:return: Whether or not the objects are equal.
"""
if not isinstance(other, TexReferable):
return ValueError("Equality can only be tested for objects with" +
"the same type")
return self._label == other._label and self._name == other._name and \
self._ref_type == other._ref_type
def __lt__(self, other):
"""
Determine if the current object is less than the other one.
The current object is less than the other one if the label is less, or
if the name is less, or if the ref_type is less than the one of the
other object.
:param other: The object which should be tested against.
:type other: TexReferable
:rtype: bool
:return: Whether or not the current object is less then the other one.
"""
if not isinstance(other, TexReferable):
raise ValueError("Less than can only be tested for objects with" +
"the same type.")
if self._label != other._label:
return self._label < other._label
elif self._name != other._name:
return self._name < other._name
elif self._ref_type != other._ref_type:
return self._ref_type < other._ref_type
else:
return False
def shorten(self, ignore_name = "Unknown"):
"""
Shorten the name of the referable object so that it is not too long.
This method just alters the internal state of the object.
:param ignore_name: If the name is equal to the given one, shorten is
skipped for the name. (Defaults to 'Unknown')
:type ignore_name: str
:rtype: TexReferable
:return: The current object
"""
# Smartly shorten the name of the referable object if this name should
# not be ignored.
if self._name != ignore_name:
self._short_name = self._smart_shorten(self._name, self.MaxNameLength)
return self
def completion(self):
"""
:see TexObject.completion:
"""
return self._label
def extra_info(self, shorten = True):
"""
:see TexObject.completion:
"""
if shorten:
if self._short_name is None:
name = self.shorten()._short_name
else:
name = self._short_name
else:
name = self._name
return self._abbreviation + " " + name
@total_ordering
class TexCitable(TexObject):
MaxTitleLength = 45
AbbreviationMap = {
"unknown" : "u",
"article" : "A",
"book" : "B",
"booklet" : "b",
"conference" : "C",
"inbook" : "I",
"incollection" : "i",
"inproceedings" : "p",
"journal" : "J",
"manual" : "M",
"masterthesis" : "t",
"misc" : "m",
"phdthesis" : "T",
"proceedings" : "P",
"techreport" : "R",
"unpublished" : "U"
}
def __init__(self, label, title="Unknown", author="Unknown",
cite_type="unknown"):
"""
Constructor
:param label: The identifier which is used to cite it in the text.
:type label: str
:param title: The title of the cited object. (Defaults to 'Unknown')
:type title: str
:param author: The author of the cited object. (Defaults to 'Unknown')
:type author: str
:param cite_type: The Bibtex type of the cited object (Defaults to
'Unknown')
:type cite_type: str
"""
self._label = label
self._title = title
self._short_title = None
self._author = author
self._short_author = None
self._cite_type = cite_type
self._abbreviation = self.AbbreviationMap[cite_type] if \
self.AbbreviationMap.has_key(cite_type) else \
self.AbbreviationMap["unknown"]
def __eq__(self, other):
"""
Compare for equality with another citable object.
Equality is reached if all parameters (label, title, author, and cite_type)
are equal.
:param other: The object which should be tested for equality.
:type other: TexCitable
:rtype: bool
:return: Whether or not the objects are equal.
"""
if not isinstance(other, TexCitable):
return ValueError("Equality can only be tested for objects with" +
"the same type")
return self._label == other._label and self._title == other._title and \
self._author == other._author and self._cite_type == other._cite_type
def __lt__(self, other):
"""
Determine if the current object is less than the other one.
The current object is less than the other one if it label is less, or if
the title is less, or if the author is less, or if the cite_type is less
than the one of the other object.
:param other: The object which should be tested against.
:type other: TexCitable
:rtype: bool
:return: Whether or not the current object is less then the other one.
"""
if not isinstance(other, TexCitable):
raise ValueError("Less than can only be tested for objects with" +
"the same type.")
if self._label != other._label:
return self._label < other._label
elif self._title != other._title:
return self._title < other._title
elif self._author != other._author:
return self._author < other._author
elif self._cite_type != other._cite_type:
return self._cite_type < other._cite_type
else:
return False
def shorten(self, ignore_title = "Unknown", ignore_author = "Unknown"):
"""
Shorten the title and the author string of the citable object so that
they can be displayed properly.
This method just alters the internal state of the object.
:param ignore_title: If the title is equal to the given one, shorten is
skipped for the title. (Defaults to 'Unknown')
:type ignore_title: str
:param ignore_author: If the author is equal to the given one, shorten
is skipped for the author. (Defaults to 'Unknown')
:type ignore_author: str
:rtype: TexCitable
:return: The current object
"""
# Smartly shorten the title if it should not be ignored.
if self._title != ignore_title:
self._short_title = self._smart_shorten(self._title, self.MaxTitleLength)
# Shorten the authors if they should not be ignored.
if self._author != ignore_author:
# If the author string contains multiple authors replace them with
# 'et. al.'.
if " and " in self._author:
# There are multiple authors mentioned. Replace them by 'et. al.'.
# And remember where the first author ended.
end_of_first_author = self._author.find(" and ")
self._short_author = self._author[:end_of_first_author].strip() + " et. al."
else:
# There is just one author. So set the variable to the end of the
# string.
end_of_first_author = len(self._author)
self._short_author = self._author
# Just keep the authors surname and not the first and middle names.
# If they are separated by a "," otherwise keep the full name.
if "," in self._author[:end_of_first_author]:
end_of_surname = self._author[:end_of_first_author].find(",")
# Build the resulting name.
self._short_author = self._short_author[:end_of_surname].strip() + \
self._short_author[end_of_first_author:]
return self
def completion(self):
"""
:see TexObject.completion:
"""
return self._label
def extra_info(self, shorten = True):
"""
:see TexObject.extra_info:
"""
if shorten:
if self._short_author is None or \
self._short_title is None:
self.shorten()
author = self._short_author
title = self._short_title
else:
author = self._author
title = self._title
return self._abbreviation + " " + author + " - " + title
class TexCompleter(Completer):
###
# List of Latex commands and options known by the completer.
###
BibliographyCommands = ["bibliography"]
ReferenceCommands = ["ref", "refv"]
CitationCommands = ["cite", "citep", "citev"]
SectioningCommands = ["chapter", "section", "subsection", "subsubsection",
"paragraph", "subparagraph"]
SpecialSectioningCommands = [("addchap", "chapter")]
###
# List of supported VIM file types
###
FileTypes = ["tex", "plaintex"]
###
# Action which the completer supports
###
class Actions(object):
NoAction = 0
Reference = 1
Citation = 2
def __init__(self, user_options):
super(TexCompleter, self).__init__(user_options)
self._action = self.Actions.NoAction
def DebugInfo(self, request_data):
file_name = request_data['filepath']
return "TeX Completer for {}".format(file_name)
def SupportedFiletypes(self):
return self.FileTypes
def ShouldUseNowInner(self, request_data):
self._action = self.Actions.NoAction
# Extract the last command
current_line = request_data['line_value']
word_start = request_data['start_column'] - 1
# As according to the documentation the start_column points to the
# begin of the word which is currently typed, the last command ends
# at exactly this position.
last_command = current_line[:word_start]
if self._WantsReferable(last_command):
self._action = self.Actions.Reference
return True
elif self._WantsCitable(last_command):
self._action = self.Actions.Citation
return True
return False
def ComputeCandidatesInner(self, request_data):
if self._action == self.Actions.Citation:
return self._CollectCitables(request_data)
elif self._action == self.Actions.Reference:
return self._CollectReferables(request_data)
return []
def _ExtractFromCommand(self, content, command_name, starable = False):
"""
Extracts the argument of a LaTeX command.
:param content: The string where the extraction should happen.
:type content: str
:param command_name: The name of the command from which the argument
should be extracted.
:type command_name: str
:param starable: Whether or not a stared version of the command exists,
too. (Defaults to False)
:type starable: bool
:rtype: str
:return: The extracted command argument.
"""
commands = ["\\"+command_name+"{"]
if starable:
commands.append("\\"+command_name+"*{")
for command in commands:
# Try to find the begin of the command in the given content.
begin = content.find(command)
# The command is not in the content. Try the next one.
if begin == -1:
continue
# Determine the begin and the end of the commands argument.
begin += len(command)
end = content.find("}", begin)
# Extract it and remove newlines.
return content[begin:end].replace('\n', ' ').replace('\r', '')
return None
def _ExtractFromOption(self, content, option_name, compoundable = True):
"""
Extracts the argument of an option of a command.
:param content: The string where the extraction should happen.
:type content: str
:param option_name: The name of the option from which the argument
should be extracted.
:type option_name: str
:param compoundable: Whether or not the argument can be enclosed by
curly brackets. (Defaults to True)
:type compoundable: bool
:rtype: str
:return: The extracted option argument.
"""
option = option_name + "="
# Try to find the begin of the option.
begin = content.find(option)
if begin == -1:
# The option is not included in the given content string.
return None
begin += len(option)
# Search for the end of the argument.
if compoundable and content[begin] == "{":
# The argument is surrounded by curly brackets. Extract it by
# finding the corresponding closing one.
begin += 1
end = content.find("}", begin)
else:
# The argument is not surrounded by curly brackets, so search for
# the possible end of the argument.
end = begin
while end < len(content) and \
(content[end] != " " and content[end] != "," and \
content[end] != "]" and content[end] != "}"):
end += 1
# Extract it and remove newlines.
return content[begin:end].replace('\n', ' ').replace('\r', '')
def _ExtractFromOptionOrCommand(self, content, name, starable = False,
compoundable = True):
"""
Extracts an argument from either an option or a command.
:param content: The string where the extraction should happen.
:type content: str
:param name: The name of the option from which the argument should be
extracted.
:type name: str
:param starable: Whether or not a stared version of the command exists,
too. (Defaults to False)
:type starable: bool
:param compoundable: Whether or not the argument can be enclosed by
curly brackets. (Defaults to True)
:type compoundable: bool
:rtype: str
:return: The extracted argument if possible.
"""
# First try whether the given name is included as option in the given
# content.
extracted = self._ExtractFromOption(content, name, compoundable)
if extracted is not None:
return extracted
# Next try if the given name is included as a command in the given
# content.
extracted = self._ExtractFromCommand(content, name, starable)
if extracted is not None:
return extracted
# It was neither found is argument of a command nor of an option.
return None
def _WantsReferable(self, line):
"""
This method checks if the line given ends with a LaTeX reference
command.
:param line: The line of text which should be checked.
:type line: str
:rtype: bool
:return: Whether or not given line end with a LaTeX reference command.
"""
for command in self.ReferenceCommands:
if line.endswith("\\" + command + "{"):
return True
return False
def _WantsCitable(self, line):
"""
This method checks if the line given ends with a LaTeX citation
command.
:param line: The line of text which should be checked.
:type line: str
:rtype: bool
:return: Whether or not given line end with a LaTeX citation command.
"""
for command in self.CitationCommands:
if line.endswith("\\" + command + "{"):
return True
return False
def _CollectReferables(self, request_data):
"""
Create the YCM compatible list of all referable objects which could be
found.
:param request_data: The data which YouCompleteMe passes to the
completer.
:type request_data: dict[str,str]
:rtype: list[dict[str,str]]
:return: A list of all referable objects which could be found in a format
which YCM understands.
"""
referables = self._CollectReferablesInner(request_data)
return [ BuildCompletionData(
r.completion(),
extra_menu_info=r.extra_info()
) for r in referables ]
def _CollectReferablesInner(self, request_data):
"""
Create a list of all referable objects which could be found.
:param request_data: The data which YouCompleteMe passes to the
completer.
:type request_data: dict[str,str]
:rtype: list[TexReferable]
:return: A list of all referable objects which could be found.
"""
referables = []
# Get the directory where to search for the files.
file_dir = request_data['filepath']
if not isdir(file_dir):
# Use the corresponding directory if the file path points to a file.
file_dir = dirname(file_dir)
for tex_file_name in self._GetAllTexFiles(file_dir):
try:
with open(tex_file_name, "r") as tex_file:
content = tex_file.read()
logger.debug("Get referables from {}".format(tex_file_name))
# Add all the referable objects which are found in the current
# file to the overall list.
referables.extend(self._GetAllReferables(content))
except IOError as e:
# The file could somehow not be opened. Skip it.
logger.warn("Could not open {} for inspection".format(
tex_file_name))
return sorted(referables)
def _CollectCitables(self, request_data):
"""
Create the YCM compatible list of all citable objects which could be
found.
:param request_data: The data which YouCompleteMe passes to the
completer.
:type request_data: dict[str,str]
:rtype: list[dict[str,str]]
:return: A list of all citable objects which could be found in a format
which YCM understands.
"""
citables = self._CollectCitablesInner(request_data)
return [ BuildCompletionData(
c.completion(),
extra_menu_info=c.extra_info()
) for c in citables]
def _CollectCitablesInner(self, request_data):
"""
Create a list of all citable objects which could be found.
:param request_data: The data which YouCompleteMe passes to the
completer.
:type request_data: dict[str,str]
:rtype: list[TexCitable]
:return: A list of all citable objects which could be found.
"""
citables = []
bibliographies = []
# Get the directory where to search for the files.
file_dir = request_data['filepath']
if not isdir(file_dir):
# Use the corresponding directory if the file path points to a file.
file_dir = dirname(file_dir)
# 1. Scan all found tex-files for a bibliography command.
for tex_file_name in self._GetAllTexFiles(file_dir):
try:
with open(tex_file_name, "r") as tex_file:
content = tex_file.read()
# Add all found bib-files mentioned in this file to the
# overall list.
bibliographies.extend(self._GetAllBibliographies(content))
except IOError as e:
# The file could somehow not be opened.
logger.warn("Could not open {} for inspection".format(
tex_file_name))
# 2. Parse the corresponding Bibtex-files.
for bib in bibliographies:
bib_file_name = join(file_dir, bib + ".bib")
if isfile(bib_file_name):
# Open the file and parse it
try:
with open(bib_file_name, "r") as bib_file:
content = bib_file.read()
logger.debug("Get citables from {}".format(bib_file_name))
# Add all citables found in this bibliography file to the
# overall list.
citables.extend(self._GetAllCitables(content))
except IOError as e:
# The file could somehow not be opened.
logger.warn("Could not open {} for inspection".format(
bib_file_name))
else:
# The file does not exist. Ignore it.
logger.warn("Bibliography {} does not exist".format(
bib_file_name))
return sorted(citables)
def _GetAllTexFiles(self, directory):
"""
Get the list of all tex-files which are present in the specified
directory.
:param directory: The directory of interest.
:type directory: str
:rtype: list[str]
:return: A list of all tex-files found in the directory.
"""
return [join(directory, f) for f in listdir(directory)
if isfile(join(directory, f)) and splitext(f)[1] == ".tex"]
def _GetAllReferables(self, file_content):
"""
Parse the given content for labels which can be later referenced.
:param file_content: The content of the file which should be examined.
:type file_content: str
:rtype: list[TexReferable]
:return: The list of all referable objects in the file.
"""
found_referables = []
for line_nr, line in enumerate(file_content.splitlines()):
label = self._ExtractFromOptionOrCommand(line, "label")
if label is not None:
name, ref_type = self._GetAdditionalReferableInformation(
file_content, label)
referable = TexReferable(label=label, name=name,
ref_type=ref_type)
referable.shorten("No Name")
found_referables.append(referable)
return found_referables
def _GetAdditionalReferableInformation(self, file_content, label):
"""
Parse the given content for additional information to a given label of a
referable object.
:param file_content: The content of the file which should be examined.
:type file_content: str
:param label: The label of the referable object.
:type label: str
:rtype: (str,str)
:return: A tuple containing the name and the type of the corresponding
referable object.
"""
name = "No Name"
ref_type = "unknown"
# Search for the definition of the label in the text.
label_pos = 0
while True:
label_pos = file_content.find(label, label_pos)
# Was the label actually contained in the file?
if label_pos == -1:
break
# Check that this is actually the label definition and not a
# reference to it.
if file_content[:label_pos].endswith("label=") or \
file_content[:label_pos].endswith(r"\label{"):
# This is actually the definition so break here.
break
else:
# Continue searching.
label_pos += len(label)
if label_pos != -1:
# The label was found in the file. Continue to search for additional
# information.
# Search from the label position beginning backwards until another latex
# command is found. If this command is either a begin or a sectioning
# command use it to extract the data. Otherwise continue to search.
current_pos = label_pos
while current_pos >= 0:
current_pos = file_content.rfind("\\", 0, current_pos)
if current_pos == -1:
# Nothing could be found any more. Leave the loop
break
current_content = file_content[current_pos:label_pos]
if current_content.startswith(r"\begin{"):
# This is a begin command. Which begins an environment.
# Search inside within the environment for the caption
# command and extract the reference type which is the
# environment type.
# Extract the reference type.
ref_type = self._ExtractFromCommand(current_content, "begin")
# Extract the name from the environment
env_begin = current_pos
env_end = file_content.find(r"\end{" + ref_type + "}", env_begin)
env_content = file_content[env_begin:env_end]
found_name = self._ExtractFromOptionOrCommand(env_content,
"caption")
if name is not None:
# The name of the referable object could not be
# determined. So use the default.
name = found_name
# Leave the search loop as the needed information is found.
break
else:
found = False
# Check for all sectioning commands.
for command in self.SectioningCommands:
# Try if the command can be found in the currently
# examined content.
found_name = self._ExtractFromCommand(current_content,
command, starable=True)
if found_name is not None:
# The command was found and the name directly
# extracted. So just use extract also the reference
# type which is the command name and finish the
# search.
name = found_name
ref_type = command
found = True
break
# Leave the search loop as the needed information is found.
if found:
break
for command, command_type in self.SpecialSectioningCommands:
# Try if this command can be found in the currently
# examined content.
found_name = self._ExtractFromCommand(current_content,
command)
if found_name is not None:
# The command could be found. The name is already
# extracted, so just use it. The reference type is
# the command_type.
name = found_name
ref_type = command_type
found = True
break
# Leave the search loop as the needed information is found.
if found:
break
else:
# The label could not be found in the file. Something went wrong!
logger.warn("Could not find additional information " + \
"to referable {}".format(label))
return (name, ref_type)
def _GetAllBibliographies(self, file_content):
"""
Parse the given content for mentioned bibliographies and return them.
:param file_content: The content of the file which should be examined.
:type file_content: str
:rtype: list[str]
:return: The list of all bibliographies found in the file.
"""
found_bibliographies = []
for line in file_content.splitlines():
# Check whether a bibliography command is in the current line.
for command in self.BibliographyCommands:
biblographies = self._ExtractFromCommand(line, command)
if biblographies is not None:
# The command to define a bibliography is in this line.
# Add then to the list.
found_bibliographies.extend(biblographies.split(","))
return found_bibliographies
def _GetAllCitables(self, file_content):
"""
Parse the given content for citable objects and return them.
:param file_content: The content of the file which should be examined.
:type file_content: str
:rtype: list[TexCitable]
:return: The list of all found citable objects.
"""
found_citables = []
database = bibtexparser.loads(file_content)
for entry in database.entries:
# Extract the needed data from the entry.
label = entry['ID']
title = entry['title'] if entry.has_key('title') else "No Title"
author = entry['author'] if entry.has_key('author') else "No Author"
cite_type = entry['ENTRYTYPE']
citable = TexCitable(label=label, title=title, author=author,
cite_type=cite_type)
citable.shorten("No Title", "No Author")
found_citables.append(citable)
return found_citables
###
# Enable the file to be runnable as script, too.
###
if __name__ == "__main__":
# Additional imports:
from os import getcwd
from os.path import isabs, expanduser
from argparse import ArgumentParser
# Command line options for the script.
options = ArgumentParser(prog="tex_completer",
description="A semantic completer for YCM supporting TeX files")