Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Nov 19, 2023
1 parent e6b9ee6 commit 5f8b7a2
Show file tree
Hide file tree
Showing 4 changed files with 237 additions and 226 deletions.
52 changes: 34 additions & 18 deletions packages/markdown/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export function create(): Service<Provide> {
return {} as any;
}

let lastProjectVersion = context.host.getProjectVersion();
assert(context.env, 'context.env must be defined');
let lastProjectVersion: string | undefined;

const { fs, onDidChangeWatchedFiles } = context.env;
assert(fs, 'context.env.fs must be defined');
assert(
Expand Down Expand Up @@ -61,15 +61,15 @@ export function create(): Service<Provide> {
for (const change of event.changes) {
switch (change.type) {
case 2 satisfies typeof FileChangeType.Changed: {
const document = context.getTextDocument(change.uri);
const document = getTextDocument(change.uri, false);
if (document) {
onDidChangeMarkdownDocument.fire(document);
}
break;
}

case 1 satisfies typeof FileChangeType.Created: {
const document = context.getTextDocument(change.uri);
const document = getTextDocument(change.uri, false);
if (document) {
onDidCreateMarkdownDocument.fire(document);
}
Expand All @@ -95,7 +95,7 @@ export function create(): Service<Provide> {
},

hasMarkdownDocument(resource) {
const document = context.getTextDocument(String(resource));
const document = getTextDocument(resource.toString(), true);
return Boolean(document && isMarkdown(document));
},

Expand All @@ -106,7 +106,7 @@ export function create(): Service<Provide> {
onDidDeleteMarkdownDocument: onDidDeleteMarkdownDocument.event,

async openMarkdownDocument(resource) {
return context.getTextDocument(String(resource));
return getTextDocument(resource.toString(), true);
},

async readDirectory(resource) {
Expand Down Expand Up @@ -136,26 +136,34 @@ export function create(): Service<Provide> {
const syncedVersions = new Map<string, TextDocument>();

const sync = () => {
const newProjectVersion = context.host.getProjectVersion();
const shouldUpdate = newProjectVersion !== lastProjectVersion;
const newProjectVersion = context.project.typescript?.projectHost.getProjectVersion?.();
const shouldUpdate = newProjectVersion === undefined || newProjectVersion !== lastProjectVersion;
if (!shouldUpdate) {
return;
}

lastProjectVersion = newProjectVersion;

const oldVersions = new Set(syncedVersions.keys());
const newVersions = new Map<string, TextDocument>();

for (const { root } of context.virtualFiles.allSources()) {
const embeddeds = [root];
root.embeddedFiles.forEach(function walk(embedded) {
embeddeds.push(embedded);
embedded.embeddedFiles.forEach(walk);
});
for (const embedded of embeddeds) {
const document = context.getTextDocument(embedded.fileName);
for (const sourceFile of context.project.fileProvider.getAllSourceFiles()) {
if (sourceFile.root) {
const virtualFiles = [sourceFile.root];
sourceFile.root.embeddedFiles.forEach(function walk(embedded) {
virtualFiles.push(embedded);
embedded.embeddedFiles.forEach(walk);
});
for (const embedded of virtualFiles) {
if (embedded.languageId === 'markdown') {
const document = context.documents.get(embedded.id, embedded.languageId, embedded.snapshot);
newVersions.set(document.uri, document);
}
}
}
else {
const document = context.documents.get(sourceFile.id, sourceFile.languageId, sourceFile.snapshot);
if (document && isMarkdown(document)) {
newVersions.set(String(document.uri), document);
newVersions.set(document.uri, document);
}
}
}
Expand Down Expand Up @@ -311,6 +319,14 @@ export function create(): Service<Provide> {
return result || link;
}
};

function getTextDocument(uri: string, includeVirtualFile: boolean) {
const file = (includeVirtualFile ? context!.project.fileProvider.getVirtualFile(uri)[0] : undefined)
?? context!.project.fileProvider.getSourceFile(uri);
if (file) {
return context!.documents.get(uri, file.languageId, file.snapshot);
}
}
};
}

Expand Down
9 changes: 4 additions & 5 deletions packages/typescript/src/configs/getUserPreferences.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type * as ts from 'typescript/lib/tsserverlibrary';
import { getConfigTitle } from '../shared';
import * as path from 'path-browserify';
import { URI } from 'vscode-uri';
import { SharedContext } from '../types';
import type { TextDocument } from 'vscode-languageserver-textdocument';

Expand All @@ -28,7 +27,7 @@ export async function getUserPreferences(
includeCompletionsWithSnippetText: config.suggest?.includeCompletionsWithSnippetText ?? true,
includeCompletionsWithClassMemberSnippets: config.suggest?.classMemberSnippets?.enabled ?? true,
includeCompletionsWithObjectLiteralMethodSnippets: config.suggest?.objectLiteralMethodSnippets?.enabled ?? true,
autoImportFileExcludePatterns: getAutoImportFileExcludePatternsPreference(preferencesConfig, ctx.env.rootUri),
autoImportFileExcludePatterns: getAutoImportFileExcludePatternsPreference(preferencesConfig, ctx.typescript.languageServiceHost.getCurrentDirectory()),
useLabelDetailsInCompletionEntries: true,
allowIncompleteCompletions: true,
displayPartsForJSDoc: true,
Expand Down Expand Up @@ -60,14 +59,14 @@ function getQuoteStylePreference(config: any) {
}
}

function getAutoImportFileExcludePatternsPreference(config: any, workspaceFolder: URI | undefined) {
return workspaceFolder && (config.autoImportFileExcludePatterns as string[] | undefined)?.map(p => {
function getAutoImportFileExcludePatternsPreference(config: any, workspacePath: string | undefined) {
return workspacePath && (config.autoImportFileExcludePatterns as string[] | undefined)?.map(p => {
// Normalization rules: https://github.com/microsoft/TypeScript/pull/49578
const slashNormalized = p.replace(/\\/g, '/');
const isRelative = /^\.\.?($|\/)/.test(slashNormalized);
return path.isAbsolute(p) ? p :
p.startsWith('*') ? '/' + slashNormalized :
isRelative ? URI.parse(path.join(workspaceFolder.toString(), p)).fsPath :
isRelative ? path.join(workspacePath, p) :
'/**/' + slashNormalized;
});
}
Expand Down
Loading

0 comments on commit 5f8b7a2

Please sign in to comment.