Skip to content
Open

Dev2 #111

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions client/src/CodeChatEditor.mts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import { show_toast } from "./show_toast.mjs";

// ### CSS
import "./css/CodeChatEditor.css";
import { CursorPosition } from "./rust-types/CursorPosition.js";

// Data structures
// ---------------
Expand Down Expand Up @@ -103,12 +104,12 @@ declare global {
open_lp: (
codechat_for_web: CodeChatForWeb,
is_re_translation: boolean,
cursor_line?: number,
cursor_position?: CursorPosition,
scroll_line?: number,
) => Promise<void>;
on_save: (_only_if_dirty: boolean) => Promise<void>;
scroll_to_line: (
cursor_line?: number,
cursor_position?: CursorPosition,
scroll_line?: number,
) => void;
show_toast: (text: string) => void;
Expand Down Expand Up @@ -172,7 +173,7 @@ const is_doc_only = () => {
const open_lp = async (
codechat_for_web: CodeChatForWeb,
is_re_translation: boolean,
cursor_line?: number,
cursor_line?: CursorPosition,
scroll_line?: number,
) =>
// Wait for the DOM to load before opening the file.
Expand Down Expand Up @@ -205,7 +206,7 @@ const _open_lp = async (
// associated metadata. See [`AllSource`](#AllSource).
codechat_for_web: CodeChatForWeb,
is_re_translation: boolean,
cursor_line?: number,
cursor_position?: CursorPosition,
scroll_line?: number,
) => {
// Note that globals, such as `is_dirty` and document contents, may change
Expand Down Expand Up @@ -331,7 +332,7 @@ const _open_lp = async (
}
}
await mathJaxTypeset(codechat_body);
scroll_to_line(cursor_line, scroll_line);
scroll_to_line(cursor_position, scroll_line);
} else {
if (is_dirty && "Diff" in source) {
// Send an `OutOfSync` response, so that the IDE will send the
Expand All @@ -349,7 +350,7 @@ const _open_lp = async (
codechat_body,
codechat_for_web,
[],
cursor_line,
cursor_position,
scroll_line,
);
}
Expand Down Expand Up @@ -656,11 +657,14 @@ const save_then_navigate = (codeChatEditorUrl: URL) => {

// This can be called by the framework. Therefore, make no assumptions about
// variables being valid; it be called before a file is loaded, etc.
const scroll_to_line = (cursor_line?: number, scroll_line?: number) => {
const scroll_to_line = (
cursor_position?: CursorPosition,
scroll_line?: number,
) => {
if (is_doc_only()) {
// TODO.
} else {
codemirror_scroll_to_line(cursor_line, scroll_line);
codemirror_scroll_to_line(cursor_position, scroll_line);
}
};

Expand Down
5 changes: 3 additions & 2 deletions client/src/CodeChatEditorFramework.mts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
on_dom_content_loaded,
} from "./CodeChatEditor.mjs";
import { ResultErrTypes } from "./rust-types/ResultErrTypes.js";
import { CursorPosition } from "./rust-types/CursorPosition.js";

// Websocket
// ---------
Expand Down Expand Up @@ -427,7 +428,7 @@ const get_client = () => root_iframe?.contentWindow?.CodeChatEditor;
const set_content = async (
contents: CodeChatForWeb,
is_re_translation: boolean,
cursor_line?: number,
cursor_position?: CursorPosition,
scroll_line?: number,
) => {
const client = get_client();
Expand All @@ -448,7 +449,7 @@ const set_content = async (
await root_iframe!.contentWindow!.CodeChatEditor.open_lp(
contents,
is_re_translation,
cursor_line,
cursor_position,
scroll_line,
);
}
Expand Down
32 changes: 21 additions & 11 deletions client/src/CodeMirror-integration.mts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ import {
} from "./shared.mjs";
import { assert } from "./assert.mjs";
import { show_toast } from "./show_toast.mjs";
import { CursorPosition } from "./rust-types/CursorPosition";

// Globals
// -------
Expand Down Expand Up @@ -922,7 +923,7 @@ export const CodeMirror_load = async (
codechat_for_web: CodeChatForWeb,
// Additional extensions.
extensions: Array<Extension>,
cursor_line?: number,
cursor_position?: CursorPosition,
scroll_line?: number,
) => {
if ("Plain" in codechat_for_web.source) {
Expand Down Expand Up @@ -1135,12 +1136,15 @@ export const CodeMirror_load = async (
annotations: noAutosaveAnnotation.of(true),
});
}
scroll_to_line(cursor_line, scroll_line);
scroll_to_line(cursor_position, scroll_line);
};

// Scroll to the provided `scroll_line`; place the cursor at `cursor_line`.
export const scroll_to_line = (cursor_line?: number, scroll_line?: number) => {
if (cursor_line === undefined && scroll_line === undefined) {
export const scroll_to_line = (
cursor_position?: CursorPosition,
scroll_line?: number,
) => {
if (cursor_position === undefined && scroll_line === undefined) {
return;
}

Expand All @@ -1150,13 +1154,19 @@ export const scroll_to_line = (cursor_line?: number, scroll_line?: number) => {
const dispatch_data: TransactionSpec = {
annotations: noAutosaveAnnotation.of(true),
};
if (cursor_line !== undefined) {
if (cursor_position !== undefined) {
// Translate the line numbers to a position.
const cursor_pos = current_view?.state.doc.line(cursor_line).from;
dispatch_data.selection = {
anchor: cursor_pos,
head: cursor_pos,
};
if ("Line" in cursor_position) {
const cursor_pos = current_view?.state.doc.line(
cursor_position.Line,
).from;
dispatch_data.selection = {
anchor: cursor_pos,
head: cursor_pos,
};
} else {
report_error("Not supported.");
}
// If a scroll position is provided, use it; otherwise, scroll the
// cursor into the current view.
if (scroll_line == undefined) {
Expand Down Expand Up @@ -1222,7 +1232,7 @@ export const set_CodeMirror_positions = (
current_view.state.selection.main.from,
).number;
}
update_message_contents.cursor_position = cursor_line;
update_message_contents.cursor_position = { Line: cursor_line };

// `current_view.viewport.from` isn't accurate, since it's not really the
// top line, but a margin before it; see the
Expand Down
24 changes: 14 additions & 10 deletions extensions/VSCode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,19 +437,23 @@ export const activate = (context: vscode.ExtensionContext) => {
);
}

const cursor_line = current_update.cursor_position;
if (cursor_line !== undefined && editor) {
const cursor_position =
current_update.cursor_position;
if (cursor_position !== undefined && editor) {
assert("Line" in cursor_position);
const cursor_line = cursor_position.Line;
ignore_selection_change = true;
const cursor_position = new vscode.Position(
// The VSCode line is zero-based; the
// CodeMirror line is one-based.
cursor_line - 1,
0,
);
const vscode_cursor_position =
new vscode.Position(
// The VSCode line is zero-based; the
// CodeMirror line is one-based.
cursor_line - 1,
0,
);
editor.selections = [
new vscode.Selection(
cursor_position,
cursor_position,
vscode_cursor_position,
vscode_cursor_position,
),
];
// I'd prefer to set `ignore_selection_change =
Expand Down
28 changes: 2 additions & 26 deletions server/.cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,7 @@ TS_RS_EXPORT_DIR = { value = "../client/src/rust-types", relative = true }
# TypeScript's convention.
TS_RS_IMPORT_EXTENSION = "ts"

# Code coverage, the manual way:
#
# 1. Run `cargo install rustfilt` per the
# [code coverage docs](https://doc.rust-lang.org/rustc/instrument-coverage.html#building-the-demangler).
# 2. You must manually run `rustup component add llvm-tools-preview` following
# the
# [coverge docs](https://doc.rust-lang.org/rustc/instrument-coverage.html#installing-llvm-coverage-tools).
# Per some searching, also run `cargo install cargo-binutils` to put these
# tools in the path.
# 3. In Powershell, `$Env:RUSTFLAGS = "-C instrument-coverage"` then `cargo
# test`. When the tests run, record the name of the test binary.
# 4. `rust-profdata merge -sparse default_*.profraw -o default.profdata`.
# 5. `rust-cov show --Xdemangler=rustfilt
# target\debug\deps\code_chat_editor-4dbe5c7815a53cd9.exe
# --instr-profile=default.profdata --ignore-filename-regex=\\.cargo\\registry
# --format=html --output-dir=coverage`, replacing the binary path with the
# one recorded in step 3.
# 6. Open the file `coverage\index.html`.
#
# Or, `cargo install cargo-tarpaulin` then `cargo tarpaulin --ignore-panics
# --out=html --skip-clean`.

[build]
# Set these to match the output from `cargo tarpaulin --print-rust-flags` to
# avoid recompiles.
#
# This is commented out; for development, uncomment this.
##rustflags = ["-Cdebuginfo=2", "-Cstrip=none", "--cfg=tarpaulin", "-Cinstrument-coverage"]
# To check coverage, `cargo install cargo-tarpaulin` then `cargo tarpaulin
# --skip-clean --out=html --target-dir=tarpaulin`.
8 changes: 3 additions & 5 deletions server/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@
target/

# Output from profiling.
tarpaulin/
*.profraw
tarpaulin-report.html

# Copied files needed to `cargo publish`.
static/
# Other files.
hashLocations.json

config.json
*.log
.windows/

# CodeChat Editor lexer: python. See TODO.
8 changes: 4 additions & 4 deletions server/src/ide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ use crate::{
processing::{CodeChatForWeb, CodeMirror, CodeMirrorDiffable, SourceFileMetadata},
translation::{CreatedTranslationQueues, create_translation_queues},
webserver::{
self, EditorMessage, EditorMessageContents, INITIAL_IDE_MESSAGE_ID, MESSAGE_ID_INCREMENT,
REPLY_TIMEOUT_MS, ResultErrTypes, ResultOkTypes, UpdateMessageContents, WebAppState,
setup_server,
self, CursorPosition, EditorMessage, EditorMessageContents, INITIAL_IDE_MESSAGE_ID,
MESSAGE_ID_INCREMENT, REPLY_TIMEOUT_MS, ResultErrTypes, ResultOkTypes,
UpdateMessageContents, WebAppState, setup_server,
},
};

Expand Down Expand Up @@ -271,7 +271,7 @@ impl CodeChatEditorServer {
) -> std::io::Result<f64> {
self.send_message_timeout(EditorMessageContents::Update(UpdateMessageContents {
file_path,
cursor_position,
cursor_position: cursor_position.map(CursorPosition::Line),
scroll_position: scroll_position.map(|x| x as f32),
is_re_translation: false,
contents: option_contents.map(|contents| CodeChatForWeb {
Expand Down
2 changes: 2 additions & 0 deletions server/src/ide/filewatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ pub const FILEWATCHER_PATH_PREFIX: &[&str] = &["fw", "fsc"];
/// replaced by something better.
///
/// Redirect from the root of the filesystem to the actual root path on this OS.
#[cfg(not(tarpaulin_include))]
pub async fn filewatcher_root_fs_redirect() -> impl Responder {
HttpResponse::TemporaryRedirect()
.insert_header((header::LOCATION, "/fw/fsb/"))
Expand Down Expand Up @@ -310,6 +311,7 @@ async fn dir_listing(web_path: &str, dir_path: &Path) -> HttpResponse {
/// Copied almost verbatim from the
/// [win\_partitions crate](https://docs.rs/crate/win_partitions/0.3.0/source/src/win_api.rs#144)
/// when compilation errors broke the crate.
#[cfg(not(tarpaulin_include))]
#[cfg(target_os = "windows")]
pub fn get_logical_drive() -> Result<Vec<char>, std::io::Error> {
let bitmask = unsafe { GetLogicalDrives() };
Expand Down
Loading
Loading