Coverage for C:\Repos\leo-editor\leo\core\leoHistory.py: 50%

60 statements  

« prev     ^ index     » next       coverage.py v6.4, created at 2022-05-24 10:21 -0500

1# -*- coding: utf-8 -*- 

2#@+leo-ver=5-thin 

3#@+node:ekr.20150514154159.1: * @file leoHistory.py 

4#@@first 

5from leo.core import leoGlobals as g 

6assert g 

7#@+others 

8#@+node:ekr.20160514120255.1: ** class NodeHistory 

9class NodeHistory: 

10 """A class encapsulating knowledge of visited nodes.""" 

11 #@+others 

12 #@+node:ekr.20070615131604.1: *3* NodeHistory.ctor 

13 def __init__(self, c): 

14 """Ctor for NodeHistory class.""" 

15 self.c = c 

16 self.beadList = [] # a list of (position,chapter) tuples. 

17 self.beadPointer = -1 

18 self.skipBeadUpdate = False 

19 #@+node:ekr.20160426061203.1: *3* NodeHistory.dump 

20 def dump(self): 

21 """Dump the beadList""" 

22 for i, data in enumerate(self.beadList): 

23 p, chapter = data 

24 p = p.h if p else 'no p' 

25 chapter = chapter.name if chapter else 'main' 

26 mark = '**' if i == self.beadPointer else ' ' 

27 print(f"{mark} {i} {chapter} {p}") 

28 #@+node:ekr.20070615134813: *3* NodeHistory.goNext 

29 def goNext(self): 

30 """Select the next node, if possible.""" 

31 if self.beadPointer + 1 < len(self.beadList): 

32 self.beadPointer += 1 

33 p, chapter = self.beadList[self.beadPointer] 

34 self.select(p, chapter) 

35 #@+node:ekr.20130915111638.11288: *3* NodeHistory.goPrev 

36 def goPrev(self): 

37 """Select the previously visited node, if possible.""" 

38 if self.beadPointer > 0: 

39 self.beadPointer -= 1 

40 p, chapter = self.beadList[self.beadPointer] 

41 self.select(p, chapter) 

42 #@+node:ekr.20130915111638.11294: *3* NodeHistory.select 

43 def select(self, p, chapter): 

44 """ 

45 Update the history list when selecting p. 

46 Called only from self.goToNext/PrevHistory 

47 """ 

48 c, cc = self.c, self.c.chapterController 

49 if c.positionExists(p): 

50 self.skipBeadUpdate = True 

51 try: 

52 oldChapter = cc.getSelectedChapter() 

53 if oldChapter != chapter: 

54 cc.selectChapterForPosition(p, chapter=chapter) 

55 c.selectPosition(p) # Calls cc.selectChapterForPosition 

56 finally: 

57 self.skipBeadUpdate = False 

58 # Fix bug #180: Always call self.update here. 

59 self.update(p, change=False) 

60 #@+node:ville.20090724234020.14676: *3* NodeHistory.update 

61 def update(self, p, change=True): 

62 """ 

63 Update the beadList while p is being selected. 

64 Called *only* from c.frame.tree.selectHelper. 

65 """ 

66 c, cc = self.c, self.c.chapterController 

67 if not p or not c.positionExists(p) or self.skipBeadUpdate: 

68 return 

69 # A hack: don't add @chapter nodes. 

70 # These are selected during the transitions to a new chapter. 

71 if p.h.startswith('@chapter '): 

72 return 

73 # Fix bug #180: handle the change flag. 

74 aList, found = [], -1 

75 for i, data in enumerate(self.beadList): 

76 p2, junk_chapter = data 

77 if c.positionExists(p2): 

78 if p == p2: 

79 if change: 

80 pass # We'll append later. 

81 elif found == -1: 

82 found = i 

83 aList.append(data) 

84 else: 

85 pass # Remove any duplicate. 

86 else: 

87 aList.append(data) 

88 if change or found == -1: 

89 data = p.copy(), cc.getSelectedChapter() 

90 aList.append(data) 

91 self.beadPointer = len(aList) - 1 

92 else: 

93 self.beadPointer = found 

94 self.beadList = aList 

95 #@-others 

96#@-others 

97#@@language python 

98#@@tabwidth -4 

99#@@pagewidth 70 

100#@-leo