Skip to content

Commit

Permalink
Merge pull request #344 from alcarney/fix-preview
Browse files Browse the repository at this point in the history
Fix preview on Windows
  • Loading branch information
alcarney authored Mar 22, 2022
2 parents 4a6c911 + 4c39790 commit 59a3c4b
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 5 deletions.
1 change: 1 addition & 0 deletions code/changes/340.fix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix handling of Windows URIs in preview code.
2 changes: 1 addition & 1 deletion code/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export const PYTHON_EXTENSION = "ms-python.python"

export namespace Server {
export const REQUIRED_PYTHON = "3.6.0"
export const REQUIRED_VERSION = "0.9.0"
export const REQUIRED_VERSION = "0.10.2"
}

export namespace Commands {
Expand Down
4 changes: 2 additions & 2 deletions code/src/preview/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@ export class PreviewManager {
let buildDir = config.buildDir
let sourceUri = editor.document.uri

if (sourceUri.scheme !== 'file' || !sourceUri.path.startsWith(srcDir)) {
if (sourceUri.scheme !== 'file' || !sourceUri.fsPath.startsWith(srcDir)) {
this.logger.debug(`Ignoring ${sourceUri}`)
return undefined
}


let rstPath = sourceUri.path.replace(srcDir, '')
let rstPath = sourceUri.fsPath.replace(srcDir, '')
let htmlPath = rstPath.replace(new RegExp(`\\${path.extname(rstPath)}`), '.html')

// Check if the file exists.
Expand Down
2 changes: 1 addition & 1 deletion lib/esbonio/.bumpversion.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ current_version = 0.10.1
commit = False
tag = False
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(.dev(?P<dev>\d+))?
serialize =
serialize =
{major}.{minor}.{patch}.dev{dev}
{major}.{minor}.{patch}

Expand Down
1 change: 1 addition & 0 deletions lib/esbonio/changes/341.fix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Previews on Windows should now start correctly
14 changes: 13 additions & 1 deletion lib/esbonio/esbonio/lsp/sphinx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import traceback
import typing
from multiprocessing import Process
from multiprocessing import Queue
from typing import Any
from typing import Dict
from typing import Iterator
Expand Down Expand Up @@ -40,6 +41,7 @@
from esbonio.lsp.rst import RstLanguageServer
from esbonio.lsp.rst import ServerConfig
from esbonio.lsp.sphinx.preview import make_preview_server
from esbonio.lsp.sphinx.preview import start_preview_server

try:
from sphinx.util.logging import OnceFilter
Expand Down Expand Up @@ -478,14 +480,24 @@ def preview(self, options: Dict[str, Any]) -> Dict[str, Any]:

return {}

if not self.preview_process:
if not self.preview_process and not IS_WIN:
self.logger.debug("Starting preview server.")
server = make_preview_server(self.app.outdir)
self.preview_port = server.server_port

self.preview_process = Process(target=server.serve_forever, daemon=True)
self.preview_process.start()

if not self.preview_process and IS_WIN:
self.logger.debug("Starting preview server")

q: Queue = Queue()
self.preview_process = Process(
target=start_preview_server, args=(q, self.app.outdir), daemon=True
)
self.preview_process.start()
self.preview_port = q.get()

if options.get("show", True):
self.show_document(
ShowDocumentParams(
Expand Down
14 changes: 14 additions & 0 deletions lib/esbonio/esbonio/lsp/sphinx/preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from functools import partial
from http.server import HTTPServer
from http.server import SimpleHTTPRequestHandler
from multiprocessing import Queue
from typing import Any
from typing import Type

Expand All @@ -25,3 +26,16 @@ def make_preview_server(directory: str) -> HTTPServer:
"""Construst a http server that can be used to preview the docs."""
handler_class = partial(RequestHandler, directory=directory)
return ServerClass(("localhost", 0), handler_class)


def start_preview_server(q: Queue, directory: str):
"""Start a preview server in the given directory.
The server's port number will be sent back via the given ``q`` object.
"""

handler_class = partial(RequestHandler, directory=directory)
server = ServerClass(("localhost", 0), handler_class)
q.put(server.server_port)

server.serve_forever()

0 comments on commit 59a3c4b

Please sign in to comment.