Skip to content

Commit 951450c

Browse files
committed
Clean: use constants where possible.
1 parent 44994a8 commit 951450c

File tree

8 files changed

+24
-13
lines changed

8 files changed

+24
-13
lines changed

client/src/CodeChatEditor.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ import "graphviz-webcomponent";
6666
import { tinymce, init, Editor } from "./tinymce-config.mjs";
6767
import {
6868
CodeChatForWeb,
69-
CodeMirrorDocBlockTuple,
7069
CodeMirrorDiffable,
7170
UpdateMessageContents,
7271
CodeMirror,
72+
autosave_timeout_ms,
7373
} from "./shared_types.mjs";
7474
import { show_toast } from "./show_toast.mjs";
7575

@@ -400,7 +400,7 @@ export const startAutosaveTimer = () => {
400400
autosaveTimeoutId = window.setTimeout(() => {
401401
console_log("CodeChat Editor Client: autosaving.");
402402
on_save();
403-
}, 1000);
403+
}, autosave_timeout_ms);
404404
};
405405

406406
const clearAutosaveTimer = () => {

client/src/shared_types.mts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
//
1717
// `shared_types.mts` -- Shared type definitions
1818
// =============================================
19+
// The time, in ms, to wait between the last user edit and sending updated data to the Server.
20+
export const autosave_timeout_ms = 300;
1921
//
2022
// ### Message types
2123
//

extensions/VSCode/src/extension.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import { CodeChatEditorServer, initServer } from "./index";
4040

4141
// ### Local packages
4242
import {
43+
autosave_timeout_ms,
4344
EditorMessage,
4445
MessageResult,
4546
UpdateMessageContents,
@@ -600,7 +601,7 @@ const send_update = (this_is_dirty: boolean) => {
600601
if (idle_timer !== undefined) {
601602
clearTimeout(idle_timer);
602603
}
603-
// ... schedule a render after 300 ms.
604+
// ... schedule a render after an autosave timeout.
604605
idle_timer = setTimeout(async () => {
605606
if (can_render()) {
606607
const ate = vscode.window.activeTextEditor!;
@@ -648,7 +649,7 @@ const send_update = (this_is_dirty: boolean) => {
648649
scroll_position,
649650
);
650651
}
651-
}, 300);
652+
}, autosave_timeout_ms);
652653
}
653654
};
654655

server/src/ide/vscode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ pub fn vscode_ide_core(
183183
// Make sure it's the `Result` message with no errors.
184184
let res =
185185
// First, make sure the ID matches.
186-
if message.id != 0.0 {
186+
if message.id != RESERVED_MESSAGE_ID {
187187
Err(format!("Unexpected message ID {}.", message.id))
188188
} else {
189189
match message.message {

server/src/lexer/supported_languages.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ use super::{
5656
StringDelimiterSpec, pest_parser,
5757
};
5858

59+
pub const MARKDOWN_MODE: &str = "markdown";
60+
5961
// Helper functions
6062
// -----------------------------------------------------------------------------
6163
//
@@ -543,7 +545,7 @@ pub fn get_language_lexer_vec() -> Vec<LanguageLexer> {
543545
),
544546
// ### Markdown
545547
make_language_lexer(
546-
"markdown",
548+
MARKDOWN_MODE,
547549
&["md"],
548550
&[],
549551
&[],

server/src/processing.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ use serde::{Deserialize, Serialize};
5959
use ts_rs::TS;
6060

6161
// ### Local
62-
use crate::lexer::{CodeDocBlock, DocBlock, LEXERS, LanguageLexerCompiled, source_lexer};
62+
use crate::lexer::{
63+
CodeDocBlock, DocBlock, LEXERS, LanguageLexerCompiled, source_lexer,
64+
supported_languages::MARKDOWN_MODE,
65+
};
6366

6467
// Data structures
6568
// -----------------------------------------------------------------------------
@@ -426,7 +429,7 @@ pub fn codechat_for_web_to_source(
426429
};
427430

428431
// If this is a Markdown-only document, handle this special case.
429-
if *lexer.language_lexer.lexer_name == "markdown" {
432+
if *lexer.language_lexer.lexer_name == MARKDOWN_MODE {
430433
// There should be no doc blocks.
431434
if !code_mirror.doc_blocks.is_empty() {
432435
return Err(CodechatForWebToSourceError::DocBlocksNotAllowed);
@@ -802,7 +805,7 @@ pub fn source_to_codechat_for_web(
802805
mode: lexer.language_lexer.lexer_name.to_string(),
803806
},
804807
version,
805-
source: if lexer.language_lexer.lexer_name.as_str() == "markdown" {
808+
source: if lexer.language_lexer.lexer_name.as_str() == MARKDOWN_MODE {
806809
// Document-only files are easy: just encode the contents.
807810
let html = markdown_to_html(file_contents);
808811
// TODO: process the HTML.

server/src/processing/tests.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ use super::{
3434
};
3535
use crate::{
3636
cast,
37-
lexer::{CodeDocBlock, DocBlock, compile_lexers, supported_languages::get_language_lexer_vec},
37+
lexer::{
38+
CodeDocBlock, DocBlock, compile_lexers,
39+
supported_languages::{MARKDOWN_MODE, get_language_lexer_vec},
40+
},
3841
prep_test_dir,
3942
processing::{
4043
CodeDocBlockVecToSourceError, CodeMirrorDiffable, CodeMirrorDocBlockDelete,
@@ -510,7 +513,7 @@ fn test_source_to_codechat_for_web_1() {
510513
assert_eq!(
511514
source_to_codechat_for_web("", &"md".to_string(), 0.0, false, false),
512515
Ok(TranslationResults::CodeChat(build_codechat_for_web(
513-
"markdown",
516+
MARKDOWN_MODE,
514517
"",
515518
vec![]
516519
)))
@@ -526,7 +529,7 @@ fn test_source_to_codechat_for_web_1() {
526529
false,
527530
),
528531
Ok(TranslationResults::CodeChat(build_codechat_for_web(
529-
"markdown",
532+
MARKDOWN_MODE,
530533
&format!("<p>{lexer_spec}markdown</p>\n"),
531534
vec![]
532535
)))

server/tests/overall_core/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ async fn test_server_core(
678678
file_path: md_path_str.clone(),
679679
contents: Some(CodeChatForWeb {
680680
metadata: SourceFileMetadata {
681-
mode: "markdown".to_string()
681+
mode: MARKDOWN_MODE.to_string()
682682
},
683683
source: CodeMirrorDiffable::Diff(CodeMirrorDiff {
684684
doc: vec![StringDiff {

0 commit comments

Comments
 (0)