Skip to content

Commit 4189184

Browse files
committed
Merge branch 'st4-develop'
2 parents 4876cb8 + ca5fb54 commit 4189184

3 files changed

Lines changed: 69 additions & 33 deletions

File tree

plugins/wiki_page.py

Lines changed: 64 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,26 @@ def select_page(self, pagename):
184184
else:
185185
self.open_new_file(pagename)
186186

187+
def project_folders(self):
188+
window = self.view.window()
189+
if window and window.folders():
190+
return window.folders()
191+
192+
filename = self.view.file_name()
193+
if filename:
194+
return [os.path.dirname(filename)]
195+
196+
return []
197+
187198
def find_files_with_name(self, pagename):
188199
pagename = pagename.replace("\\", os.sep).replace(os.sep + os.sep, os.sep).strip()
189200

190-
self.current_file = self.view.file_name()
191-
self.current_dir = os.path.dirname(self.current_file)
192-
logger.debug("Locating page '%s' in: %s" % (pagename, self.current_dir))
201+
search_dirs = self.project_folders()
202+
if not search_dirs:
203+
return []
204+
205+
sublime.status_message("Locating page '{}' in: {}".format(pagename, search_dirs))
206+
logger.debug("Locating page '%s' in: %s" % (pagename, search_dirs))
193207

194208
markdown_extension = self.view.settings().get(
195209
"mde.wikilinks.markdown_extension", DEFAULT_MARKDOWN_EXTENSION
@@ -201,34 +215,40 @@ def find_files_with_name(self, pagename):
201215
else:
202216
search_pattern = "^%s%s$" % (pagename, markdown_extension)
203217

204-
# Scan directory tree for files that match the pagename...
218+
# Scan project directory trees for files that match the pagename...
205219
results = []
206-
for dirname, _, files in self.list_dir_tree(self.current_dir):
207-
for file in files:
208-
if re.search(search_pattern, file):
209-
filename = os.path.join(dirname, file)
210-
results.append([self.extract_page_name(filename), filename])
220+
for search_dir in search_dirs:
221+
for dirname, _, files in self.list_dir_tree(search_dir):
222+
for file in files:
223+
if re.search(search_pattern, file):
224+
filename = os.path.join(dirname, file)
225+
results.append([self.extract_page_name(filename), filename])
211226

212227
return results
213228

214229
def find_files_with_ref(self):
215-
self.current_file = self.view.file_name()
216-
self.current_dir, current_base = os.path.split(self.current_file)
217-
self.current_name, _ = os.path.splitext(current_base)
230+
current_file = self.view.file_name()
231+
if not current_file:
232+
return []
233+
_, current_base = os.path.split(current_file)
234+
search_dirs = self.project_folders()
235+
sublime.status_message("Showing backlinks from: {}".format(search_dirs))
236+
current_name, _ = os.path.splitext(current_base)
218237

219238
markdown_extension = self.view.settings().get(
220239
"mde.wikilinks.markdown_extension", DEFAULT_MARKDOWN_EXTENSION
221240
)
222241

223242
results = []
224-
for dirname, _, files in self.list_dir_tree(self.current_dir):
225-
for file in files:
226-
page_name, extension = os.path.splitext(file)
227-
filename = os.path.join(dirname, file)
228-
if extension == markdown_extension and self.contains_ref(
229-
filename, self.current_name
230-
):
231-
results.append([page_name, filename])
243+
for search_dir in search_dirs:
244+
for dirname, _, files in self.list_dir_tree(search_dir):
245+
for file in files:
246+
page_name, extension = os.path.splitext(file)
247+
filename = os.path.join(dirname, file)
248+
if extension == markdown_extension and self.contains_ref(
249+
filename, current_name
250+
):
251+
results.append([page_name, filename])
232252

233253
return results
234254

@@ -255,8 +275,19 @@ def select_backlink(self, file_list):
255275

256276
def open_new_file(self, pagename):
257277
current_syntax = self.view.settings().get("syntax")
258-
current_file = self.view.file_name()
259-
current_dir = os.path.dirname(current_file)
278+
search_dirs = self.project_folders()
279+
if not search_dirs:
280+
return
281+
282+
# Prefer current file location
283+
filename = self.view.file_name()
284+
if filename:
285+
current_dir = os.path.dirname(filename)
286+
else:
287+
# Fallback to first project folder
288+
current_dir = search_dirs[0]
289+
290+
sublime.status_message("New page location: {}".format(current_dir))
260291

261292
markdown_extension = self.view.settings().get(
262293
"mde.wikilinks.markdown_extension", DEFAULT_MARKDOWN_EXTENSION
@@ -335,9 +366,9 @@ def replace_selection_with_pagename(self, selected_index):
335366
def find_matching_files(self, word_region):
336367
word = None if word_region.empty() else self.view.substr(word_region)
337368

338-
current_file = self.view.file_name()
339-
current_dir, current_base = os.path.split(current_file)
340-
logger.debug("Finding matching files for %s in %s", word, current_dir)
369+
search_dirs = self.project_folders()
370+
sublime.status_message("Finding matching files for {} in {}".format(word, search_dirs))
371+
logger.debug("Finding matching files for %s in %s", word, search_dirs)
341372

342373
markdown_extension = self.view.settings().get(
343374
"mde.wikilinks.markdown_extension", DEFAULT_MARKDOWN_EXTENSION
@@ -347,15 +378,16 @@ def find_matching_files(self, word_region):
347378
if word is not None and word.endswith(markdown_extension):
348379
word = word[: -len(markdown_extension)]
349380

350-
# Scan directory tree for potential filenames that contain the word...
381+
# Scan project directory trees for potential filenames that contain the word...
351382
results = []
352-
for dirname, _, files in self.list_dir_tree(current_dir):
353-
for file in files:
354-
page_name, extension = os.path.splitext(file)
355-
filename = os.path.join(dirname, file)
383+
for search_dir in search_dirs:
384+
for dirname, _, files in self.list_dir_tree(search_dir):
385+
for file in files:
386+
page_name, extension = os.path.splitext(file)
387+
filename = os.path.join(dirname, file)
356388

357-
if extension == markdown_extension and (not word or word in page_name):
358-
results.append([page_name, filename])
389+
if extension == markdown_extension and (not word or word in page_name):
390+
results.append([page_name, filename])
359391

360392
return results
361393

syntaxes/Markdown.sublime-syntax

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3253,7 +3253,7 @@ contexts:
32533253
footnote-def-continuation:
32543254
# if line starts with footnote or reference definition,
32553255
# rollback to previous line and pop footnote contexts.
3256-
- match: ^(?=[ \t]*{{reference_definition}})
3256+
- match: ^(?=[ \t]*(?:{{atx_heading}}|{{reference_definition}}))
32573257
fail: footnote-def-end
32583258
- match: ^
32593259
pop: 1

tests/syntax_test_markdown.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3279,6 +3279,10 @@ with a *second* line.
32793279
| ^ punctuation.definition.reference.end.markdown
32803280
| ^ punctuation.separator.key-value.markdown
32813281

3282+
# not-a-foot-note
3283+
| <- markup.heading.1.markdown punctuation.definition.heading.begin.markdown - meta.link
3284+
|^^^^^^^^^^^^^^^^^ markup.heading.1.markdown - meta.link
3285+
32823286
## https://custom-tests/footnote-reference-definitions/in-block-quotes
32833287

32843288
> [^1]: And that's the footnote.

0 commit comments

Comments
 (0)