Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirect compiler trace to IDE through language server #5316

Merged
merged 3 commits into from
Dec 11, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@typespec/compiler"
---

Compiler trace will be sent to IDE as trace log through language server
5 changes: 3 additions & 2 deletions packages/compiler/src/core/logger/console-sink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { supportsHyperlink } from "./support-hyperlinks.js";
export interface FormatLogOptions {
pathRelativeTo?: string;
pretty?: boolean;
excludeLogLevel?: boolean;
}

export interface ConsoleSinkOptions extends FormatLogOptions {}
Expand All @@ -33,8 +34,8 @@ function hyperlink(text: string, url: string | undefined, options: FormatLogOpti

export function formatLog(log: ProcessedLog, options: FormatLogOptions): string {
const code = log.code ? ` ${hyperlink(color(options, log.code, pc.gray), log.url, options)}` : "";
const level = formatLevel(options, log.level);
const content = `${level}${code}: ${log.message}`;
const level: string = options.excludeLogLevel === true ? "" : formatLevel(options, log.level);
const content = level || code ? `${level}${code}: ${log.message}` : log.message;
const root = log.sourceLocation;
if (root?.file) {
const formattedLocation = formatSourceLocation(options, root);
Expand Down
13 changes: 12 additions & 1 deletion packages/compiler/src/server/serverlib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ import { resolveCodeFix } from "../core/code-fixes.js";
import { compilerAssert, getSourceLocation } from "../core/diagnostics.js";
import { formatTypeSpec } from "../core/formatter.js";
import { getEntityName, getTypeName } from "../core/helpers/type-name-utils.js";
import { resolveModule, ResolveModuleHost } from "../core/index.js";
import { ProcessedLog, resolveModule, ResolveModuleHost } from "../core/index.js";
import { formatLog } from "../core/logger/index.js";
import { getPositionBeforeTrivia } from "../core/parser-utils.js";
import { getNodeAtPosition, getNodeAtPositionDetail, visitChildren } from "../core/parser.js";
import { ensureTrailingDirectorySeparator, getDirectoryPath } from "../core/path-utils.js";
Expand Down Expand Up @@ -963,6 +964,16 @@ export function createServer(host: ServerHost): Server {
readFile,
stat,
getSourceFileKind,
logSink: {
log: (log: ProcessedLog) => {
const msg = formatLog(log, { excludeLogLevel: true });
const sLog: ServerLog = {
level: log.level,
message: msg,
};
host.log(sLog);
},
},
};

async function readFile(path: string): Promise<ServerSourceFile> {
Expand Down
Loading